0

I'm using JSON and database to get some values and set them into listviews. JSON objects are ok but when I try to get values from database(cursor objects) and set them into ArrayList, it gives NullPointerException.

ArrayList<String> stringName,stringAddress,stringCapacity;
...
...
...
cursor_restnames = db.getRestNames("Ankara");

if (cursor_restnames.moveToFirst()) {
    for (cursor_restnames.moveToFirst(); !cursor_restnames.isAfterLast(); cursor_restnames.moveToNext()) {
        System.out.println("aaaaa "+cursor_restnames.getString(cursor_restnames.getColumnIndex("name")).toString()); // I can see the cursor object in logcat which is "aspava".
        stringName.add(cursor_restnames.getString(cursor_restnames.getColumnIndex("name")).toString()); // I got error here which is NullPointerException but as I mentioned it is not null, it has "aspava" string.
    }

    cursor_restnames.close();
}

My DBadapter has getRestNames function which is;

public Cursor getRestNames(String city) {
      Cursor myCursor = db.rawQuery("select name from rest where city='"+city+"'", null);

       if (myCursor != null) {  
            myCursor.moveToFirst();  
       }  

       return myCursor;  
  }

If these code snippets will not help, I can write down the whole project. Thanks for help and if there are any mistakes about my English, I'm sorry.

Beena
  • 2,334
  • 24
  • 50
Y.Kaan Yılmaz
  • 612
  • 5
  • 18
  • Hello and welcome to Stack Overflow. Unfortunately you don't show us enough code to help you: a) The exception will give you the exact line number of the issue. b) You don't show where any of your ArrayLists are set. Perhaps this question may help: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – John3136 Dec 22 '15 at 04:37
  • Babul Patel answered my question, there were a little mistake. Thanks for help anyway // Edit: Also I wrote my errors as a comment on the code snippets. – Y.Kaan Yılmaz Dec 22 '15 at 04:42

2 Answers2

2

Have you initialized your Arraylist like

stringName = new Arraylist();

Harsh Patel
  • 1,056
  • 2
  • 10
  • 20
1

you should initialize your array list because in this way your object value is null

 ArrayList<String> stringName = new ArrayList<String>();