1

I'm baffled by what is happening to my simple code. It just doesn't go into another class file even though I checked that code flows. Below is my code in android:

public class MedF1 extends ListActivity {

DrugsDbAdapter drugsDbAdapter = new DrugsDbAdapter(this);
DrugsDbAdapter.myDbHelper mDbHelper = new DrugsDbAdapter.myDbHelper(this);

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



    try {

        mDbHelper.createDataBase();

        } catch (IOException ioe) {

        throw new Error("Unable to create database");

        }

        try {

        mDbHelper.openDataBase();

        }catch(SQLException sqle){

        throw sqle;

        }

        populateDrugList();

}

public void populateDrugList() {

    Cursor drugListCursor = drugsDbAdapter.getAllEntries();

    startManagingCursor (drugListCursor);

    String[] from = new String[] {DrugsDbAdapter.KEY_DRUG};

    int[] to = new int[] {R.id.text1};

    SimpleCursorAdapter drugs = new SimpleCursorAdapter(this, R.layout.drug_row, drugListCursor, from, to);

    setListAdapter(drugs);
}

The database adapter class is below , the code just doesn't go beyond druglistCursor of the populateDrugList() in the above code!

public class DrugsDbAdapter {

private static final String DATABASE_TABLE = "data";


//The index column name for use in where clauses
public static final String KEY_ID = "_id";

//The name and column index of each column in DB
public static final String KEY_DRUG = "drug";
public static final String KEY_CONTENT = "content";
public static final String KEY_INDICATION = "indication";
public static final String KEY_DOSAGE = "dosage";
public static final String KEY_SPECIALPRECAUTION = "specialprecaution";


//variable to hold the database instance
private static SQLiteDatabase db;
//Context of the application using the database
private final Context context;
//Database open/upgrade helper
private myDbHelper dbHelper;

public DrugsDbAdapter(Context _context) {

    this.context = _context;

}

public void open() throws SQLiteException {
    try {
        db = dbHelper.getWritableDatabase();
    } catch (SQLiteException ex) {
        db = dbHelper.getReadableDatabase();
    }
}

//Creation of database and basic parameters
public static class myDbHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "data/data/com.paad.MedF1/databases/";

    private static String DB_NAME = "data.sqlite";

    private SQLiteDatabase myDataBase;

    private Context myContext;

    public  myDbHelper (Context context) {

        super(context, DB_NAME, null, 1);

        this.myContext = context;

    }

    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist) {
            // do nothing
        } else {

            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error ("Error copying database");

            }

        }
    }

    private boolean checkDataBase(){


        boolean checkDB = false;

        try{
            String myPath = DB_PATH + DB_NAME;
            File dbfile = new File(myPath);

            checkDB = dbfile.exists();

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        return checkDB;

    }


    private void copyDataBase() throws IOException{

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

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_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();

    }

    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

            if(myDataBase != null)
                myDataBase.close();

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

}


public void close() {
    db.close();
}

public long insertEntry(myDrug _myDrug) {

    ContentValues newDrugValues = new ContentValues();

    newDrugValues.put(KEY_DRUG, _myDrug.getDrug());
    newDrugValues.put(KEY_CONTENT, _myDrug.getContent());
    newDrugValues.put(KEY_INDICATION, _myDrug.getIndication());
    newDrugValues.put(KEY_DOSAGE, _myDrug.getDosage());
    newDrugValues.put(KEY_SPECIALPRECAUTION, _myDrug.getSpecialprecaution());

    return db.insert(DATABASE_TABLE, null, newDrugValues);
}

public boolean removeEntry (long _rowIndex) {
    return db.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
}

public Cursor getAllEntries () { 
    return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_DRUG, KEY_CONTENT, KEY_INDICATION, KEY_DOSAGE, KEY_SPECIALPRECAUTION}, null, null, null, null, null);
}


}   

HElp !!! my logcat output is:

06-03 22:29:01.636: ERROR/AndroidRuntime(1424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.MedF1/com.paad.MedF1.MedF1}: java.lang.NullPointerException
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.os.Looper.loop(Looper.java:123)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at dalvik.system.NativeStart.main(Native Method)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424): Caused by: java.lang.NullPointerException
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.paad.MedF1.MedF1.populateDrugList(MedF1.java:51)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at com.paad.MedF1.MedF1.onCreate(MedF1.java:45)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-03 22:29:01.636: ERROR/AndroidRuntime(1424):     ... 11 more
2
  • have you included the activity in your manifest? Commented Jun 3, 2011 at 14:32
  • what is in between line 45 and 51 in your MedF1.java Commented Jun 3, 2011 at 14:42

3 Answers 3

1

Your drugsDbAdapter is null. You have only declared DrugsDbAdapter drugsDbAdapter;, but you need to instantiate it.

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

5 Comments

Hi Daniel thank you for your reply ! How may i instantiate it ? I'm pretty bad at this ... is there anywhere i can learn more about it ?
That's difficult to say before seeing the source code of DrugsDbAdapter. Try make a new instance in your onCreate method, drugsDbAdapter = new DrugsDbAdapter(). The constructor most probably needs the database helper, which is provided probably by the constructor parameters or another method. This can't be answered without seeing other code. But it should be obvious if you check the code.
Hi Daniel , I've edited and included the other half of the code and tried the instantiation at the start of the code above but still to no avail ... might there be any other potential problems ? Could it be a class problem ? I've instantiated it twice , might there be a problem there ?
@jamen Try starting with the "Binding to Data with AdapterView" reference at developer.android.com/guide/topics/ui/binding.html
@Joe , Thanks! You're saying there might be a problem in the display ? I will have a look at it later :) But right now I've been walking through the debugger and the whole code stops at getAllEntries of the populateDrugList which is weird because I've checked the context "this" and it matches exactly that of a working code ..
0

Have you initialised drugsDbAdapter? I can't see the initialisation anywhere in your code.

1 Comment

THanks Mark! What is exactly an instantiation ? I'm really quite bad at this ... how can i instantiate the drugsDbAdapter ?
0

This looks like the error I get every time I have an error in my Activity's layout XML. Check to make sure the XML is well formatted and correct. Simplify it down to its simplest state and test it to make sure this isn't the problem.

Also, as the others stated, with the code given it doesn't appear as though you instantiated the db adapter.

Comments

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.