I am getting error when I run my app. I am supposed to get my photo data from the database i have created an even added the values. I am looked at every possible online source out there and really can't figure out why I am getting null value when it is saying there is no data in the parameter I am calling. it is my first time asking question here so don't mind me being unaware with the features. and also my first time working with sqlite and android studio. FYP deadline is coming soon and I am very stuck because of this :(
My error is on the line photoQuestion.setText(currentQuestion.getPhoto) It says that basically there is no data in that column of my database
PeopleModeActivity (Main Activity)
public class PeopleModeActivity extends AppCompatActivity {
private PeopleMode PEOPLEMODE;
private PeopleModeQuestions currentQuestion; // current question that is accessed
private int RELATIVES_ID; // current relatives ID that is accessed (Question_ID)
private SQLiteDbHandler DbHandler; // database handler
private ArrayList<PeopleModeAnswers> currentQuestion_Answers; // store list of answers to respective questions
private PeopleModeAnswers relationshipOption_1; // variable in reference to relationships option 1
private PeopleModeAnswers relationshipOption_2; // variable in reference to relationships option 2
private TextView photoQuestion; // UI for question (display photo)
private Button buttonOption1; // UI for relationships option 1
private Button buttonOption2; // UI for relationships option 2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_people_mode);
DbHandler = new SQLiteDbHandler(this);
// Obtain the Intent which contains extra values inserted in the previous Activity
// The values to retrieve and use: PEOPLEMODE, RELATIVES_ID
// If game mode with same Patient id is going on, the values (such as questions and answers for that patient id will carry forward)
Intent intent = getIntent();
// Game mode intialisation
PEOPLEMODE = intent.getParcelableExtra("PEOPLEMODE");
// current RELATIVES_ID initialisation
RELATIVES_ID = intent.getIntExtra("RELATIVES_ID", 1);
// To make sure that the game ends and goes to congratulations screen
// when all questions of the ongoing patient id finishes
// There are 4 questions (Relatives_ID) in the PEOPLE MODE, so 4 will be used as counter to know when to end
if (RELATIVES_ID > 4) {
Intent congratulations_screen = new Intent(PeopleModeActivity.this, EndOfQuestionsActivity.class);
congratulations_screen.putExtra("PEOPLEMODE", PEOPLEMODE);
startActivity(congratulations_screen);
}
// there are still more questions (Relatives_ID), people mode will continue and carry forward previous question data if any
// such as current Relatives_ID
else {
// current question initialisation (in reference to photo associated with current RelativesID)
// current questions based on current RelativesID will be
// stored in a PeopleModeQuestions object
currentQuestion = DbHandler.getRelativesPhotoQuestion(RELATIVES_ID);
// Retrieve the list that stores the answers related to the current question (Relatives_ID)
// List created in db will be called, getRelativesPhotoAnswersByRelativesID
currentQuestion_Answers = (ArrayList) DbHandler.getRelativesPhotoAnswersByRelativesID(RELATIVES_ID);
// Initialise all the relationships answers for the people mode
relationshipOption_1 = currentQuestion_Answers.get(0);
relationshipOption_1 = currentQuestion_Answers.get(1);
// Display the Photo (Question) in the screen
photoQuestion = (TextView) findViewById(R.id.photoQuestiontest);
photoQuestion.setText(currentQuestion.getPhoto());
// Display the relationships options (Answers) in the screen
buttonOption1 = findViewById(R.id.option1);
buttonOption1.setText(relationshipOption_1.getRelationship1());
buttonOption2 = findViewById(R.id.option2);
buttonOption2.setText(relationshipOption_1.getRelationship2());
OnOption1ClickButtonListener();
OnOption2ClickButtonListener();
}
}
// Click Listener action for each option selected
public void OnOption1ClickButtonListener() {
buttonOption1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (relationshipOption_1.getCorrectRelationship() == 1) { // If correct, click next to go to next qn if any
AlertDialog.Builder correct = new AlertDialog.Builder(PeopleModeActivity.this);
correct.setMessage("You got it correct! \n Good Job!").setNeutralButton("Next", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent peopleMode_intent = new Intent(PeopleModeActivity.this, PeopleModeActivity.class);
peopleMode_intent.putExtra("PEOPLEMODE", PEOPLEMODE);
peopleMode_intent.putExtra("RELATIVES_ID", (RELATIVES_ID + 1));
startActivity(peopleMode_intent);
overridePendingTransition(0, 0);
}
}).setCancelable(false);
AlertDialog alert_2 = correct.create();
correct.show();
}
if (relationshipOption_1.getCorrectRelationship() == 0) { // If wrong, same question will be repeated
AlertDialog.Builder wrong = new AlertDialog.Builder(PeopleModeActivity.this);
wrong.setMessage("No, not correct! \n It's okay, Try Again!").setNeutralButton("Do again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent peopleMode_intent = new Intent(PeopleModeActivity.this, PeopleModeActivity.class);
peopleMode_intent.putExtra("PEOPLEMODE", PEOPLEMODE);
peopleMode_intent.putExtra("RELATIVES_ID", RELATIVES_ID); // Relatives_ID (Question id) will stay the same since question will be repeated
startActivity(peopleMode_intent);
overridePendingTransition(0, 0);
}
}).setCancelable(false);
AlertDialog alert_2 = wrong.create();
wrong.show();
}
}
});
}
public void OnOption2ClickButtonListener() {
buttonOption2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (relationshipOption_2.getCorrectRelationship() == 1) { // If correct, click next to go to next qn if any
AlertDialog.Builder correct = new AlertDialog.Builder(PeopleModeActivity.this);
correct.setMessage("You got it correct! \n Good Job!").setNeutralButton("Next", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent peopleMode_intent = new Intent(PeopleModeActivity.this, PeopleModeActivity.class);
peopleMode_intent.putExtra("PEOPLEMODE", PEOPLEMODE);
peopleMode_intent.putExtra("RELATIVES_ID", (RELATIVES_ID + 1));
startActivity(peopleMode_intent);
overridePendingTransition(0, 0);
}
}).setCancelable(false);
AlertDialog alert_2 = correct.create();
correct.show();
}
if (relationshipOption_2.getCorrectRelationship() == 0) { // If wrong, same question will be repeated
AlertDialog.Builder wrong = new AlertDialog.Builder(PeopleModeActivity.this);
wrong.setMessage("No, not correct! \n It's okay, Try Again!").setNeutralButton("Do again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent peopleMode_intent = new Intent(PeopleModeActivity.this, PeopleModeActivity.class);
peopleMode_intent.putExtra("PEOPLEMODE", PEOPLEMODE);
peopleMode_intent.putExtra("RELATIVES_ID", RELATIVES_ID); // Relatives_ID (Question id) will stay the same since question will be repeated
startActivity(peopleMode_intent);
overridePendingTransition(0, 0);
}
}).setCancelable(false);
AlertDialog alert_2 = wrong.create();
wrong.show();
}
}
});
}
}
SQLiteDbHandler
public class SQLiteDbHandler extends SQLiteOpenHelper {
public static final int SQLITEDB_VERSION = 1;
public static final String SQLITEDB_NAME = "fyp.db";
// Patient Details
public static final String Patient_Details_Table = "PatientDetails";
public static final String Patient_Details_Column_Id = "Id";
public static final String Patient_Details_Column_Name = "Name";
// Patients' Relatives (Question for "People" Game Mode)
public static final String PatientDetails_Relatives_Table = "PatientDetails_Relatives";
public static final String PatientDetails_Relatives_Column_RelativesID = "RelativesID";
public static final String PatientDetails_Relatives_Column_PatientID = "PatientID";
public static final String PatientDetails_Relatives_Column_Name = "Name";
public static final String PatientDetails_Relatives_Column_Photo = "Photo";
// Patients' Relationships with Relatives (Answer for "People" Game Mode)
public static final String PatientDetails_Relatives_Relationships_Table = "PatientDetails_Relatives_Relationships";
public static final String PatientDetails_Relatives_Relationships_Column_RelationshipID = "RelationshipID";
public static final String PatientDetails_Relatives_Relationships_Column_RelativesID = "RelativesID";
public static final String PatientDetails_Relatives_Relationships_Column_Relationship = "Relationship";
public static final String PatientDetails_Relatives_Relationships_Column_CorrectRelationship = "CorrectRelationship";
public SQLiteDbHandler(Context context) {
super(context, SQLITEDB_NAME, null, SQLITEDB_VERSION);
}
// Populate database with data to support "People" Game Mode
@Override
public void onCreate (SQLiteDatabase db) {
// Create table in preetifyp.db database for Patient Details
String PATIENT_DETAILS = "CREATE TABLE " + Patient_Details_Table + "("
+ Patient_Details_Column_Id + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Patient_Details_Column_Name + " TEXT" + ")";
db.execSQL(PATIENT_DETAILS);
// Create table in preetifyp.db database for Patients' Relatives (Questions for "People" Game Mode)
String PATIENTDETAILS_RELATIVES = "CREATE TABLE " + PatientDetails_Relatives_Table + " ( "
+ PatientDetails_Relatives_Column_RelativesID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PatientDetails_Relatives_Column_PatientID + " INTEGER, "
+ PatientDetails_Relatives_Column_Name + " TEXT, "
+ PatientDetails_Relatives_Column_Photo + " TEXT, " // testing with TEXT to get the quiz running, change to INTEGER later
+ " FOREIGN KEY( " + PatientDetails_Relatives_Column_PatientID + " ) REFERENCES " + Patient_Details_Table
+ " ( " + Patient_Details_Column_Id + " ) )";
db.execSQL(PATIENTDETAILS_RELATIVES);
// Create table in preetifyp.db database for Patients' Relationships with Relatives (Answers for "People" Game Mode)
String PATIENTDETAILS_RELATIVES_RELATIONSHIPS = "CREATE TABLE " + PatientDetails_Relatives_Relationships_Table + " ( "
+ PatientDetails_Relatives_Relationships_Column_RelationshipID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PatientDetails_Relatives_Relationships_Column_RelativesID + " INTEGER, "
+ PatientDetails_Relatives_Relationships_Column_Relationship + " TEXT, "
+ PatientDetails_Relatives_Relationships_Column_CorrectRelationship + " INTEGER, " // Responses: 0 = no, 1 = yes
+ " FOREIGN KEY( " + PatientDetails_Relatives_Relationships_Column_RelativesID + " ) REFERENCES " + PatientDetails_Relatives_Table
+ " ( " + PatientDetails_Relatives_Column_RelativesID + " ) )";
db.execSQL(PATIENTDETAILS_RELATIVES_RELATIONSHIPS);
// Insert data for "People" Game Mode
// Insert Patient Details data
db.execSQL("INSERT INTO " + Patient_Details_Table + " VALUES (" + 1 + ", 'Sandra' )");
db.execSQL("INSERT INTO " + Patient_Details_Table + " VALUES (" + 2 + ", 'Tom' )");
// Insert Patients' Relatives data (Questions)
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Table + " VALUES (" + 1 + ", " + 1 + ", 'john' , 'photo of john' )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Table + " VALUES (" + 2 + ", " + 1 + ", 'susan' , 'photo of susan' )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Table + " VALUES (" + 3 + ", " + 1 + ", 'mary' , 'photo of mary' )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Table + " VALUES (" + 4 + ", " + 1 + ", 'sarah' , 'photo of sarah' )" );
// Insert Patients' Relatives' Relationships data (Answers)
// First Question's Answer - John
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 1 + ", " + 1 + ", 'Father' , + 1 )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 2 + ", " + 1 + ", 'Father' , + 0 )" );
// Second Question's Answer - Susan
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 3 + ", " + 2 + ", 'Father' , + 0 )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 4 + ", " + 2 + ", 'Mother' , + 1 )" );
// Third Question's Answer - Mary
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 5 + ", " + 3 + ", 'Sister' , + 1 )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 6 + ", " + 3 + ", 'Friend' , + 0 )" );
// Fourth Question's Answer - Sarah
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 7 + ", " + 4 + ", 'Sister' , + 0 )" );
db.execSQL("INSERT INTO " + PatientDetails_Relatives_Relationships_Table + " VALUES (" + 8 + ", " + 4 + ", 'Friend' , + 1 )" );
}
public void addPhotos(PeopleModeQuestions photo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(PatientDetails_Relatives_Column_Photo, photo.getPhoto()); // Photo path
// Inserting photo
db.insert(PatientDetails_Relatives_Table, null, values);
db.close();
}
// Return all patient names stored in database
public List<String> getAllPatientNames() {
List<String> patients = new ArrayList<String>();
// Select Patient Names data from table
String selectPatientNames = "SELECT " + Patient_Details_Column_Name + " FROM " + Patient_Details_Table;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectPatientNames, null);
// looping through all rows and adding to list
if(cursor!=null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
patients.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return patients;
}
public List<PeopleModeQuestions> getAllPeopleModeQuestions() {
List<PeopleModeQuestions> listOfRelativesPhotos = new ArrayList<PeopleModeQuestions>();
String selectAllRelativesPhotos = "SELECT " + PatientDetails_Relatives_Column_Photo + " FROM " + PatientDetails_Relatives_Table;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectAllRelativesPhotos, null);
// looping through all rows and adding relatives' photos (all questions) to List
if(cursor!=null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
PeopleModeQuestions qn = new PeopleModeQuestions();
qn.setPhoto(cursor.getString(cursor.getColumnIndex(PatientDetails_Relatives_Column_Photo)));
listOfRelativesPhotos.add(qn);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return listOfRelativesPhotos;
}
// get current relative's photo (current question)
public PeopleModeQuestions getRelativesPhotoQuestion(int Relatives_ID) {
// Select Relative's Photo data from table (questions)
String selectcurrentRelativesPhoto = "SELECT " + PatientDetails_Relatives_Column_Photo + " FROM " + PatientDetails_Relatives_Table
+ " WHERE " + PatientDetails_Relatives_Column_RelativesID + " = " + Relatives_ID;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectcurrentRelativesPhoto, null);
if(cursor!=null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
PeopleModeQuestions qn = new PeopleModeQuestions();
qn.setPhoto(cursor.getString(cursor.getColumnIndex(PatientDetails_Relatives_Column_Photo)));
//listOfRelativesPhotos.add(qn);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
//return selectcurrentRelativesPhoto;
return null;
}
// People Mode Answers - PatientDetails_Relatives_Relationships
public ArrayList <PeopleModeAnswers> getRelativesPhotoAnswersByRelativesID(int Relatives_ID) {
ArrayList<PeopleModeAnswers> relationships = new ArrayList<>();
relationships.clear(); // clear ArrayList to allow new relationships answer data to be added
// Select Relative's Relationship data from table (answers)
String selectRelativesPhotoAnswersByRelativesID = "SELECT " + PatientDetails_Relatives_Relationships_Column_Relationship + " FROM " + PatientDetails_Relatives_Relationships_Table
+ " WHERE " + PatientDetails_Relatives_Relationships_Column_RelativesID + " = " + Relatives_ID;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectRelativesPhotoAnswersByRelativesID, null);
// looping through all rows and adding relationships (answers) to ArrayList
if(cursor!=null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
PeopleModeAnswers answers = new PeopleModeAnswers();
answers.setRelationship1(cursor.getString(cursor.getColumnIndex(PatientDetails_Relatives_Relationships_Column_Relationship)));
answers.setRelationship2(cursor.getString(cursor.getColumnIndex(PatientDetails_Relatives_Relationships_Column_Relationship)));
relationships.add(answers);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return relationships;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Patient_Details_Table);
db.execSQL("DROP TABLE IF EXISTS " + PatientDetails_Relatives_Table);
db.execSQL("DROP TABLE IF EXISTS " + PatientDetails_Relatives_Relationships_Table);
// create tables again
onCreate(db);
}
}
@Override
public void onConfigure(SQLiteDatabase db) {
db.setForeignKeyConstraintsEnabled(true);
super.onConfigure(db);
}
}
2021-04-28 03:28:52.702 5617-5617/com.example.memorydementia E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.memorydementia, PID: 5617
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memorydementia/com.example.memorydementia.PeopleModeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.memorydementia.PeopleModeQuestions.getPhoto()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.memorydementia.PeopleModeQuestions.getPhoto()' on a null object reference
at com.example.memorydementia.PeopleModeActivity.onCreate(PeopleModeActivity.java:97)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
PeopleModeQuestions class
public class PeopleModeQuestions implements Parcelable{
// relativesID will store question
private int relativesID; // QuestionID
private int patientID; // QuizID
private String name; // Name of Relative
private String photo; // -> Question
public PeopleModeQuestions() {
}
public PeopleModeQuestions(int relativesID, int patientID, String name, String photo) {
this.relativesID = relativesID;
this.patientID = patientID;
this.name = name;
this.photo = photo;
}
public int getRelativesID() {
return relativesID;
}
public void setRelativesID(int relativesID) {
this.relativesID = relativesID;
}
public int getPatientID() {
return patientID;
}
public void setPatientID(int patientID) {
this.patientID = patientID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
protected PeopleModeQuestions(Parcel in) {
relativesID = in.readInt();
patientID = in.readInt();
name = in.readString();
photo = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(relativesID);
dest.writeInt(patientID);
dest.writeString(name);
dest.writeString(photo);
}
public static final Parcelable.Creator<PeopleModeQuestions> CREATOR = new Parcelable.Creator<PeopleModeQuestions>() {
public PeopleModeQuestions createFromParcel(Parcel in) {
return new PeopleModeQuestions(in);
}
public PeopleModeQuestions[] newArray(int size) {
return new PeopleModeQuestions[size];
}
};
}