0

In AsyncTask I read database table (one-by-one) like this:

Cursor result = database.query("country", new String[] { "_id", "name", "var1", "var2", "var3", "var4", "rightanswer", "time1", "time2", "time3" }, "_id=" + id, null, null, null, null);
result.moveToFirst();

Then, I print result.getColumnCount() and receive 10.

But when I trying to print data like this:

 System.out.println(result.getString(result.getColumnIndex("name")));
 System.out.println(result.getString(result.getColumnIndex("var1")));
 System.out.println(result.getString(result.getColumnIndex("var2")));
 System.out.println(result.getString(result.getColumnIndex("var3")));
 System.out.println(result.getString(result.getColumnIndex("var4")));
 System.out.println(result.getString(result.getColumnIndex("rightanswer")));
 System.out.println(result.getString(result.getColumnIndex("time1")));
 System.out.println(result.getString(result.getColumnIndex("time2")));
 System.out.println(result.getString(result.getColumnIndex("time3")));

I receive error AndroidRuntime(8279): FATAL EXCEPTION: AsyncTask #1

AndroidRuntime(8279): Caused by: java.lang.NullPointerException

But, when I print only two first columns,

 System.out.println(result.getString(result.getColumnIndex("name")));
 System.out.println(result.getString(result.getColumnIndex("var1")));

It's fully working.

I have no clue.

UPDATE:

  String createQuery = "CREATE TABLE country (_id integer primary key autoincrement,name, var1, var2,var3,var4,rightanswer, time1,time2,time3);";                 
   db.execSQL(createQuery); 
5
  • It may the 'var2' is returning null Commented May 6, 2013 at 9:09
  • 1
    show the db.create query Commented May 6, 2013 at 9:10
  • 1
    Which line throws the NPE? Commented May 6, 2013 at 9:11
  • may be there is no string value in "var2" column Commented May 6, 2013 at 10:07
  • Show the entire stack trace! Commented May 6, 2013 at 11:53

1 Answer 1

1

It seems that the value

result.getString(result.getColumnIndex("var2"))

returns is null. You can enter debug mode in your IDE and verify that.

Another possibility is that you don't even have a column named var2 in your database. In that case the getColumnIndex will return null.

If you provide us more information like your db schema we can help you more.

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

1 Comment

I check columns names of course. Then, I checked data and fill every column.

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.