ankur3000 0 Newbie Poster

I am pretty new in android application development and I am trying to get on of the rows from a SQLite db to show up as a Listactivity, but this is not working and the program terminates unexpectedly. Here is the code:

package ankur.test.app;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class testMenu extends ListActivity{

    HotOrNot Infor = new HotOrNot(this);
    {
        Infor.open();
    }
    String[] result = Infor.getList();
    {
        Infor.close();
    }


    @Override
    protected void onCreate(Bundle ankurtestlist) {
        // TODO Auto-generated method stub
        super.onCreate(ankurtestlist);
        setListAdapter(new ArrayAdapter<String>(testMenu.this, android.R.layout.simple_list_item_2, result));

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Object cheese = result[position];
    /*  try{
        Class ourClass = Class.forName("ankur.test.app."+ cheese);
        Intent ourIntent = new Intent(testMenu.this, ourClass);
        startActivity(ourIntent);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }*/
    }
}

and this is the method in another class:

public String[] getList() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        ArrayList name = new ArrayList(); 
        int i = 0;

        int iName = c.getColumnIndex(KEY_NAME);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            name.add(c.getString(iName));
            i++;
        }

        String[] result = (String[]) name.toArray();
        return result;
    }

Here are the logcat results:

02-06 14:53:10.383: D/AndroidRuntime(3963): Shutting down VM
02-06 14:53:10.383: W/dalvikvm(3963): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-06 14:53:10.403: E/AndroidRuntime(3963): FATAL EXCEPTION: main
02-06 14:53:10.403: E/AndroidRuntime(3963): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ankur.test.app/ankur.test.app.testMenu}: java.lang.NullPointerException
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.os.Looper.loop(Looper.java:123)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at java.lang.reflect.Method.invokeNative(Native Method)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at java.lang.reflect.Method.invoke(Method.java:521)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at dalvik.system.NativeStart.main(Native Method)
02-06 14:53:10.403: E/AndroidRuntime(3963): Caused by: java.lang.NullPointerException
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at ankur.test.app.HotOrNot.open(HotOrNot.java:62)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at ankur.test.app.testMenu.<init>(testMenu.java:14)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at java.lang.Class.newInstanceImpl(Native Method)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at java.lang.Class.newInstance(Class.java:1429)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-06 14:53:10.403: E/AndroidRuntime(3963):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
02-06 14:53:10.403: E/AndroidRuntime(3963):     ... 11 more

Here is the HotOrNot class:

package ankur.test.app;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class HotOrNot {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "persons_name";
    public static final String KEY_HOTNESS = "persons_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;


    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_NAME + " TEXT NOT NULL, " +
                    KEY_HOTNESS + " TEXT NOT NULL);"        
            );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);

        }

    }

    public HotOrNot(Context c){
        ourContext = c;
    }

    public HotOrNot open() throws SQLException{
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }
    public void close(){
        ourHelper.close();
    }

    public long createEntry(String name, String hotness) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_HOTNESS, hotness);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iHotness = c.getColumnIndex(KEY_HOTNESS);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iHotness) + "\n";
        }
        return result;
    }

    public String[] getList() {
        // TODO Auto-generated method stub
        String[] column = new String[]{KEY_NAME};
        Cursor d = ourDatabase.query(DATABASE_TABLE, column, null, null, null, null, null);
        ArrayList name = new ArrayList(); 

        int iName = d.getColumnIndex(KEY_NAME);

        for (d.moveToFirst(); !d.isAfterLast(); d.moveToNext()){
            name.add(d.getString(iName));
        }

        String[] result = (String[]) name.toArray();
        return result;
    }
}

Any help would be appreciated.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.