tags:

views:

73

answers:

3

Hello guys, please have a look at this code and see if you could help me out. viewItem is populating from a database and the method fillRemindersData(long rowId) is only called from the reminder class. I don't know why i am getting a nullPointerException when calling the viewitems.fillRemindersData()method. if i comment the line, the code works fine and the rowId is correct. what could be the reason? thanks

// this is a reminder class
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_list);
        adapter = new DBAdapter(this);
        viewItems = new ViewItems();
        fillReminder();     
    }

    private void fillReminder() {
        Bundle extra = getIntent().getExtras();
        if(extra != null){
            rowId = extra.getLong(DBAdapter.KEY_ID);


            message = extra.getString(NewItem.Test);
        }
        Toast.makeText(this, message + "its ok here" + rowId, Toast.LENGTH_LONG).show();
        viewItems.fillRemindersData(rowId); // having a null pointer exception here.
}

And important part of the viewItems class is:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_list);
        adapter = new DBAdapter(this);
        adapter.open();
        fillData();
        list = (ListView)findViewById(android.R.id.list);
        list.setOnItemClickListener(this);


    }

 protected void fillData() {

 Cursor c = adapter.retrieveItems(); // method in sqlitedatabase
 startManagingCursor(c);

String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};
int[] to = new int[]{R.id.viewNameId, R.id.viewDateId, R.id.viewTimeId};

customCursorAdapter items = new customCursorAdapter(this, R.layout.view_items, c, from, to);
                setListAdapter(items);          

    }

 protected void fillRemindersData(long Id) {
 long row = Id;
 Cursor c = adapter.retrieveRow(row); // method in sqlitedatabase
 startManagingCursor(c);

String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};

int[] to = new int[]{R.id.RemindNameId, R.id.remindDateId, R.id.remindTimeId};

customCursorAdapter items = new customCursorAdapter(this, R.layout.remind_viewer, c, from, to);
setListAdapter(items);          

    }

method the logcat is referring to:

public Cursor retrieveRow(long rowId){
String[] resultColumns = new String[] {NAME,START_DATE,START_TIME};
Cursor row = db.query(true,DATABASE_TABLE, resultColumns, KEY_ID +"=" +rowId, null, null, null, null,null);
     if(row != null){
    row.moveToNext();
          return row;
    }
    return row;
     }

Logcat output:

09-22 15:19:00.346: ERROR/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception
09-22 15:19:00.386: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MemoBuzz/com.MemoBuzz.RemindViewer}: java.lang.NullPointerException  


    09-22 15:19:00.386: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at com.MemoBuzz.ViewItems.fillRemindersData(ViewItems.java:57)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at com.MemoBuzz.RemindViewer.fillReminder(RemindViewer.java:51)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at com.MemoBuzz.RemindViewer.onCreate(RemindViewer.java:36)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)

    09-22 15:19:00.386: ERROR/AndroidRuntime(808):     ... 11 more
A: 

Are you trying to start an activity with viewItems = new ViewItems();? Is ViewItems an activity class? You have an onCreate() in it. You need to start an activity with an intent, not with new

Falmarri
no actuall, i was trying to instantiate it, so i could call the method fillRemindersData.
manuelJ
yes, viewitems extends a listActivity.
manuelJ
You shouldn't ever call `new` on a class that extends activity. If you need a class object, make a new class, don't try to reuse Activities
Falmarri
A: 

It looks like onCreate() method of ViewItems is never invoked in your case. Hence adapter is null and you get the exception. Probably you have to put database object out of ViewItems class since right now it looks like an activity or service (and if that is the case invoking constructor on it, like you do, is definitely bad idea).

Konstantin Burov
hmm.. not too sure what you mean there. Because the ViewItems class is a listActivity that is already populated from the database Object and a Cursor Adapter. i just wanted to populate another list view from the fillRemindersData() method using the rowId i had gotten from the ViewItems class in the first Place. (the rowId passed through different activities to be back here.)
manuelJ
as i say, i need the database objects in other to call methods from them in my class.
manuelJ
or should i just Duplicate the ViewItems class and edit it to be called only for the fillRemindersData() method. but that doesn't sound right?
manuelJ
Don't duplicate, move common logic to some place where it will be accessible for all the code that needs the logic. For example you can consider moving the database staff to the Application class or Service or Content provider, you can end up even with Singletone object. There is bunch of ways.
Konstantin Burov
Thanks will look to using one of those and let you guys know how it turns out.
manuelJ
hey guys, just wanted to find out. is it possible to create a listview from a row id using a custom cursor adapter?
manuelJ
A: 

Looks to me like you didn't add your activity to your manifest.

schwiz
yes, i did that.
manuelJ