manageQuery causes null pointer exception

All your problems with Audio, Video and Images.

manageQuery causes null pointer exception

Postby rugers285 » Wed Feb 16, 2011 11:57 pm

This has been giving me some trouble. I have tried to do everything. My goal is to search and display the music files on the sd card. So far this is my code for that

Code: Select all
package com.rugers285.ringtonePicker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ringtonePickerMediaStore extends Activity {
      private Cursor cur;
      private int audio_column_index;
      ListView audiolist;
      int count;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init_phone_video_grid();
      }

      private void init_phone_video_grid() {
            System.gc();
            Log.i("Garbage Collection", "Collected");
            String[] proj = {MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION };
            Log.i("String[] proj", "Created");
            try{
               Log.i("Try managedQuery", "Tried");
               cur = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
            }
           
            count = cur.getCount();
            audiolist = (ListView) findViewById(R.id.mediastorelistview);
            audiolist.setAdapter(new AudioAdapter(getApplicationContext()));
            audiolist.setOnItemClickListener(audiogridlistener);
      }

      private OnItemClickListener audiogridlistener = new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
long id) {
                  System.gc();
                  audio_column_index = cur
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                  cur.moveToPosition(position);
                  String filename = cur.getString(audio_column_index);
                  Intent intent = new Intent(ringtonePickerMediaStore.this, ViewAudio.class);
                  intent.putExtra("audiofilename", filename);
                  startActivity(intent);
            }
      };

      public class AudioAdapter extends BaseAdapter {
            private Context vContext;

            public AudioAdapter(Context c) {
                  vContext = c;
            }

            public int getCount() {
                  return count;
            }

            public Object getItem(int position) {
                  return position;
            }

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

            public View getView(int position, View convertView, ViewGroup parent) {
                  System.gc();
                  TextView tv = new TextView(vContext.getApplicationContext());
                  String id = null;
                  if (convertView == null) {
                        audio_column_index = cur
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                        cur.moveToPosition(position);
                        id = cur.getString(audio_column_index);
                        audio_column_index = cur
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
                        cur.moveToPosition(position);
                        id += " Size(KB):" + cur.getString(audio_column_index);
                        tv.setText(id);
                  } else
                        tv = (TextView) convertView;
                  return tv;
            }
      }
}


Now that that is the code this is the error


02-16 17:35:56.261: ERROR/AndroidRuntime(370): FATAL EXCEPTION: main
02-16 17:35:56.261: ERROR/AndroidRuntime(370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rugers285.ringtonePicker/com.rugers285.ringtonePicker.ringtonePickerMediaStore}: java.lang.NullPointerException
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.os.Looper.loop(Looper.java:123)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at dalvik.system.NativeStart.main(Native Method)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): Caused by: java.lang.NullPointerException
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at com.rugers285.ringtonePicker.ringtonePickerMediaStore.init_phone_video_grid(ringtonePickerMediaStore.java:48)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at com.rugers285.ringtonePicker.ringtonePickerMediaStore.onCreate(ringtonePickerMediaStore.java:29)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-16 17:35:56.261: ERROR/AndroidRuntime(370): ... 11 more


Would appreciate any help.

Thanks

Justin
rugers285
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 16, 2011 11:43 pm

Top

Re: manageQuery causes null pointer exception

Postby raj_J2ME » Thu Feb 17, 2011 9:47 am

rugers, check what could be the reason on the line number 4 inside ringtonePickerMediaStore.java?
Please try to debug the code using the println's statement to get the exact location of the exception... :)
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 16 guests