views:

43

answers:

1
06-29 01:24:15.882: ERROR/AndroidRuntime(747): Uncaught handler: thread main exiting due to uncaught exception
06-29 01:24:15.922: ERROR/AndroidRuntime(747): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.two/one.two.Arrival}: java.lang.NullPointerException
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.os.Looper.loop(Looper.java:123)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.ActivityThread.main(ActivityThread.java:3948)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at java.lang.reflect.Method.invoke(Method.java:521)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at dalvik.system.NativeStart.main(Native Method)
06-29 01:24:15.922: ERROR/AndroidRuntime(747): Caused by: java.lang.NullPointerException
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at one.two.DBAdapter.getAllTitles(DBAdapter.java:167)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at one.two.Arrival.getData(Arrival.java:35)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at one.two.Arrival.onCreate(Arrival.java:21)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
06-29 01:24:15.922: ERROR/AndroidRuntime(747):     ... 11 more

My Class to call the database

package one.two;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Arrival extends ListActivity
{
    private ListView listView;
    DBAdapter db = 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);
        // db.close();
    }

    private void getData()
    {
        // show some display that you are going to open db
        db.open();
        // 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);

        db.close();
    }

}

My DBAdapter.java

package one.two;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;

public class DBAdapter extends ListActivity
{
    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;

    public String status = "status";
    public String id = "id";
    public String arrival = "arrival";
    public String destination = "destination";
    public String ferry = "ferry";

    //null constructor
    public DBAdapter()
    {

    }

    //overloaded non-null constructor
    public DBAdapter(Context context)
    {

    }

    public class DatabaseHelper extends SQLiteOpenHelper
    {
        Context context;
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context = context;

        }//end constructor DatabaseHelper

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

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            boolean dbExist = checkDatabase();

            if (dbExist)
            {
            }
            else
            {
                this.getReadableDatabase();
                try
                {
                    copyDataBase();
                }
                catch (IOException e)
                {
                    throw new Error("Error copying database");
                }
            }
        }//end onCreate()
    }// end class DatabaseHelper

    private static DatabaseHelper DBHelper;
    private static SQLiteDatabase db;

    private static final int DATABASE_VERSION = 1;

    //// context brought up /////////////
    //private final Context context;


        private boolean checkDatabase()
        {
            SQLiteDatabase checkDB = null;
            try
            {
                String myPath = DB_PATH + DATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);
            }
            catch (SQLiteException e)
            {

                // database doesn't exist yet
            }
            if (checkDB != null)
            {

                checkDB.close();
            }
            return checkDB != null ? true : false;
        }//end checkDatabase()

        private void copyDataBase() throws IOException
        {

            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open(DATABASE_NAME);

            // Path to the just created empty db
            String outFileName = DB_PATH + DATABASE_NAME;

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0)
            {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

        }//end copyDataBase()

        public void DBAdapter() throws SQLException
        {
            String myPath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        }//end DBAdapter()
        public void open()
        {
            //Open the database
            String myPath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }
        // ---closes the database---
        public void close()
        {
            DBHelper.close();
        }//end close()

        public static List<String> getAllTitles()
        {

            List<String> titles = 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 titles;
        }//end getAllTitles()
    }//end class DBAdapter
+2  A: 

You never call open() on your object. You are using a static call in your code, but that static call depends on nonstatic methods being called to set up the static data members. So when you call db.query() db is null, hence the null pointer exception. Your overall architecture is fundamentally flawed. You need to instantiate your DBAdapter object and call several methods such as open from within your static method. However, inside your static method, you are relying on the context object being set. However, you don't set that until you call the constructor. You need to make sure that your references are set before attempting to operate on them. I would suggest not attempting to make a static call like this unless you want to pass it a Context object. From there you will need to instantiate the DBAdapter object and call the appropriate setup methods (like open) before you can interact with the database.

Edit

Based on rereading your code, change your database adapter class to remove the static method:

package one.two;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;

public class DBAdapter extends ListActivity
{
    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;

    public String status = "status";
    public String id = "id";
    public String arrival = "arrival";
    public String destination = "destination";
    public String ferry = "ferry";

    //null constructor
    public DBAdapter()
    {

    }

    //overloaded non-null constructor
    public DBAdapter(Context context)
    {

    }

    public class DatabaseHelper extends SQLiteOpenHelper
    {
        Context context;
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context = context;

        }//end constructor DatabaseHelper

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

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            boolean dbExist = checkDatabase();

            if (dbExist)
            {
            }
            else
            {
                this.getReadableDatabase();
                try
                {
                    copyDataBase();
                }
                catch (IOException e)
                {
                    throw new Error("Error copying database");
                }
            }
        }//end onCreate()
    }// end class DatabaseHelper

    private static DatabaseHelper DBHelper;
    private static SQLiteDatabase db;

    private static final int DATABASE_VERSION = 1;

    //// context brought up /////////////
    //private final Context context;


        private boolean checkDatabase()
        {
            SQLiteDatabase checkDB = null;
            try
            {
                String myPath = DB_PATH + DATABASE_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);
            }
            catch (SQLiteException e)
            {

                // database doesn't exist yet
            }
            if (checkDB != null)
            {

                checkDB.close();
            }
            return checkDB != null ? true : false;
        }//end checkDatabase()

        private void copyDataBase() throws IOException
        {

            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open(DATABASE_NAME);

            // Path to the just created empty db
            String outFileName = DB_PATH + DATABASE_NAME;

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(outFileName);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0)
            {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();

        }//end copyDataBase()

        public void DBAdapter() throws SQLException
        {
            String myPath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READONLY);
        }//end DBAdapter()
        public void open()
        {
            //Open the database
            String myPath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }
        // ---closes the database---
        public void close()
        {
            DBHelper.close();
        }//end close()

        public List<String> getAllTitles()
        {

            List<String> titles = 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 titles;
        }//end getAllTitles()
    }//end class DBAdapter

And change your Arrival class to use the nonstatic method:

package one.two;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Arrival extends ListActivity
{
    private ListView listView;
    DBAdapter db = 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);
        // db.close();
    }

    private void getData()
    {
        // show some display that you are going to open db
        db.open();
        // 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 = db.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);

        db.close();
    }

}
Chris Thompson
Isn't this statement opening the database? String myPath = DB_PATH + DATABASE_NAME; db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
User358218
Yes it does, but you call that after you use the database... you need to put the call to instantiate the DBAdapter object before you attempt to use the db object. Also, you still need to pass this method a context object.
Chris Thompson
Sorry i don't really get what do you mean by using the database.
User358218
Your call to db.query is using the database. Until you call openDatabase, your db object is null
Chris Thompson
Sorry but where do i call the openDatabase() method? Im new to android
User358218
You're fine calling it where you're calling it, you just have to make sure you instantiate the DBAdapter object before you call db.query
Chris Thompson
Do i have to instantiate the public DBAdapter() constructor or the public DBAdapter(Context context) constructor?
User358218
`DBAdapter(Context context)` and you must pass a context object to the static method
Chris Thompson
So it would be something like Context mycontext = new Context("context"); ?
User358218
No, your activity, the class that implements "onCreate" is a context (your arrival class). You cannot create a context object
Chris Thompson
Context mycontext = new Arrival(); ?
User358218
I'm going to say it. I know others are going to think it. jjteo: you need to a) learn how to program, then b) learn how to program for android, before asking any more questions. I'm sorry, but the reason why you're not understanding is because you do not have the required knowledge to understand the answers and to ask the appropriate questions.
Qberticus
@Qberticus I agree with you, I was trying to figure out when I should bail...I clearly gave in and did it for him although I don't think this will be the last of the problems
Chris Thompson