简体   繁体   中英

NullPointerException when trying to execute SQL

I'm getting a NullPointerException when trying to run some SQL against the database.

I have a class called CustomApp , which extends Application . This class creates a data helper class, which controls the creation and opening of a database. The instance of this data helper class is called data - the database inside this class is called db .

I then have my own class where I store static database functions for easy access - allowing me to perform the standard database stuff with only a single line of code. This is a class called Db , with a nested static class inside called Functions . Therefore, if I want to for example, insert a contact, I just call the Db.Functions.insertContact() function. The problem I'm having is on a function called 'setupDb()` that just runs table creation strings if the tables don't already exist.

My CustomApp class:

public class CustomApp extends Application
{

public static CustomDataHelper data;

public static CustomApp app;

@Override
public void onCreate()
{
    super.onCreate();

    app = this;
    data = new CustomDataHelper();
}
}

My CustomDataHelper class:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CustomDataHelper {

private static final int DATABASE_VERSION = 1;
public SQLiteDatabase db = null;

public CustomDataHelper()
{
    SQLiteOpenHelper o = new MyOpenHelper(CustomApp.app, "MyData.db");
    this.db = o.getWritableDatabase();
}
// Rest of standard DataHelper class

private class MyOpenHelper extends SQLiteOpenHelper
{
    MyOpenHelper(Context context,String DatabaseName)
    {
        super(context, DatabaseName, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
          // Standard data code here
    }

}
}

Relevant code from Db class:

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Db
{
public static Contact contact = null;

static final String TAG = "DbFunctions";    
public static class Functions
{       
    static final String TABLE_CONTACTS = "Contact";     
    static final String CONTACTS_ID = "id";
    static final String CONTACTS_FNAME = "firstName";
    static final String CONTACTS_LNAME = "lastName";
    static final String CONTACTS_MOBNUM = "mobileNumber";
    static final String CONTACTS_SQL = "create table if not exists " + TABLE_CONTACTS +
            "(" + CONTACTS_ID + " text not null, " + CONTACTS_FNAME + " text null, " + CONTACTS_LNAME + " text null, " +
            CONTACTS_MOBNUM + " text null)";

    static final String ALL_SQL = CONTACTS_SQL;

    public static boolean setupDb()
    {
        try 
        {
            CustomApp.data.db.execSQL(ALL_SQL);
            return true;
        }
        catch (Exception ex)
        {
            Log.e(TAG, ex.getMessage());
            return false;
        }
    }       
}
}

When I call the line of code: Db.Functions.setupDb(); , I get the NullPointerException .

My logcat:

07-09 16:24:26.596: E/AndroidRuntime(18660): FATAL EXCEPTION: main
07-09 16:24:26.596: E/AndroidRuntime(18660): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testproj/com.example.testproj.MainActivity}: java.lang.NullPointerException: println needs a message
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.os.Looper.loop(Looper.java:137)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.ActivityThread.main(ActivityThread.java:4921)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at java.lang.reflect.Method.invoke(Method.java:511)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at dalvik.system.NativeStart.main(Native Method)
07-09 16:24:26.596: E/AndroidRuntime(18660): Caused by: java.lang.NullPointerException: println needs a message
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.util.Log.println_native(Native Method)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.util.Log.e(Log.java:297)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at com.example.testproj.Db$Functions.setupDb(Db.java:55)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at com.example.testproj.MainActivity.onCreate(MainActivity.java:16)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.Activity.performCreate(Activity.java:5206)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-09 16:24:26.596: E/AndroidRuntime(18660):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-09 16:24:26.596: E/AndroidRuntime(18660):    ... 11 more

So I used breakpoints to track down which line of code was causing the problem, and its CustomApp.data.db.execSQL(ALL_SQL); . Now I found that this is because the data object is actually null - but why is this? When CustomApp is created, it should automatically have instantiated the datahelper object 'data' - which, in turn, should have automatically instantiated the database object. So that leaves me pretty stumped. Any help is greatly appreciated.

In method setupDb() you are calling Log.e(TAG, ex.getMessage()); - but in this case ex.getMessage() returns null . I don't know what your exception is, so can't comment on why, but one way to see what's going on is to do

Log.e(TAG, ex.toString());

I found out what the problem is. The reference to my CustomApp class had been somehow removed from my manifest by Eclipse (even though I definitely added it in). I'll keep an eye out to make sure this doesn't happen again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM