samedi 2 mai 2015

android attempt to Invoke virtual method

So this is now starting to drive me crazy, I have the code that I am using in two different files, and it works in one file, but then in other file I get the error. Please do not mark this as duplicate, I have spent over 2 days looking through others codes to see if there is something that might help me.

Receiver file (This code works and stores in the information with no problem)

DatabaseOperations db = new DatabaseOperations(context); db.insertData(Data, getDateTime(), "99");

Activity file (This code does not work)

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xml);

    // DEFINE OUR BUTTONS TO THE XML
    sendBtn = (Button) findViewById(R.id.send_btn);
    MessageField = (EditText) findViewById(R.id.message_field);

    // OPEN OUR DATABASE
    openDB();

    // IF THEY CLICK SEND, WE WILL SEND THE DATA TO THE DB
    sendBtn.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            sendData();
        }
    });
}

protected void sendData()
{
    DatabaseOperations db = new DatabaseOperations(this);
    Log.i("Sending Data", "");

    String data_field = MessageField.getText().toString();
    String type = "99";
    String timestamp = getDateTime();

    Log.i("Data "Info: " + data_field +" | "+ timestamp +" | "+ type);

    db.insertData(data_field, timestamp, type);
    } 

My Database Insert

public long insertData(String data, String datetime, String type)
{
    Log.i("DataBase", "Inserting Data: " + data +","+ datetime +","+ type);

    //SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_DATA, data);
    values.put(KEY_DATETIME, datetime);
    values.put(KEY_TYPE, type);

    long data_id = db.insert(TABLE_DATA, null, values);
    // Closing database connection
    //db.close();

    return data_id;
}

My logcat file

05-02 09:56:03.585 27833:27833 I/ActivityFile]
Data Info: Testing Data | 2015-05-02 09:56:03 | 99
05-02 09:56:03.585  27833-27833/myapp.myapp I/DataBase﹕ Inserting Data: Testing Data,2015-05-02 09:56:03,99
 05-02 09:56:03.585  27833-27833/myapp.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: myapp.myapp, PID: 27833
java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
        at myapp.myapp.DatabaseOperations.insertData(DatabaseOperations.java:95)
        at myapp.myapp.ActivityFile.sendData(ActivityFile.java:82)
        at myapp.myapp.ActivityFile.onClick(ActivityFile.java:66)
        at android.view.View.performClick(View.java:5197)
        at android.view.View$PerformClick.run(View.java:20909)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5942)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Aucun commentaire:

Enregistrer un commentaire