Paul Robert Carlson
Paul Robert Carlson

Reputation: 169

getWritableDatabase causes nullpointerexception

I am working on a project using a Database that stores specific information. The database works for part of the program, but yet for others it does not. When the user first starts the program they will see information this is pulled from the db, and they also have the option to add new information. The db works fine for both of these.

What I am having issues with is at another spot of adding information. When I go to open the db, my program crashes. LOGCAT:

08-29 20:22:47.000: E/AndroidRuntime(3022): FATAL EXCEPTION: main
08-29 20:22:47.000: E/AndroidRuntime(3022): java.lang.NullPointerException
08-29 20:22:47.000: E/AndroidRuntime(3022):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
08-29 20:22:47.000: E/AndroidRuntime(3022):     at android.database.sqlite.SQLiteOpenHelper._getWritableDatabase(SQLiteOpenHelper.java:164)
08-29 20:22:47.000: E/AndroidRuntime(3022):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:100)
08-29 20:22:47.000: E/AndroidRuntime(3022):     at peices.DbM.open(DbM.java:173)
08-29 20:22:47.000: E/AndroidRuntime(3022):     at peices.NewStuff.CreateNewUnit(NewStuff.java:27)
08-29 20:22:47.000: E/AndroidRuntime(3022):     at carlson.msum.arch.Site$5.onClick(Site.java:231)
08-29 20:22:47.000: E/AndroidRuntime(3022):     at android.view.View.performClick(View.java:2538)

Code for NewStuff:27(Lines 20-30):

bb=Lon.getText().toString();
            cc=UnitName.getText().toString();
            Log.d("Class",aa);
            Log.d("Class",bb);
            Log.d("Class",cc);
            Log.d("Class",SiteName);
            Log.d("Stuff","Before Open");
            **db.open();**
            Log.d("Stuff","Before Insert/After Open");
            db.InsertUnit(SiteName, cc, aa, bb);
            Log.d("Stuff","Before Close/After Insert");
            db.close();

Code for DbM.java:173(170-176)

public DbM open() throws SQLException
{
    Log.d("BmDb","open");
    **db= DbMh.getWritableDatabase();**
    Log.d("BmDb","Open before return");
    return this;
}

I already know it has somthing to do with the opening of the db, but why would it open and work for other activities, but not this one? I checked out and all activities that would be before this one, and I did close all of the db's.

this is the start of the class, the DbM initialization is how I do it for all the others.

public class NewStuff extends Activity {
Context context;
**DbM db=new DbM(this);**

One thing it could be, but not sure how to fix it is that this is a class of objects that I am using in other parts of the program. So I am not calling any oncreates or anything like that, would that be causing my issue?

*EDIT*

This is my entire NewStuff class, I must be missing somthing else other then just the context because nothing seems to work.

package peices;
import peices.DbM;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.app.Activity;
import android.content.Context;

public class NewStuff extends Activity {
Context context;
DbM db;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db=new DbM(this);
        }
    public void CreateNewUnit(EditText Lat, EditText Lon, EditText UnitName, String SiteName)
    {

            db=new DbM(this);
            Log.d("NewUnit","Start of");
            String aa="";
            String bb="";
            String cc="";

            aa=Lat.getText().toString();
            bb=Lon.getText().toString();
            cc=UnitName.getText().toString();
            Log.d("Class",aa);
            Log.d("Class",bb);
            Log.d("Class",cc);
            Log.d("Class",SiteName);
            Log.d("Stuff","Before Open");
            db.open();
            Log.d("Stuff","Before Insert/After Open");
            db.InsertUnit(SiteName, cc, aa, bb);
            Log.d("Stuff","Before Close/After Insert");
            db.close();     
    }

EDIT

The onclick function that calls NewStuff

ucreate.setOnClickListener(new OnClickListener(){
        public void onClick(View m){
            Log.d("Ubox","Unit Name: "+uname.getText().toString());
            Log.d("Ubox","Unit Longitude: "+ulon.getText().toString());
            Log.d("Ubox","Unit Latitude: "+ulat.getText().toString());
            Log.d("Ubox","Site Name: "+SiteName);
            NewStuff ns= new NewStuff();
            ns.CreateNewUnit(ulat,ulon,uname,SiteName);
            Intent I= new Intent(Site.this, Site.class);
            I.putExtra("SITE", SiteName);
            startActivity(I);

Upvotes: 0

Views: 457

Answers (2)

nandeesh
nandeesh

Reputation: 24820

You cannot initialize DbM as a field, Since the Context would not have been initialized

 DbM db=new DbM(this);

Call

 db=new DbM(this); 

only in or after OnCreate.

Edit: You cannot call

NewStuff ns= new NewStuff()

This is because if you instantiate yourself , the context for the activity will not be set. So this inside new DbM(this); will have no meaning.

Mainly you should not usually call a function an activity from another Activity.

Upvotes: 1

Lazy Ninja
Lazy Ninja

Reputation: 22527

Define db this way

public class NewStuff extends Activity {
Context context;
DbM db;

then onCreate(

onCreate(){
........
   db=new DbM(this); 
}

Thats what @nandeesh is trying to tell you.

Upvotes: 1

Related Questions