onPause() NullPointerException - trying to write data to database onPause
<p>I am trying to write some variable data back to a database table when the App is paused, for later retrieval. I will admit to beign a complete noob at this point (we all gotta start somewhere).</p>

<p>My Code:</p>

<pre><code>package com.android.dbtest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;





import android.R.layout;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.InputEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;


@SuppressWarnings("unused")
public class jobActivity extends Activity {   

public static final String LOG_TAG = "dbtest";

public SQLiteAdapter jobSave;
//public String buttText;
//SimpleCursorAdapter cursorAdapter;
//String cursor;    

public void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);          
setContentView(R.layout.joblayout); 


final EditText jobIDInput = (EditText) findViewById(R.id.jobIDText);  
final EditText jobAddress = (EditText) findViewById(R.id.jobAddressText);  
final EditText jobPCInput = (EditText) findViewById(R.id.jobPostcodeText); 

jobIDInput.setText(String.valueOf(Property.jobId));
jobAddress.setText(String.valueOf(Property.jobAddress));
jobPCInput.setText(String.valueOf(Property.jobPostCode));

Log.v(LOG_TAG, "Property part 2:" + Property.jobAddress);

jobIDInput.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable id) {
        Property.jobId = jobIDInput.getText().toString();

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        Property.jobId = jobIDInput.getText().toString();

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

});

jobAddress.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable id) {
        Property.jobAddress = jobAddress.getText().toString();



    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        Property.jobAddress = jobAddress.getText().toString();

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

});

jobPCInput.addTextChangedListener(new TextWatcher(){

    @Override
    public void afterTextChanged(Editable id) {
        Property.jobPostCode = jobPCInput.getText().toString();

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {


    }

});




}


protected void onPause(){   

Log.v(LOG_TAG, "job state called");
saveJobState();
super.onPause();

}

private void saveJobState() {
// TODO Auto-generated method stub

String id = Property.jobId;
String jobAddress = Property.jobAddress;
String jobPostCode = Property.jobPostCode;

Log.v(LOG_TAG, "Paused");
jobSave.openToWrite();         // This is line 388
Log.v(LOG_TAG, "open to write ok");
jobSave.insertjob(id, jobAddress, jobPostCode);
jobSave.close();


}


}
</code></pre>

<p>logcat:</p>

<pre><code>05-09 06:04:30.180: ERROR/AndroidRuntime(21996): FATAL EXCEPTION: main
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): java.lang.RuntimeException: Unable to pause activity {com.android.dbtest/com.android.dbtest.jobActivity}: java.lang.NullPointerException
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.LocalActivityManager.performPause(LocalActivityManager.java:200)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:184)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:288)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:691)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.widget.TabHost.setCurrentTab(TabHost.java:341)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:140)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.view.View.performClick(View.java:2538)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.view.View$PerformClick.run(View.java:9152)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.os.Handler.handleCallback(Handler.java:587)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.os.Looper.loop(Looper.java:130)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.ActivityThread.main(ActivityThread.java:3691)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at java.lang.reflect.Method.invoke(Method.java:507)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at dalvik.system.NativeStart.main(Native Method)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996): Caused by: java.lang.NullPointerException
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at com.android.dbtest.jobActivity.saveJobState(jobActivity.java:388)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at com.android.dbtest.jobActivity.onPause(jobActivity.java:375)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.Activity.performPause(Activity.java:3877)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
05-09 06:04:30.180: ERROR/AndroidRuntime(21996):     ... 19 more
</code></pre>

<p>I suspect my problem is that 'jobsave.openToWrite()' is pointing to nothing, but it is defintely there in my SQLiteAdapter.</p>

<p>If anyone can shed any light, i;d be extremely greatful!</p>

<p>EDIT: -OpenTo Write code from SQLiteAdapter Added below.
    package com.android.dbtest;</p>

<pre><code>import android.content.ContentValues;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;


public class SQLiteAdapter { 
private SQLiteHelper sqLiteHelper; 
private SQLiteDatabase sqLiteDatabase;
private Context context;  
public SQLiteAdapter(Context c)
{  
    context = c;
}  

public SQLiteAdapter openToRead() throws android.database.SQLException 
{ 
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
    sqLiteDatabase = sqLiteHelper.getReadableDatabase();
    return this; 
} 

public SQLiteAdapter openToWrite() throws android.database.SQLException {
    sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
    sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
    return this;
    }

public class SQLiteHelper extends SQLiteOpenHelper 
{  
    public SQLiteHelper(Context context, String name,    CursorFactory factory, int version) 
    {  
        super(context, name, factory, version); 
        }
    public void onCreate(SQLiteDatabase db)
    {   
        Log.v (LOG_TAG, "Create " + SCRIPT_CREATE_DATABASE);
        // TODO Auto-generated method stub 
        Log.v (LOG_TAG, "Create DB: ");
        db.execSQL(SCRIPT_CREATE_DATABASE); 
        db.execSQL(SCRIPT_CREATE_COUNT);
        } 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {  
        Log.w(LOG_TAG,
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + MYDATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS Count");
        onCreate(db);

}
    public void onPause(){
        updateAll();
    }
}
}
</code></pre>

<p>I have chopped out the database create methods, for the sake of making post significantly shorter. Plus i am 99.99% sure they work as i am able to post and show data from the table.</p>
