This question already has an answer here:
I've built a program that allows the user to log in using an SQLite database, after the user logs in I have a few methods to pull information from DB.java and gets the current date and displays accordingly.
However the program crashes after log in, the logcat is posted at the bottom
First method is getModuleTime() - This is in the DB.java class and is called in MainActivity in the getTodaysModules() method shown below.
public List<tableModules> getModuleTime(String module1, String module2, String module3, String day) {
List<tableModules> moduleList = new ArrayList<tableModules>();
// Select All Query
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT modulename, modulelecturetime, moduleseminartime FROM " + Table2 + " WHERE (modulename=? OR modulename=? OR modulename=?) AND (modulelecturedate=? OR moduleseminardate=?)", new String[]{module1, module2, module3, day, day});
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
tableModules module = new tableModules();
module.modulename = cursor.getString(0);
module.modulelecturedate = cursor.getString(1);
module.moduleseminardate = cursor.getString(2);
moduleList.add(module);
} while (cursor.moveToNext());
}
else
{
Log.d("Cant", "Find any lectures today");
}
// return contact list
return moduleList;
}
getTodaysModules()
public void getTodaysModules(String sessionName)
{
List<tableStudents> studentModules = db.getStudentsModules(sessionName);
if (!studentModules.isEmpty())
{
for (tableStudents session: studentModules)
{
//Save modules to string
String module1 = session.modules.toString();
String module2 = session.modules1.toString();
String module3 = session.modules2.toString();
String today = getCurrentDay().toString();
Toast.makeText(MainActivity.this, "Today is: " + today, Toast.LENGTH_LONG).show();
TextView event1 = (TextView)findViewById(R.id.event1);
event1.setText(today + "'s Events:");
List<tableModules> moduleDayList = db.getModuleTime(module1, module2, module3, today);
for (tableModules module: moduleDayList) {
if(!module.modulelecturedate.isEmpty())
{
event1.setText("Your Lecture for" + module.modulename.toString() + "is at" + module.modulelecturetime.toString());
}
if (!module.moduleseminardate.isEmpty())
{
event1.setText("Your Seminar for" + module.modulename.toString() + "is at" + module.moduleseminartime.toString());
}
else
{
event1.setText("You have no seminars/lectures today");
}
}
}
}
else
{
Toast.makeText(MainActivity.this, "N", Toast.LENGTH_LONG).show();
}
}
getTodaysModules() is then called in the onResume() so that after the user logs in their details are displayed:
onResume()
protected void onResume() {
super.onResume();
final Intent intent = getIntent();
//If statement triggered when user logs in only.
if (loginSuccess == 0)
{
if (intent.hasExtra("loginSuccess")) {
loginSuccess++;
//Store session name
boolean loginSuccess = intent.getBooleanExtra("loginSuccess", true);
String sessionName = intent.getStringExtra("sessionName");
Toast.makeText(MainActivity.this, "Welcome " + sessionName.toString(), Toast.LENGTH_LONG).show();
TextView loggedinas = (TextView)findViewById(R.id.loggedinas);
loggedinas.setText(sessionName);
//User has logged in, changing login button to logout button.
Button login=(Button)findViewById(R.id.log);
Button logout=(Button)findViewById(R.id.logout);
login.setVisibility(View.GONE);
logout.setVisibility(View.VISIBLE);
//Set today's events
getTodaysModules(sessionName);
}
}
}
LogCat
04-05 10:35:16.473: E/AndroidRuntime(1345): FATAL EXCEPTION: main
04-05 10:35:16.473: E/AndroidRuntime(1345): Process: com.example.project, PID: 1345
04-05 10:35:16.473: E/AndroidRuntime(1345): java.lang.RuntimeException: Unable to resume activity {com.example.project/com.example.project.MainActivity}: java.lang.NullPointerException
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.os.Handler.dispatchMessage(Handler.java:102)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.os.Looper.loop(Looper.java:136)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread.main(ActivityThread.java:5001)
04-05 10:35:16.473: E/AndroidRuntime(1345): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 10:35:16.473: E/AndroidRuntime(1345): at java.lang.reflect.Method.invoke(Method.java:515)
04-05 10:35:16.473: E/AndroidRuntime(1345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-05 10:35:16.473: E/AndroidRuntime(1345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-05 10:35:16.473: E/AndroidRuntime(1345): at dalvik.system.NativeStart.main(Native Method)
04-05 10:35:16.473: E/AndroidRuntime(1345): Caused by: java.lang.NullPointerException
04-05 10:35:16.473: E/AndroidRuntime(1345): at com.example.project.DB.getStudentsModules(DB.java:191)
04-05 10:35:16.473: E/AndroidRuntime(1345): at com.example.project.MainActivity.getTodaysModules(MainActivity.java:108)
04-05 10:35:16.473: E/AndroidRuntime(1345): at com.example.project.MainActivity.onResume(MainActivity.java:100)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.Activity.performResume(Activity.java:5310)
04-05 10:35:16.473: E/AndroidRuntime(1345): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
04-05 10:35:16.473: E/AndroidRuntime(1345): ... 12 more
ADDED:
getStudentsModules()
public List<tableStudents> getStudentsModules(String username) {
List<tableStudents> studentModules = new ArrayList<tableStudents>();
// Select All Query depending on who is logged in.
String selectQuery = "SELECT " + Modules1 + ", " + Modules2 + ", " + Modules + " FROM " + Table + " WHERE " + CNumber + " = \"" + username + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
tableStudents student = new tableStudents();
if (!student.modules.isEmpty() || !student.modules1.isEmpty() || !student.modules2.isEmpty())
{
student.modules = cursor.getString(1);
student.modules1 = cursor.getString(2);
student.modules2 = cursor.getString(3);
studentModules.add(student);
}
else
{
Log.d("Unable", "To Find Any Modules");
}
} while (cursor.moveToNext());
}
// return contact list
return studentModules;
}
Aucun commentaire:
Enregistrer un commentaire