dimanche 8 mai 2016

NullPointerException in android sqlite [duplicate]

This question already has an answer here:

I am trying to create an action that gets a string value and returns String array of the items with the keyword in their name.

This is my code:

public String[] getList(String st) {

   SQLiteDatabase database = myDBHelper.getReadableDatabase();

   String query = "Select * from " + TASKS_TABLE + " WHERE name LIKE '%" + st + "%'";
   Cursor cursor = database.rawQuery(query,null);
   //String[] columns = {TASK_IMAGE};
   // cursor = database.query(TASKS_TABLE,columns, TASK_NAME + "='" + st + "'",null, null, null, null, null);
   String[] array = new String[cursor.getCount()];
   int i = 0;
   while(cursor.moveToNext()){
       String uname = cursor.getString(cursor.getColumnIndex("name"));
       array[i] = uname;
       i++;
   }

   return array;
}

When I run it I get:

05-08 18:18:28.353 6637-6637/? E/AndroidRuntime: FATAL EXCEPTION: main
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime: Process: com.sahar.wave,   PID: 6637
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{http://ift.tt/1rBQmWz}: java.lang.NullPointerException
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at com.sahar.wave.Results.onCreate(Results.java:26)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5231)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at  java.lang.reflect.Method.invoke(Method.java:515) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
05-08 18:18:28.353 6637-6637/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

This is the Activity code:

private SiteDBMngr dbMngr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);

    String st = "c";//-------------------------------put here the search key

   // String[] sites = new String[2];
    String[] sites = dbMngr.getList(st);
    ListAdapter Adapter = new CustomAdapter(this, sites);
    ListView listView = (ListView) findViewById(R.id.sitesListView);
    listView.setAdapter(Adapter);

    listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            }
    );
}

Aucun commentaire:

Enregistrer un commentaire