NullPointerException, can't see why??


Last Updated:

  1. motoko

    motoko Member

    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. hat could be the reason? thanks

    Code (Text):
    1. // this is a reminder class
    2. public void onCreate(Bundle savedInstanceState) {
    3.         super.onCreate(savedInstanceState);
    4.         setContentView(R.layout.view_list);
    5.         adapter = new DBAdapter(this);
    6.         viewItems = new ViewItems();
    7.         fillReminder();    
    8.     }
    9.    
    10.     private void fillReminder() {
    11.         Bundle extra = getIntent().getExtras();
    12.         if(extra != null){
    13.             rowId = extra.getLong(DBAdapter.KEY_ID);
    14.            
    15.            
    16.             message = extra.getString(NewItem.Test);
    17.         }
    18.         Toast.makeText(this, message + "its ok here" + rowId, Toast.LENGTH_LONG).show();
    19.         viewItems.fillRemindersData(rowId); // having a null pointer exception here.
    20. }
    And important part of the viewItems class is:

    Code (Text):
    1. public void onCreate(Bundle savedInstanceState) {
    2.         super.onCreate(savedInstanceState);
    3.         setContentView(R.layout.view_list);
    4.         adapter = new DBAdapter(this);
    5.         adapter.open();
    6.         fillData();
    7.         list = (ListView)findViewById(android.R.id.list);
    8.         list.setOnItemClickListener(this);
    9.        
    10.        
    11.     }
    12.    
    13.    
    14.     protected void fillData() {
    15.        
    16.     Cursor c = adapter.retrieveItems(); // method in sqlitedatabase
    17.     startManagingCursor(c);
    18.        
    19. String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};
    20. int[] to = new int[]{R.id.viewNameId, R.id.viewDateId, R.id.viewTimeId};
    21.                
    22. customCursorAdapter items = new customCursorAdapter(this, R.layout.view_items, c, from, to);
    23.                 setListAdapter(items);         
    24.                
    25.     }
    26.    
    27.            protected void fillRemindersData(long Id) {
    28.            long row = Id;
    29.           Cursor c = adapter.retrieveRow(row); // method in sqlitedatabase
    30.            startManagingCursor(c);
    31.        
    32. String[] from = new String[]{DBAdapter.NAME, DBAdapter.START_DATE, DBAdapter.START_TIME};
    33.  
    34. int[] to = new int[]{R.id.RemindNameId, R.id.remindDateId, R.id.remindTimeId};
    35.                
    36. customCursorAdapter items = new customCursorAdapter(this, R.layout.remind_viewer, c, from, to);
    37. setListAdapter(items);         
    38.                
    39.     }

    Advertisement
  2. wabbitt1970

    wabbitt1970 Well-Known Member

    Have you tried moving the } bracket on your if to after the point where you populate your list

    Your code

    Code (Text):
    1.  
    2. [COLOR="DarkRed"]}[/COLOR]
    3. Toast.makeText(this, message + "its ok here" + rowId, Toast.LENGTH_LONG).show();
    4. viewItems.fillRemindersData(rowId); // having a null pointer exception here.
    Alternative

    Code (Text):
    1. Toast.makeText(this, message + "its ok here" + rowId, Toast.LENGTH_LONG).show();
    2. viewItems.fillRemindersData(rowId); // having a null pointer exception here.
    3. [COLOR="DarkRed"]}[/COLOR]
    I think if your Bundle is empty you will get a null pointer exception.

Share This Page