0

This is my code from my android project where i would want to store each string element from the cursor to a string array, but i keep getting NullPointer Exception.

The Code is Down Below, series2Strings[] is the string array i have defined,

int k=0;
        if(cur.moveToFirst()){
            while(!cur.isAfterLast()){

                Log.i("String", " "+ k + " " + cur.getString(1));
                series2Strings[k]=cur.getString(1);
                k++;
                cur.moveToNext();
            }
        }


        cur.close();

The data is available at the cursor because i have checked it in the logcat by using the log statement. Can anyone please point what's going wrong.

1
  • 1
    maybe your array series2Strings is not initialized Commented Mar 19, 2013 at 13:38

2 Answers 2

1

You need to initialize your string array first..

series2Strings = new String[cur.getCount()];
int k=0;
if(cur.moveToFirst()){
    while(!cur.isAfterLast()){

      Log.i("String", " "+ k + " " + cur.getString(1));
       series2Strings[k]=cur.getString(1);
        k++;
         cur.moveToNext();
       }
 }
 cur.close();
Sign up to request clarification or add additional context in comments.

2 Comments

I've got the output in the logcat, but just after it comes out of the loop it goes into NullPointerException and the app closes. Should i use try/catch or is there any recification.
I got it, could able to remove the NullPointerException by adding a if inside the while loop. Thanks
0
final int length = cur.getCount();
series2Strings = new series2Strings[length];

Comments

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.