0

I have tried all forms of error checking in order to catch and deal with the error checking but nothing works and it keeps crashing.

Cursor cursor = null;
    try{
     cursor  = mydb.getAllRows();
    }finally{
        if(cursor == null)
        {
            // I want to exit here and do nothing. 
            // Crashes here. 

        }
        else{if(cursor.moveToFirst()){
             do{
                String id= cursor.getString(0);
                String text = cursor.getString(1);
                String cooktime = cursor.getString(2);
                String difficulty = cursor.getString(3);
                String making = cursor.getString(4);




                }while(cursor.moveToNext());
             }
          cursor.close();

        }

    }

I know mydb has nothing in it, so how do I error check with this issue so it doesn't crash.

Error Log

12-03 14:54:21.259: E/AndroidRuntime(16766): FATAL EXCEPTION: main
12-03 14:54:21.259: E/AndroidRuntime(16766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exasmple.test/com.exasmple.test.List_of_recipes}: java.lang.NullPointerException
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.ActivityThread.access$900(ActivityThread.java:148)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.os.Looper.loop(Looper.java:137)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.ActivityThread.main(ActivityThread.java:5457)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at java.lang.reflect.Method.invokeNative(Native Method)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at java.lang.reflect.Method.invoke(Method.java:525)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at dalvik.system.NativeStart.main(Native Method)
12-03 14:54:21.259: E/AndroidRuntime(16766): Caused by: java.lang.NullPointerException
12-03 14:54:21.259: E/AndroidRuntime(16766):    at com.exasmple.test.List_of_recipes.onCreate(List_of_recipes.java:114)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.Activity.performCreate(Activity.java:5234)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-03 14:54:21.259: E/AndroidRuntime(16766):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
12-03 14:54:21.259: E/AndroidRuntime(16766):    ... 11 more

EDIT: Thanks for the help everyone, I used a try catch method as suggested by Nathan.

try{
        Cursor cursor  = mydb.getAllRows();

        // Do everything here

    }catch (NullPointerException e){
         // if caught then I do nothing and move on. 
    }

This stopped my problem from crashing instead of try{} and finally{}. The problem was also found within the .getAllRows() class call.

Johnathan Logan
  • 357
  • 5
  • 14

0 Answers0