开发者

SQLiteOpenHelper NullPointerException [closed]

开发者 https://www.devze.com 2023-02-18 07:59 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

I have another post here that is similar to this one, however, i have made some changes to my classes and having a different issue now. The previous issue was also never resolved.

My application needs to be able to write to the sqlite3 database that is on the android from 2 different events. One of my events is writing to the database just fine. When the second event tries to write to the database the attached error occurs.

I am unable to resolve this issue. I have been looking at this for over a week. Any help is greatly appreciated. If any other information is needed, PLEASE let me know! I开发者_如何学C will post everything i have as long as i can get this issue resolved, it is stressing me out.

The Error Logcat
03-22 23:50:27.065: INFO/System.out(281): Where: DB-submitData
03-22 23:50:30.846: WARN/System.err(281): java.lang.NullPointerException
03-22 23:50:30.865: WARN/System.err(281):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
03-22 23:50:30.865: WARN/System.err(281):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
03-22 23:50:30.875: WARN/System.err(281):     at cpe495.smartapp.SmartDBHelper.open(SmartDBHelper.java:70)
03-22 23:50:30.875: WARN/System.err(281):     at cpe495.smartapp.DataBuilder.submitData(DataBuilder.java:37)
03-22 23:50:30.884: WARN/System.err(281):     at cpe495.smartapp.DataBuilder.prepareData(DataBuilder.java:29)
03-22 23:50:30.904: WARN/System.err(281):     at cpe495.smartapp.SmartApp$2.dataAnalyzedReceived(SmartApp.java:56)
03-22 23:50:30.904: WARN/System.err(281):     at cpe495.smartapp.DataRobot.fireDataAnalyzedEvent(DataRobot.java:269)
03-22 23:50:30.916: WARN/System.err(281):     at cpe495.smartapp.DataRobot.analyzeData(DataRobot.java:79)
03-22 23:50:30.925: WARN/System.err(281):     at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:49)
03-22 23:50:30.935: WARN/System.err(281):     at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79)
03-22 23:50:30.945: WARN/System.err(281):     at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46)
03-22 23:50:30.945: WARN/System.err(281):     at java.lang.Thread.run(Thread.java:1096)  

//The main class SmartApp.java
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener {
    TextView smartConnectionStatus;
    TextView testOutputView;
    Thread cThread;
    private ConnectDevice cD = new ConnectDevice();
    private DataRobot dR = new DataRobot(this);
    private DataBuilder dB = new DataBuilder();
    private DataSender dS = new DataSender(this);

    Handler mHandler = new Handler();

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

        cD.addDataReceivedListener(new DataReceivedListener() {
            @Override
            public void dataReceivedReceived(DataReceivedEvent event) {
                // TODO Auto-generated method stub
                dR.analyzeData(event.getData());
            }
        });
        dR.addDataAnalyzedListener(new DataAnalyzedListener() {
            @Override
            public void dataAnalyzedReceived(DataAnalyzedEvent event) {
                // TODO Auto-generated method stub
                dB.prepareData(event.getData());
            }
        });
        dR.addDataAlertListener(new DataAlertListener() {
            @Override
            public void dataAlertReceived(DataAlertEvent event) {
                Log.v("SmartApp", "data alert event caught");
                DataAlert a = new DataAlert(SmartApp.this);
                mHandler.post(a);
            }
        });
        dR.addDataNotifyListener(new DataNotifyListener() {
            @Override
            public void dataNotifyReceived(DataNotifyEvent event) {
                // TODO Auto-generated method stub
                Log.v("SmartApp", "data notification event caught");
                DataNotification a = new DataNotification(SmartApp.this);
                mHandler.post(a);
            }
        });
        dB.addDataBuilderListener(new DataBuilderListener() {
            @Override
            public void dataBuilderReceived(DataBuilderEvent event) {
                // TODO Auto-generated method stub
                dS.sendData(event.getData());
            }
        });
    }
    private Context getContext() {
        // TODO Auto-generated method stub
        return null;
    }
}   

//The DataBuilder.java class that fails to access the database
public class DataBuilder extends Activity {
    private List _listeners = new ArrayList();
    private SmartDataObject data;
    SmartDBHelper sDBH = new SmartDBHelper(this);

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("databuilder", "on create");
    }

    public void prepareData(SmartDataObject temp) {
        submitData(temp);
    }

    public void submitData(SmartDataObject temp) {
        data = temp;
        System.out.println("Where: DB-submitData");
        try {
            sDBH.open();
            sDBH.insertDataResponse(data.getHeartRate(), data.getAct(), data.getTimeStamp());
            sDBH.close();
            fireDataBuilderEvent(data);
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(NullPointerException e) {
            e.printStackTrace();
        }
    }
    public synchronized void addDataBuilderListener(DataBuilderListener listener) {
        _listeners.add(listener);
    }
    public synchronized void removeDataBuilderListener(DataBuilderListener listener) {
        _listeners.remove(listener);
    }
    private synchronized void fireDataBuilderEvent(SmartDataObject temp) {
        DataBuilderEvent dRE = new DataBuilderEvent(this, temp);
        Iterator listeners = _listeners.iterator();
        while(listeners.hasNext()) {
            ((DataBuilderListener)listeners.next()).dataBuilderReceived(dRE);
        }
    }
    public interface DataBuilderListener {
        public void dataBuilderReceived(DataBuilderEvent event);
    }
}   

//The DataNotificationSurvey.java class that access the database successfully.
public class DataNotificationSurvey extends Activity {
    private Date timeStamp;
    private Uri mUri;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datanotificationlayout);
        Log.v("datanotificationsurvey", "inside datanotificationsurvey");


        timeStamp = new Date(DataNotification.when);

        TextView notifyDate = (TextView) findViewById(R.id.notifyDateTV);
        notifyDate.setText(timeStamp.toLocaleString());
        final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton);
        final RadioButton patientCrisisRB = (RadioButton) findViewById(R.id.patientCrisis);
        final RadioButton physicalActivityRB = (RadioButton) findViewById(R.id.physicalActivity);

        notifySubmitButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        if(patientCrisisRB.isChecked()) {
                            submitNotify(1, timeStamp);
                        }
                        else if(physicalActivityRB.isChecked()) {
                            submitNotify(2, timeStamp);
                        }
                        finish();
                    }
                });
    }

    public void submitNotify(int tempType, Date tempDate) {
        SmartDBHelper sDBH = new SmartDBHelper(this);
        sDBH.open();
        sDBH.insertNotificationResponse(tempType, tempDate);
        sDBH.close();
        /*ContentValues values = new ContentValues();
        values.put("userresponse", tempType);
        values.put("notifytime", (tempDate.getTime()/1000));
        mUri = getContentResolver().insert(intent.getData(), values);*/
    }
}   

//The SQLiteOpenHelper class, it extends this DatabaseHelper.
public class SmartDBHelper extends Activity {

    private DatabaseHelper dBH;
    private SQLiteDatabase db;
    private final Context mCtx;

    private static final String DATABASE_NAME = "smart_lite_db.db";
    private static final int DATABASE_VERSION = 2;
    private static final String NOTIFY_TABLE_NAME = "user_notify_data";
    private static final String HR_TABLE_NAME = "user_hr_data";
    private static final String NOTIFY_TABLE_CREATE = 
        "CREATE TABLE " + NOTIFY_TABLE_NAME + 
        " (counter INTEGER PRIMARY KEY, " + 
        "userresponse INTEGER, " + 
        "notifytime INTEGER);";
    private static final String DATA_TABLE_CREATE = 
        "CREATE TABLE " + HR_TABLE_NAME +
        " (counter INTEGER PRIMARY KEY, " +
        "hr INTEGER, " +
        "act INTEGER, " +
        "timestamp INTEGER);";

    static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            Log.v("smartdbhelper", "before creation");
            db.execSQL(NOTIFY_TABLE_CREATE);
            Log.v("smartdbhelper", "middle creation");
            db.execSQL(DATA_TABLE_CREATE);
            Log.v("smartdbhelper", "after creation");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }
    }

    public SmartDBHelper(Context context) {
        this.mCtx = context;
    }

    public SmartDBHelper open() throws SQLException {
        dBH = new DatabaseHelper(mCtx);
        db = dBH.getWritableDatabase();
        return this;
    }

    public void close() {
        dBH.close();
    }

    public long insertNotificationResponse(int tempType, Date tempDate) {
        ContentValues values = new ContentValues();
        values.put("userresponse", tempType);
        values.put("notifytime", (tempDate.getTime()/1000));
        return db.insert(NOTIFY_TABLE_NAME, null, values);
    }

    public long insertDataResponse(double tempAct, int tempHR, long tempDate) {
        ContentValues values = new ContentValues();
        values.put("hr", tempHR);
        values.put("act", tempAct);
        values.put("timestamp", (tempDate/1000));
        return db.insert(HR_TABLE_NAME, null, values);
    }
}


This is an old question, but its accepted answer is wrong and the author can't delete the whole thing so for the sake of future visitors here is the answer.

In DataBuilder you are passing the Context before it has been initialized to SmartDBHelper's constructor.

SmartDBHelper sDBH = new SmartDBHelper(this);

You must wait until the onCreate() to access a valid Context:

public class DataBuilder extends Activity {
    private List _listeners = new ArrayList();
    private SmartDataObject data;
    SmartDBHelper sDBH;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("databuilder", "on create");
        sDBH = new SmartDBHelper(this);
    }

    // Rest of class
}

Also I don't believe DataBuilder or SmartDBHelper should extend Activity. They don't use any of Activity's methods and you cannot start an Activity by calling new DataBulder():

public class DataBuidler {

public class SmartDBHelper {


SQLiteOpenHelper.java:98 is db = mContext.openOrCreateDatabase(mName, 0, mFactory);

so you set a null context in SQLiteOpenHelper


You DatabaseHelper class is defined as static. That is causing all the problem. When a nested class is declared as static it doesnt have access to instance variables and method of the enclosing class

More Details read Nested Class in Java

0

精彩评论

暂无评论...
验证码 换一张
取 消