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())

7
  • What is the data type of difficulty in your db? Commented May 14, 2017 at 22:32
  • As your log show it may be the difficulty in your db are null Commented May 14, 2017 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? Commented May 14, 2017 at 22:35
  • 2
    Possible duplicate of What is a NumberFormatException and how can I fix it? Commented May 14, 2017 at 22:36
  • 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. Commented May 14, 2017 at 22:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Author added log output after I made my answer!
@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 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()?
@River no, I still got the same numberformat error after trying to parse the result to an int
@MZ4Code the answer is here. The result and whether this method throws an exception when the column type is not a string type is implementation-defined.
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)

3 Comments

This is only a check to make sure the question does not already exist in the table.
This should be a comment about good practices, it doesn't answer the question.
See his reputation? He's not yet able to comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.