Fadil Fadil - 6 months ago 34
Android Question

Android Programming java.lang.NullPointerException Custom Adapter

I am making a simple youtube playback app and seems like I am stuck with the custom list view adapter. It throws a error about java.lang.NullPointerException when I run the app. I can't seem to figure out the problem.

Here it the main activity

public class MainActivity extends AppCompatActivity {
private EditText searchInput;
private ListView videosFound;
private List<VideoItem> searchResults;
private Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

searchInput = (EditText) findViewById(R.id.search_input);
videosFound = (ListView) findViewById(R.id.videos_found);

handler = new Handler();

searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
searchOnYoutube(v.getText().toString());
return false;
}
return true;
}
});
}

private void addClickListener() {
videosFound.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> av, View v, int pos,
long id) {
Intent intent = new Intent(getApplicationContext(), PlayerActivity.class);
intent.putExtra("VIDEO_ID", searchResults.get(pos).getId());
startActivity(intent);
}

});
}

private void searchOnYoutube(final String keywords) {
new Thread() {
public void run() {
YoutubeConnector yc = new YoutubeConnector(MainActivity.this);
searchResults = yc.search(keywords);
handler.post(new Runnable() {
public void run() {
updateVideosFound();
}
});
}
}.start();
}

private void updateVideosFound() {
ArrayAdapter<VideoItem> adapter = new ArrayAdapter<VideoItem>(getApplicationContext(), R.layout.video_item, searchResults) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.video_item, parent, false);
}

VideoItem searchResult = searchResults.get(position);

ImageView thumbnail = (ImageView) convertView.findViewById(R.id.vid_thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.vid_title);
TextView description = (TextView) convertView.findViewById(R.id.vid_desc);

Picasso.with(getApplicationContext()).load(searchResult.getThumbnailURL()).into(thumbnail);
title.setText(searchResult.getTitle());
description.setText(searchResult.getDescription());
return convertView;
}
};
videosFound.setAdapter(adapter);
}
}


Here is the LogCat

10-12 17:49:44.624 16028-16028/com.example.fadil.youtubeplayer W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41678d58)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: FATAL EXCEPTION: main
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: Process: com.example.fadil.youtubeplayer, PID: 16028
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: java.lang.NullPointerException
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.widget.ListView.setAdapter(ListView.java:480)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.example.fadil.youtubeplayer.MainActivity.updateVideosFound(MainActivity.java:104)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.example.fadil.youtubeplayer.MainActivity.access$200(MainActivity.java:25)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.example.fadil.youtubeplayer.MainActivity$3$1.run(MainActivity.java:75)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5113)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
10-12 17:49:44.634 16028-16028/com.example.fadil.youtubeplayer E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-12 17:49:48.184 16028-16028/com.example.fadil.youtubeplayer I/Process: Sending signal. PID: 16028 SIG: 9


Please have a look and help me out. :)

Answer

My guess is that you don't assign any value to searchResults. Is your call to searchResults = yc.search(keywords); an asynchronous call? If yes than the call to updateVideosFound(); is to early. You should wait for the response and then call this method.