0

I've looked at similar questions but non had the same issue I'm having. My application keeps crashing on addQuestion() method and for the life of me I couldn't pin point down the problem. The error log shows number format exception at line 176 int difficulty = Integer.parseInt(cursor.getString(2)); I reviewed my code very carefully for possible SQL syntax errors but couldn't find anything. Here's my addQuestion method

public void addQuestion(Question question, String question_type){

   SQLiteDatabase db = getWritableDatabase();

    String query = "SELECT body FROM questions where body = '" +question.getBody()+"'";
    Cursor cr = db.rawQuery(query, null);

    if(cr.getCount() > 0){
        db.close();
        return;
    }
    else {
        ContentValues cv = new ContentValues();
        cv.put(type, question_type);
        cv.put(body, question.getBody());
        cv.put(answer, question.getAnswer());
        cv.put(difficulty, question.getDifficulty());

        try {
            db.insert(questions_table, null, cv);
        } catch (SQLiteException e) {
            e.getMessage();
        }
    }
    db.close();
}

and here's my getQuestion by type method:

public Question getQuestionByType(String type){
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT " + body + "," + answer + "," + difficulty + " FROM " + questions_table + " WHERE "
          +  this.type + " ='" + type+"';";

    Cursor cursor = db.rawQuery(query, null);
    cursor.moveToFirst();
    String body = cursor.getString(0);
    String answer = cursor.getString(1);
    int difficulty = Integer.parseInt(cursor.getString(2));
    db.close();

    return new Question(type, body, answer, difficulty);
}

Here's the log output

   java.lang.NumberFormatException: null
                                                                  at java.lang.Integer.parseInt(Integer.java:483)
                                                                  at java.lang.Integer.parseInt(Integer.java:556)
                                                                  at com.maz.quizzdat.DbHandler.getQuestionByType(DbHandler.java:176)
                                                                  at com.maz.quizzdat.MainActivity$1.onClick(MainActivity.java:47)

Any help is appreciated.

Edit: for those of you who may run into a similar situation, you cannot use Integer.parseInt(); in this particular situation on a string, even though the string is in fact a number. Use curser.getInt() instead of Integer.parseInt(curser.getString())

MZ4Code
  • 76
  • 7
  • What is the data type of difficulty in your db? – TuyenNTA May 14 '17 at 22:32
  • As your log show it may be the `difficulty` in your db are null – TuyenNTA May 14 '17 at 22:35
  • It says the data in that column is `null`. Are you sure your query matches any data in the third column? Are `body` and `answer` non-null? – River May 14 '17 at 22:35
  • 2
    Possible duplicate of [What is a NumberFormatException and how can I fix it?](http://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – Jason May 14 '17 at 22:36
  • Yes. The right way to do it was to use curser.getInt(2); since, clever me, i'm getting an int not a string. – MZ4Code May 14 '17 at 22:38
  • 1
    Just a tip going forward, your code is vulnerable to SQL injection attacks. Please investigate using prepared statements and you'll be better for it. – Jason May 14 '17 at 22:39
  • Thanks @Jason. This is my first app using SQL (First app in general) I will definitely look into it once the basic functionalities are done. – MZ4Code May 14 '17 at 23:50

2 Answers2

2

Try cursor.getInt(2) instead of getString(2), since that column contains Integers?

https://developer.android.com/reference/android/database/Cursor.html#getInt(int)

Edit: parseInt() obviously is not necessary anymore then.

enigma969
  • 397
  • 6
  • 21
  • Author added log output after I made my answer! – enigma969 May 14 '17 at 22:36
  • @MZ4Code ah, I figured the method just read the integer as a String, but oddly it just returns null if its not a string-type – River May 14 '17 at 22:40
  • @River yea me too. I'll play around with it and see why I was getting such a "strange" error. So the question is. Had I had my difficulty column to be char rather that int, would I have been able to user Integer.parseInt()? – MZ4Code May 14 '17 at 22:40
  • @River no, I still got the same numberformat error after trying to parse the result to an int – MZ4Code May 14 '17 at 22:42
  • 1
    @MZ4Code the answer is [here](https://developer.android.com/reference/android/database/Cursor.html#getString(int)). *The result and whether this method throws an exception when the column type is __not a string type__ is implementation-defined.* – River May 14 '17 at 22:42
0

You should use if(cursor .getCount() > 0) before cursor.moveToFirst() in your getQuestionByType method to be sure that the query has a result, then you can use cursor.getInt(0)

River
  • 8,585
  • 14
  • 54
  • 67