Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:40:05+00:00 2026-06-03T13:40:05+00:00

I am trying to write some variable data back to a database table when

  • 0

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).

My 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();


}


}

logcat:

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

I suspect my problem is that ‘jobsave.openToWrite()’ is pointing to nothing, but it is defintely there in my SQLiteAdapter.

If anyone can shed any light, i;d be extremely greatful!

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

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();
    }
}
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:40:08+00:00Added an answer on June 3, 2026 at 1:40 pm

    Actually, You are missed to initialize SQLiteAdapter jobSave class’s instance. And directly using its method openToWrite(). So first initialize jobSave instance and then use the method openToWrite().

    EDIT:

    In your activity’s onCreate() write code line,

    jobSave = new SQLiteAdapter(this);
    

    Now run again your code and let me know what happen..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some code that that will draw the line which
I'm trying to write a sql statement that will insert data given a few
I am trying to write a bash script(script.sh) to search and replace some variables
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I'm trying to write some code to Hide columns if the first 3 characters
I'm trying to write some code to remove the first N characters in a
I am trying to write some integration tests for some SQL server stored procedures
I am trying to write some code to return an array in C#, but
I'm trying to write some code to extract Exif information from a JPG. Exif
I am trying to write some generic LINQ queries for my entities, but am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.