0

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];
                        }
                    };
            }
MikeT
  • 51,415
  • 16
  • 49
  • 68
Preeti
  • 1
  • 2
  • 1
    Preeti welcome to Stack Overflow (SO). To enable people to provide help you need to provide more detail. You mention null value and also in the log. You should edit your question to include the relevant section of the log and the code where the issue occurs (the log typically includes a link to the code). – MikeT Apr 27 '21 at 19:56
  • @MikeT thank you for responding!! :") sorry i was actually uploading the code and trying to figure it out. i have edited my post – Preeti Apr 27 '21 at 20:07
  • Preeti what you should do is put a breakpoint on the line `photoQuestion.setText(currentQuestion.getPhoto());` (left click the line in the area to the left of the line a circle will appear). The run in debug when the code gets to the point you will be presented with a debug window just before the line. Check the values of **photoQuestion** and **currentQuestion** (I suspect that photoQuestion will be null as the findViewById(R.id.photoQuestiontest) has not found the id). – MikeT Apr 27 '21 at 20:19
  • @MikeT hi, thank you, so i did that currentQuestion shows null and photoQuestion is showing this com.google.android.material.textview.MaterialTextView{4aa990e V.ED..... ......ID 0,0-0,0 #7f080124 app:id/photoQuestiontest} – Preeti Apr 27 '21 at 20:26
  • Ok I believe the problem is in `getRelativesPhotoQuestion` in the DBhandler (so currentQuestion is null from the line `currentQuestion = DbHandler.getRelativesPhotoQuestion(RELATIVES_ID);`). 1) **A cursor will NEVER be null** when returned from an SQLiteDatabase method. so using `if(cursor!=null && cursor.getCount() > 0) ` will never be of use. Just do `moveToFirst`, if there is no row then false is returned (all the Cursor move???? methods return false if move cannot be done). more to come... – MikeT Apr 27 '21 at 20:48
  • If your query will only ever find 1 row (as in looking for id) then simply do `if (cursor.moveToFirst()) { .... }` (no need for loop). If looping though a number of rows `while (cursor.moveToNext()) { .... }` can be used. Anyway you need to cater for not finding the row (check for currentQuestion being null). You also probably need to look at why RELATIVES_ID is not as expected and therefore doesn't find a row. – MikeT Apr 27 '21 at 20:57
  • @MikeT okay i will try to apply what you have suggested, as for RELATIVES_ID, i actually am unsure too because i have intialised a starting value of 1 when it will be run in my onCreate method in my PeopleModeActivity class ---> RELATIVES_ID = intent.getIntExtra("RELATIVES_ID", 1); – Preeti Apr 27 '21 at 21:03
  • @MikeT the *RELATIVES_ID* is actually a variable that defines the number that will be passed through in my actual `SELECT method` that i do in `getRelativesPhotoQuestion method in the DbHandler`, where i pass the value into the method from the main activity and use it in the `"WHERE" clause.` the `currentQuestion, i have declared as private PeopleModeQuestions currentQuestion,` and actually in the original post, I have included the `PeopleModeQuestions class which somehow has merged with the Logcat error text.` I was wondering if you could see if the model class could be done wrongly? – Preeti Apr 27 '21 at 21:08
  • i just did a debug run (thank you for telling me about the way to do it) on the `RELATIVES_ID = intent.getEXTRA("RELATIVES_ID", 1)` which is where I do my initialisation of the variable as i mentioned previously and it says that it somehow is `0`? i think i have to check why that's the case then but if you have an idea, do let me know too :) – Preeti Apr 27 '21 at 21:13
  • Well done Preeti break points and debug are your best friend (was respond to say use BreakPoint debug you beat me to it). You need to check in the activity that passes the Intent. – MikeT Apr 27 '21 at 21:22
  • @MikeT the activity that passes the Intent as in the one that declares and initialises? the unfortunate thing is that it is in the same PeopleModeActivity class hahaha i have been at this for the past 12+ hours. I am trying to narrow down the issue. But I am not so sure about the issue on why it is not initialising. – Preeti Apr 27 '21 at 21:28
  • @MikeT So i went back to the method i had created in the DbHandler, the one called `getRelativesPhotoQuestion with object datatype of PeopleModeQuestions`. I noticed that I had done a `return null` for that method. that explains why I got a null returned, even tho my values were passing. However I am facing an issue where I am `not able to do a return to get the SELECT values i need` Referring to the post above: have I done my `PeopleModeQuestions class` wrongly? or am i missing something? – Preeti Apr 27 '21 at 22:26
  • Preeti the issue you have is that there is no row with and id of 0. So you you need to find out why you are getting 0 as the id. That will be passed from the activity that is starting the **PeopleModeActivity** activity. As a test for **PeopleModeActivity** set of the code above after the line `RELATIVES_ID = intent.getIntExtra("RELATIVES_ID", 1);` do `RELATIVES_ID = 1;` (assuming that you have a row with an id of 1 (if not an id that does exist)). – MikeT Apr 27 '21 at 23:01
  • Preeti, the line `RELATIVES_ID = intent.getIntExtra("RELATIVES_ID", 1);` is saying/doing get the value from the IntentExtras that has a key of RELATIVES_ID and only if there is no such key then set RELATIVES_ID to 1. As such the key is in the IntentExtras but the value stored is **0**. So the root of the problem as that 0 is being put into the Intent's extras in the invoking activity. When the query is executed no row is found and hence why currentQuestion is null as the moveToFirst returns false. – MikeT Apr 27 '21 at 23:12
  • @MikeT hmm okay i will try working on it thank you!! also could you help me with a new question I just posted! this is the link: (https://stackoverflow.com/q/67306905/15779151) – Preeti Apr 28 '21 at 20:23

0 Answers0