user2784556 user2784556 - 3 months ago 13
Java Question

How to debug a java.lang.NullPointerException error in Java?

I'm trying to load Album Art using

AsyncTask
but I get
NullPointerException
:

ImageLoader:

public class ImageLoader extends AsyncTask<String, Void, Bitmap>{
Context mcontext;
private ImageView image;
Cursor cursor;
private Bitmap artwork = null;
SongsListAdapter songsAdapter;

public ImageLoader(ImageView image) {
this.image = image;
}

protected Bitmap doInBackground(String... uri) {
for (int i=0; i < songsAdapter.getCount(); i++) {
cursor.moveToPosition(i);
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Long albumid = cursor.getLong(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Uri art = ContentUris.withAppendedId(sArtworkUri, albumid);
ContentResolver res = mcontext.getContentResolver();
InputStream in = null;
try {
in = res.openInputStream(art);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
artwork = BitmapFactory.decodeStream(in);
}
return null;
}

protected void onPostExecute(Void arg) {
// Display the image
image.setImageBitmap(artwork);
}
}


SongsListAdapter:

public class SongsListAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<MusikUntil> songList;

public SongsListAdapter(Context context, ArrayList<MusikUntil> list) {
this.mContext = context;
this.songList = list;
}

public int getCount() {
return songList.size();
}

public MusikUntil getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

@SuppressLint("InflateParams")
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.song_item, null);
}

ImageView mImgIcon = (ImageView)convertView.findViewById(R.id.list_image);
TextView mTxtJudul = (TextView)convertView.findViewById(R.id.Judul_Musik);
TextView mTxtMusisi = (TextView)convertView.findViewById(R.id.Artis);

new ImageLoader(mImgIcon).execute();
mTxtJudul.setText(songList.get(position).getSongName());
mTxtMusisi.setText(songList.get(position).getArtistName());

return convertView;
}

public void setSongsList (ArrayList<MusikUntil> list) {
songList = list;
this.notifyDataSetChanged();
}

}


Logcat:

Process: com.karyaanaknegeri.musikq, PID: 5736
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.karyaanaknegeri.musikq.adapters.SongsListAdapter.getCount()' on a null object reference
at com.karyaanaknegeri.musikq.loader.ImageLoader.doInBackground(ImageLoader.java:40)
at com.karyaanaknegeri.musikq.loader.ImageLoader.doInBackground(ImageLoader.java:28)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

Answer

1. Problems in ImageLoader

You have only declared SongsListAdapter songsAdapter;. Before usage, you must initialize this object first.

You didn't initialize Cursor cursor;, so it will throw NullPointerException too.


2. Problems in SongsListAdapter

Change:

public MusikUntil getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

to

public MusikUntil getItem(int position) {
    return songList.get(position);
}

public long getItemId(int position) {
    return position;
}

3. Tip for you

Your code looks like copy-paste from some tutorial. Try Picasso or Universal Image Loader for Android, if you have problems with AsyncTask. These libraries are very easy to use and will help you deal with loading images.

Comments