I'm currently working on an android app to fetch data from the server. But when i run it, i get a NullPointerException. Can anyone help me with that? Here is my code:

public class MainActivity extends AppCompatActivity {

    ListView lvPost;

    protected void onCreate(Bundle savedInstanceState) {

        lvPost = (ListView)findViewById(R.id.lvPost);

        String url2 = "http://localhost/client/login.php";
        PostResponseAsyncTask task2 = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
            public void processFinish(String s) {
                ArrayList<Post> postList =
                        new JsonConverter<Post>().toArrayList(s, Post.class);

                ArrayList<String> titles = new ArrayList<String>();
                for (Post value: postList){
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this ,


Here is my logcat

Process: com.example.1231.t12, PID: 29294
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
at com.example.1231.t12.MainActivity$1.processFinish(MainActivity.java:40)
at com.kosalgeek.genasync12.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:188)
at com.kosalgeek.genasync12.PostResponseAsyncTask.onPostExecute(PostResponseAsyncTask.java:27)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Recommended Answers

All 4 Replies

You didn't say which of the many JSONConverter implementations you are using, and the line numbers in your posted code don't correspond to the exception message, but at a guess its the enhanced for on line 20 that throws the problem, implying that postList is null. Maybe you could print that var just before the loop to confirm?

Sorry . I am new to the programming , can you help me edit the codes ?

If you are new to programming, you should consider start learning at the beginning.
Jumping into more advanced techniques won't make learning it easier.

James told you what to do to check the first thing, in a way someone new to programming should be able to understand, let alone somebody who tries to write code like this.

i followed a tutorial on youtube to reach this coding , it was not written by me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.