0

I'm getting NullpointerException. the follwing are my classes:

public class showrecordsBetweenDates extends Activity {


SQLiteDatabase db;
DBAdapter dbh;
  .........
  .........
  .........
public Cursor getMeterRecords(){

    dbh = new DBAdapter(showrecordsBetweenDates.this);
 db = dbh.getWritableDatabase();

            Cursor c;
            String[] All_Columns = new String[] { DBAdapter.COLUMN_METER_ID, DBAdapter.COLUMN_METER_DATE, DBAdapter.COLUMN_METER_START, DBAdapter.COLUMN_METER_END };
            c = db.query(DBAdapter.TABLE_METER, All_Columns, null, null, null, null, null);
            if (c != null) {
                c.moveToNext();
            }
            return c;
        }

My adapter class is

public class MeterAdapter extends Activity {

SimpleCursorAdapter mycursor;
showrecordsBetweenDates showRecords;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_records);
    showRecords= new showrecordsBetweenDates();
    populateListview();
}

public void populateListview() {

    Cursor cursor;
    cursor = showRecords.getMeterRecords();
    startManagingCursor(cursor);
    String[] fromfields = new String[] { 
            DBAdapter.COLUMN_METER_DATE,
            DBAdapter.COLUMN_METER_START, 
            DBAdapter.COLUMN_METER_END,
            DBAdapter.COLUMN_METER_CAR_ID };
    int[] tofields = new int[] { 
            R.id.meterdate, 
            R.id.meterstart,
            R.id.meterend,
            R.id.fk};
    mycursor = new SimpleCursorAdapter(this, R.layout.row_meter, cursor,
            fromfields, tofields, 0);
    ListView mylist = (ListView) findViewById(R.id.list);

here is my error

02-20 03:31:12.823: E/AndroidRuntime(31264):    at dalvik.system.NativeStart.main(Native Method)
02-20 03:31:12.823: E/AndroidRuntime(31264): Caused by: java.lang.NullPointerException
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at com.munawwar.sultan.showrecordsdatewise.showrecordsBetweenDates.getMeterRecords(showrecordsBetweenDates.java:183)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at com.munawwar.sultan.cursoradapter.MeterAdapter.populateListview(MeterAdapter.java:39)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at com.munawwar.sultan.cursoradapter.MeterAdapter.onCreate(MeterAdapter.java:31)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at android.app.Activity.performCreate(Activity.java:5020)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
 02-20 03:31:12.823: E/AndroidRuntime(31264):   ... 11 more

I populate listview from a method in DBAdapter class but now i need that method in another class(showrecordsBetweenDates) and i'm getting that error Thanks in Advance

Asif Khan
  • 59
  • 5
  • Post your DBAdapter. Which line exactly is throwing the error? – tachyonflux Feb 19 '15 at 23:26
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Feb 19 '15 at 23:27
  • You should follow the Camel Case naming convention when naming your Java classes. Also showrecordsBetweenDates shouldn't extend from Activity if its only funcionality will be to retrieve data from a database, aside from that would you specify the line that is throwing the exception? It's been marked that the `getMeterRecords` method is where the error is being thrown, I'm just guessing but probably your database hasn't been created or the location to it is incorrect. – Uriel Arvizu Feb 19 '15 at 23:28

0 Answers0