Help with Android Database Code

Put your problem here if it does not fit any of the other categories.

Help with Android Database Code

Postby jjteo » Tue Jun 29, 2010 8:57 am

Hi, i need help with my, everytime i run it there will be an error in the log as nulljavapointerexception error.

Code: Select all
package one.two;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;

public class DBAdapter extends ListActivity
   public String status = "status";
   public String id = "id";
   public String arrival = "arrival";
   public String destination = "destination";
   public String ferry = "ferry";
   private static String DB_PATH = "/data/data/one.two/databases/";
   private static final String DATABASE_NAME = "ferry.db";
   private static final String DATABASE_TABLE = "port";
   public static Context context;

   private static SQLiteDatabase db;
   //overloaded non-null constructor
   public DBAdapter(Context context)
      db = context.openOrCreateDatabase(DATABASE_NAME, 1, null);

   public class DatabaseHelper extends SQLiteOpenHelper
      Context context;
      DatabaseHelper(Context context)
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
         this.context = context;
      }//end constructor DatabaseHelper
      public void open()
          //Open the database
           String myPath = DB_PATH + DATABASE_NAME + DATABASE_TABLE;
          db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
      // ---closes the database---
      public void close()
      }//end close()

      public void onUpgrade(SQLiteDatabase db, int oldVersion,
            int newVersion)
      }//end onUpgrade()

      public void onCreate(SQLiteDatabase db)
      }//end onCreate()
   }// end class DatabaseHelper
   private static DatabaseHelper DBHelper;
   //private static SQLiteDatabase db;

   private static final int DATABASE_VERSION = 1;

   //// context brought up /////////////

      public static List<String> getAllTitles()

         List<String> port = new ArrayList<String>();

            Cursor c=null;
            c = db.query("port",
                  new String[] { "status", "id", "arrival",
                        "destination", "ferry" }, null, null,
                  null, null, null);
            try {
                if (c!=null) {
                    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
                       DBAdapter title = new DBAdapter(context);
                     title.status = c.getString(0);
            = c.getString(1);
                     title.arrival = c.getString(2);
                     title.destination = c.getString(3);
                     title.ferry = c.getString(4);
             } finally {
                 if (c!=null) {
         return port;
      }//end getAllTitles()
   }//end class DBAdapter

My class
Code: Select all
package one.two;

import java.util.List;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Arrival extends ListActivity
   private ListView listView;
   DBAdapter dbArrival = new DBAdapter(this);

   /** Called when the activity is first created. */
   public void onCreate(Bundle savedInstanceState)
      listView = (ListView) findViewById(;
      SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,, null, null, null);
      // db.close();

   private void getData()
      // show some display that you are going to open db
      // show some display that you have opened

      // redundant see below statement ---- List<String> items = new ArrayList<String>();
      // call the DBAdapter method to getAllTitles()
      List<String> items = DBAdapter.getAllTitles();
      // iterate through the items one by one thru display statement or show
      // on layout
      // check how to do that

      ArrayAdapter<String> titles = new ArrayAdapter<String>(this,
            R.layout.main, items);



Thank you.
Posts: 8
Joined: Tue Jun 01, 2010 3:38 am


Re: Help with Android Database Code

Postby anarche » Wed Jun 30, 2010 3:17 am

Post logcat please. Specifically, where is the null pointer?
Master Developer
Master Developer
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests