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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvPost = (ListView)findViewById(R.id.lvPost);
String url2 = "http://localhost/client/login.php";
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
FATAL EXCEPTION: main
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)