-2

I'm currently working on an android app to fetch data from the server. It crashes and I get a NullPointerException. Can anyone help me edit my code/ list out my error?

Here is my code (I have read a through few of the forums but did not really understand how to solve my problem)

public class MainActivity extends AppCompatActivity {

    ListView lvPost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        String url2 = "http://192.168.0.146/client/login.php?format=json";
        PostResponseAsyncTask task2 = new PostResponseAsyncTask(MainActivity.this, new AsyncResponse() {
            @Override
            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) {
                    titles.add(value.post_title);
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_list_item_1, titles);

                lvPost.setAdapter(adapter);
            }
        });

        task2.execute(url2);
    }
}

Here is my logcat

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:6075)
     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)
Irshad
  • 3,071
  • 5
  • 30
  • 51
Rean
  • 11
  • 1
  • 1
  • 2

1 Answers1

2

It looks like String s parameter is null, use logging to confirm. In such case Gson.fromJson() returns null as a result. Then you're trying to iterate through the list postList which is null and this results to NullPointerException.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • Thanks for the reply but how do i use logging? create a new java.class or add in my Mainactivity.java? – Rean Jan 19 '16 at 07:20
  • Check http://developer.android.com/reference/android/util/Log.html . Or you can just stop at debugger and check the variable value. Or put `Preconditions.checkNotNull(s)` etc. – Zbynek Vyskovsky - kvr000 Jan 19 '16 at 07:25
  • How do i rewrite my coding so that i can solve my problem? – Rean Jan 19 '16 at 08:01