0
Exhibit[] listOfEx = exhibits.toArray(new Exhibit[exhibits.size()]);;
        String[] listOfSt = null;

        for (int i = 0; i < exhibits.size(); i++){
            Log.d("DEBUG", String.valueOf(exhibits.size()));
            listOfSt[i] = listOfEx[i].getName();
            Log.d("DEBUG", listOfSt[i]);
        }

        ListView list = (ListView) findViewById(android.R.id.list);
                list.setAdapter(new ArrayAdapter<String>(this, 
                        R.layout.listview_item, listOfSt));

My app crashes where it assigns in loop. I get this:

07-18 22:43:32.760: E/AndroidRuntime(28545): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.foothill.junk/edu.foothill.junk.MainActivity}: java.lang.NullPointerException

Please let me know if more info needed. Thank you in advance.

1
  • Point out which line the exception occured on. Commented Jul 19, 2014 at 5:50

2 Answers 2

2

What do you expect this code to do?

String[] listOfSt = null;

It creates a null variable, right?

And then you use the null variable (without having initialized it first):

 listOfSt[i] = listOfEx[i].getName();
Sign up to request clarification or add additional context in comments.

Comments

0

You have to initialize the array listOfSt with an empty array, not null:

String[] listOfSt = new String[length];

where length will be the length of the array, so it will depend on how many items you want it to store. If you want it to have the same length than listOfEx, you can use

String[] listOfSt = new String[listOfEx.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.