0

I am coding Android, sql atm and I need your help. Cant launch the app cause I got a nullpointer, and I cant find it...

So I have tried to read alot but I don't think I understand everything clearly here. So it would be awesome if you can try tell me what is wrong.

Can you help me out?

Thanks to everyone reading this! :)

>public class DBAdapter
>{
>   //Variables and so on..
>   public static final String KEY_ROWID = "_id";
>   public static final String KEY_ISBN = "isbn";
>   public static final String KEY_TITLE = "title";
>   public static final String KEY_PUBLISHER ="publisher";
>   private static final String TAG ="DBAdapter";
>   private static final String DATABASE_NAME = "books";
>   private static final String DATABASE_TABLE = "titles";
>   private static final int DATABASE_VERSION = 1;
>   private static final String DATABASE_CREATE = "create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null);";
>   
>   private final Context context;
>   
>   private DatabaseHelper DBHelper;
>   private SQLiteDatabase db;
>   
>   public DBAdapter(Context ctx)
>   {
>       this.context = ctx;
>       DBHelper = new DatabaseHelper(context);
>   }
>   
>   private static class DatabaseHelper extends SQLiteOpenHelper
>   {
>       DatabaseHelper(Context context)
>       {
>           super(context, DATABASE_NAME, null, DATABASE_VERSION);
>       }
>       @Override
>       public void onCreate(SQLiteDatabase db)
>       {
>           db.execSQL(DATABASE_CREATE);
>       }
>       @Override
>       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
>       {
>           Log.w(TAG, "Upgrading database from version "+oldVersion+" to >"+newVersion+" which will destory all old data");
>           db.execSQL("DROP TABLE IF EXISTS titles");
>           onCreate(db);
>       }
>   }
>
>   //opens database
>   public DBAdapter open() throws SQLException
>   {
>       db = DBHelper.getWritableDatabase();
>       return this;
>   }
>   
>   //closes database
>   public void close()
>   {
>       DBHelper.close();
>   }
>   
>   //insert title to database
>   ContentValues initialValues = new ContentValues();
>   public long insertTitle(String isbn, String title, String publisher)
>   {
>       initialValues.put(KEY_ISBN, isbn);
>       initialValues.put(KEY_TITLE, title);
>       initialValues.put(KEY_PUBLISHER, publisher);
>       return db.insert(DATABASE_TABLE, null, initialValues);
>   }
>   
>   //Delete specific title
>   public boolean deleteTitle(long rowId)
>   {
>       return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
>   }
>   
>   //Retrieves all titles
>   public Cursor getAllTitles()
>   {
>       return db.query(DATABASE_TABLE, new String[]
>       {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, null, null, null, null, >null);
>   }
>   
>   //Retrieves a particular title
>   public Cursor getTitle(Long rowId) throws SQLException
>   {
>       Cursor mCursor = db.query(true, DATABASE_TABLE, new String[]
>               {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, KEY_ROWID >+ "=" + rowId, null, null, null, null, null);
>       if(mCursor != null)
>       {
>           mCursor.moveToFirst();
>       }
>       return mCursor;
>   }
>   
>   //Update a title
>   ContentValues args = new ContentValues();
>   public boolean updateTitle(long rowId, String title, String isbn, String publisher)
>   {
>       args.put(KEY_ISBN, isbn);
>       args.put(KEY_TITLE, title);
>       args.put(KEY_PUBLISHER, publisher);
>       return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
>   }
>}

EDIT:

Here is my code were i use the DBAdapter class:

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

        DBAdapter db = new DBAdapter(this);

        //Add 2 titles
        db.open();
        long id;
        id = db.insertTitle("0470285818", "C# 2008 Programmer's Reference", "Wrox");
        id = db.insertTitle("047017661X", "Professional Windows Vista Gadgets Programming", "Wrox");
        db.close();

        //Get all titles
        db.open();
        Cursor c = db.getAllTitles();
        if(c.moveToFirst())
        {
            do
            {
                DisplayTitle(c);
            }
            while(c.moveToNext());
            {

            }
        }
        db.close();

        //get a title
        db.open();
        Cursor c1 = db.getTitle((long) 2);
        if(c1.moveToFirst())
        {
            DisplayTitle(c1);
        }
        else
        {
            Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
        }
        db.close();

        //Update a title
        db.open();
        if(db.updateTitle(1, "0470285818", "C# 2008 Programmer's Reference", "Wrox Press"))
        {
            Toast.makeText(this, "Update successful", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(this, "Update failed", Toast.LENGTH_LONG).show();
        }
        db.close();
        //Retrive the updated title
        Cursor c2 = db.getTitle((long) 1);
        if(c.moveToFirst())
        {
            DisplayTitle(c2);
        }
        else
        {
            Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
        }
    }

    public void DisplayTitle(Cursor c)
    {
        Toast.makeText(this, "id: "+c.getString(0)+"\n"+"ISBN: "+c.getString(1)+"\n"+"Title: "+c.getString(2)+"\n"+"Publisher: "+c.getString(3), Toast.LENGTH_LONG).show();
    }

}

I managed to get some output from my logcat, here it is:

06-14 08:43:00.882: D/AndroidRuntime(426): Shutting down VM
06-14 08:43:00.892: W/dalvikvm(426): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 08:43:00.912: E/AndroidRuntime(426): FATAL EXCEPTION: main
06-14 08:43:00.912: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{databas.test.org/databas.test.org.DatabasActivity}: java.lang.NullPointerException
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.os.Looper.loop(Looper.java:123)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 08:43:00.912: E/AndroidRuntime(426):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 08:43:00.912: E/AndroidRuntime(426):  at java.lang.reflect.Method.invoke(Method.java:507)
06-14 08:43:00.912: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 08:43:00.912: E/AndroidRuntime(426):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 08:43:00.912: E/AndroidRuntime(426):  at dalvik.system.NativeStart.main(Native Method)
06-14 08:43:00.912: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException
06-14 08:43:00.912: E/AndroidRuntime(426):  at databas.test.org.DBAdapter.insertTitle(DBAdapter.java:74)
06-14 08:43:00.912: E/AndroidRuntime(426):  at databas.test.org.DatabasActivity.onCreate(DatabasActivity.java:24)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 08:43:00.912: E/AndroidRuntime(426):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 08:43:00.912: E/AndroidRuntime(426):  ... 11 more
3
  • 3
    Can you post the stacktrace please? Otherwise the NullPointer could be anywhere. It will help us to narrow it down to the line that it's crashing at. Commented Jun 14, 2012 at 8:01
  • Sorry my logcat doesnt show anything, it crashes before it like even starts. The only thing that happens is that Eclipse shows a pop-up who says: "An internal error occurred during: "Launching Databas". java.lang.NullPointerException" Commented Jun 14, 2012 at 8:18
  • Now I have posted the output from my logcat ^ Commented Jun 14, 2012 at 8:49

2 Answers 2

2

The only potential NPE I see there is while not calling open() in DbAdapter

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was the problem.
0

I think in

create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null)

">" is not valid there........

1 Comment

Updated with the class that i use DBAdapter class now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.