This question already has an answer here:
I'm creating a sports team android application for a University project and I am having a little trouble with my database. I am trying to add a fixture to the database but I am getting the following nullpointer exception when I click on the 'Add' button.
12-27 16:40:29.420 6977-6977/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.myacer.clubhub.Manager.UpdateSchedule$1.onClick(UpdateSchedule.java:54)
at android.view.View.performClick(View.java:4262)
at android.view.View$PerformClick.run(View.java:17421)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4944)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
The error is at line 54 in the UpdateSchedule class. Line 54 is the
dbHelper.insertFixture
Here is the rest of my UpdateSchedule class where the manager of the team can add a fixture
public class UpdateSchedule extends AppCompatActivity {
DBHelper dbHelper;
EditText match_date;
EditText match_time;
EditText match_opponent;
EditText match_venue;
Button btnAdd;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updateschedule);
match_date = (EditText) findViewById(R.id.fixtureDateEdit);
match_time = (EditText) findViewById(R.id.fixtureTimeEdit);
match_opponent = (EditText) findViewById(R.id.fixtureOpponentEdit);
match_venue = (EditText) findViewById(R.id.fixtureVenueEdit);
btnAdd = (Button) findViewById(R.id.addFixtureButton);
addFixture();
}
public void addFixture() {
btnAdd.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.insertFixture(
match_date.getText().toString(),
match_time.getText().toString(),
match_opponent.getText().toString(),
match_venue.getText().toString());
}
}
);
}
}
This is my DBHelper class.
public class DBHelper extends SQLiteOpenHelper {
private static final String LOG = "DatabaseHelper";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "clubhub.db";
private static final String TABLE_FIXTURES = "fixtures";
private static final String TABLE_DIARY = "diary";
private static final String TABLE_NOTIFICATION = "notification";
private static final String TABLE_USERS = "users";
//common column names
private static final String KEY_ID = "id";
private static final String PLAYER_NAME = "player_name";
//fixtures table columns
private static final String MATCH_DATE = "match_date";
private static final String MATCH_TIME = "match_time";
private static final String MATCH_OPPONENT = "match_opponent";
private static final String MATCH_VENUE = "match_venue";
//diary table columns
private static final String SLEEP_LENGTH = "sleep_length";
private static final String SLEEP_QUALITY = "sleep_quality";
private static final String ENERGY = "energy";
private static final String MOOD = "mood";
private static final String APPETITE = "appetite";
private static final String WATER_INTAKE = "water_intake";
private static final String SORENESS = "soreness";
private static final String WORKOUT_TYPE = "workout_type";
private static final String WORKOUT_LENGTH = "workout_length";
private static final String WORKOUT_RPE = "workout_rpe";
//notification table columns
private static final String MESSAGE = "message";
//user table columns
private static final String PASSWORD = "password";
//table create statements
//table fixtures create
private static final String CREATE_TABLE_FIXTURES = "CREATE TABLE " + TABLE_FIXTURES
+ "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ MATCH_DATE + " DATETIME,"
+ MATCH_TIME + " DATETIME NOT NULL,"
+ MATCH_OPPONENT + " TEXT,"
+ MATCH_VENUE + " TEXT"
+ ")";
//table diary create
private static final String CREATE_TABLE_DIARY = "CREATE TABLE " + TABLE_DIARY
+ "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ SLEEP_LENGTH + " INTEGER NOT NULL,"
+ SLEEP_QUALITY + " INTEGER NOT NULL,"
+ ENERGY + " INTEGER NOT NULL,"
+ MOOD + " INTEGER NOT NULL,"
+ APPETITE + " INTEGER NOT NULL,"
+ WATER_INTAKE + " INTEGER NOT NULL,"
+ SORENESS + " INTEGER NOT NULL,"
+ WORKOUT_TYPE + " TEXT NOT NULL,"
+ WORKOUT_LENGTH + " DATETIME NOT NULL,"
+ WORKOUT_RPE + " INTEGER NOT NULL"
+ ")";
//table notification create
private static final String CREATE_TABLE_NOTIFICATION = "CREATE TABLE " + TABLE_NOTIFICATION
+ "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ MESSAGE + " TEXT NOT NULL"
+ ")";
/**
* constructor with args
*/
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_FIXTURES);
db.execSQL(CREATE_TABLE_DIARY);
db.execSQL(CREATE_TABLE_NOTIFICATION);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FIXTURES);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIARY);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATION);
onCreate(db);
}
public boolean insertFixture(String date, String time, String opponent, String venue){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MATCH_DATE, date);
values.put(MATCH_TIME, time);
values.put(MATCH_OPPONENT, opponent);
values.put(MATCH_VENUE, venue);
long result = sqLiteDatabase.insert(TABLE_FIXTURES, null, values);
if (result == -1) {
return false;
} else {
return true;
}
}
}
The insert method looks like
public boolean insertFixture(String date, String time, String opponent, String venue){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MATCH_DATE, date);
values.put(MATCH_TIME, time);
values.put(MATCH_OPPONENT, opponent);
values.put(MATCH_VENUE, venue);
long result = sqLiteDatabase.insert(TABLE_FIXTURES, null, values);
if (result == -1) {
return false;
} else {
return true;
}
}
Any help would be greatly appreciated.
Thanks
Aucun commentaire:
Enregistrer un commentaire