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 DBAdapter.java, everytime i run it there will be an error in the log as nulljavapointerexception error.
My DBAdapter.java

Code: Select all
package one.two;

import java.util.List;

import android.app.ListActivity;
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()
      {
         DBHelper.close();
      }//end close()

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

      @Override
      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);
                     title.id = c.getString(1);
                     title.arrival = c.getString(2);
                     title.destination = c.getString(3);
                     title.ferry = c.getString(4);
                    }
                }
             } finally {
                 if (c!=null) {
                 c.close();
             }
            
      }
         return port;
      }//end getAllTitles()
   }//end class DBAdapter


My Arrival.java class
Code: Select all
package one.two;

import java.util.List;

import android.app.ListActivity;
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)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      getData();
      // db.open();
      listView = (ListView) findViewById(android.R.id.list);
      SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.id.list, null, null, null);
      this.setListAdapter(mAdapter);
      // 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.
jjteo
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 01, 2010 3:38 am

Top

Re: Help with Android Database Code

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

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

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 27 guests