1

I have a little question. I want to put some strings in an String[]. How can I do this?

I tried

String[] categorys;
for (int i = 0; i < jArray.length(); i++) {
                        JSONObject jsdata = jArray.getJSONObject(i);
                        String myString = jsdata.getString("id_category");
                        String namecategory = jsdata.getString("category_name");
                        System.out.println("Category name" + namecategory);
                        categorys[i] = namecategory;
                    }

but I get 07-28 16:40:01.719: ERROR/AndroidRuntime(452): Caused by: java.lang.ArrayIndexOutOfBoundsException

I don't know how to use an array of Strings. Need some help.Thanks...

3
  • It needs instantiation not initialization. Commented Jul 28, 2011 at 13:53
  • 1
    if not, String[] myString = new String[jArray.length()]; add this Commented Jul 28, 2011 at 13:54
  • I was looking for this for ages!Thanks! Commented Jul 28, 2011 at 14:05

2 Answers 2

5

You need to allocate the array.

String[] categorys = new String[jArray.length()];
Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to set the value of an element of the array which doesn't exist (your array has no elements). You need to initialize the length of the array.

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.