0

I'm confused with the objects and constructor in Java. I query from a SQLiteDatabase but I couldn't get the correct object/answer. I know my codes look messy and I need to clean it up but I don't know where to start...

public static class QObject {

        public String word;
        public String definition;

        public QObject(String word, String definition) {
            this.word = word;
            this.definition = definition;
        }

        public QObject getAnswer(String message) {

            QObject quizObject = null;
            String query = "select * from " + TABLE_NAME + " where " + COL_WORD + " = '" + message + "'";

            Cursor cursor = this.getDbConnection().rawQuery(query, null);
                if(cursor.moveToFirst()) {
                    do {
                        String myword = cursor.getString(cursor.getColumnIndexOrThrow(COL_WORD));
                        String mydefinition = cursor.getString(cursor.getColumnIndexOrThrow(COL_DEFINITION));
                        quizObject = new QObject(word, definition);
                    } while (cursor.moveToNext());
                }
                cursor.close();
                return quizObject;
        }

        private SQLiteDatabase getDbConnection() {
            return dbHelper.getReadableDatabase();
        }
    }
}

    public void searchName(View view) {
        String word = null;
        String definition = null;
        DatabaseTable db = new DatabaseTable(this);

        DatabaseBackend dbBackend = new DatabaseBackend(MainActivity.this);
        DatabaseObject dbo = new DatabaseObject(this);
        DatabaseB.QObject quizobject = new DatabaseB.QObject(word, definition);
        DatabaseB.QObject allQuizQuestions = quizobject.getAnswer(message);

        String answer = allQuizQuestions.definition;

        TextView textView = findViewById(R.id.textView2);
        textView.setText(answer);

    }

The error message is null object reference:

Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
...
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.justkitting.orion.databasetest.MainActivity$DatabaseB$QObject.definition' on a null object reference
at com.justkitting.orion.databasetest.MainActivity.searchName(MainActivity.java:139)
at java.lang.reflect.Method.invoke(Native Method)
...

Many many thanks.

Kit
  • 1
  • 1
  • For a start consider splitting the code for your Object `QObject` and the code that creates/retrieves these Objects. Also consider why you have declared the `QObject` as static? hint: there will only be able to be one of them. – Scary Wombat Feb 21 '18 at 06:15
  • Thanks @scary-wombat. I made QObject as static because it needs to be accessed later by searchName method, no? I will try to separate the creates and retrieves, thanks for the suggestion. – Kit Feb 21 '18 at 08:26

2 Answers2

0

Your exception is thrown at this line: String answer = allQuizQuestions.definition; and it means that allQuizQuestions is null. So in the line above (DatabaseB.QObject allQuizQuestions = quizobject.getAnswer(message);) you get null from getAnswer(message) method. And this can happen if cursor.moveToFirst() returns false and you never call this line of code: quizObject = new QObject(word, definition);.

One more thing I found: constructor in this line quizObject = new QObject(word, definition); is not using Strings you found in your DB, but values of word and definition from QObject class, which are null at this point. You should use myword and mydefinition instead.

Egan Wolf
  • 3,533
  • 1
  • 14
  • 29
  • Thank you, it works! I changed to `quizObject = new QObject(myword, mydefinition);` and I get the result successfully. Thanks for pointing that out to me. – Kit Feb 22 '18 at 07:30
-1

Can you post the code with the line numbers(If you are on eclipse right click on the far left side of the editor and select 'show line numbers' ). it looks like you are calling a method on an object which is null. You night need to do a null check before method invocation

Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30