scarhand scarhand - 3 months ago 17
Android Question

nullpointerexception and i dont know why

I get the following error:

06-09 06:56:24.675: E/AndroidRuntime(1629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gauvion.simpleworkouttracker/com.myapp.MainActivity}: java.lang.NullPointerException


Logcat says its caused by:

06-09 06:56:24.675: E/AndroidRuntime(1629): Caused by: java.lang.NullPointerException
06-09 06:56:24.675: E/AndroidRuntime(1629): at com.myapp.MainActivity.onCreate(MainActivity.java:26)


Here is the code for MainActivity:

package com.myapp;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends ListActivity {
private DayDataSource datasource;
private SimpleCursorAdapter dataAdapter;
private Cursor cursor;
private boolean isEditing = false;
private Toast toast_deleted;
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
private int[] to;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cursor = datasource.fetchAllDays();
startManagingCursor(cursor);
to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
setListAdapter(dataAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}


Here's the code for DayDataSource:

package com.myapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DayDataSource {

private SQLiteDatabase database;
private MySQLiteHelper dbHelper;

public DayDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

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

public void createDay(String name, String day) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_DAY, day);
database.insert(MySQLiteHelper.TABLE_DAYS, null, values);
}

public void updateDay(long id, String name, String day) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_DAY, day);
database.update(MySQLiteHelper.TABLE_DAYS, values, MySQLiteHelper.COLUMN_ID + " = " + id, null);
}

public void deleteDay(long id) {
database.delete(MySQLiteHelper.TABLE_DAYS, MySQLiteHelper.COLUMN_ID + " = " + id, null);
}

public Cursor fetchAllDays() {
Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_ID + ", " + MySQLiteHelper.COLUMN_NAME + ", " + MySQLiteHelper.COLUMN_DAY + " " +
"from " + MySQLiteHelper.TABLE_DAYS + " " +
"order by case " + MySQLiteHelper.COLUMN_DAY + " " +
"when 'Monday' then 0 " +
"when 'Tuesday' then 1 " +
"when 'Wednesday' then 2 " +
"when 'Thursday' then 3 " +
"when 'Friday' then 4 " +
"when 'Saturday' then 5 " +
"when 'Sunday' then 6 " +
"when 'No Specific Day' then 7 " +
"end", null);

if (cursor != null) { cursor.moveToFirst(); }
return cursor;
}

}


Any help would be greatly appreciated.

Answer

you are attempting to use datasource without initialize it

cursor = datasource.fetchAllDays();

datasource is null in onCreate

Comments