0

I wanted to store a value from a string array to another string array. But I get "NullPointerException" error with the code below. "imagesSelected" is a string array stored with values inside. But when i wanted to move it into another string array after substring, I get error. I believed is because of the last line of code. I'm not sure how to make it work.

String[] imageLocation;

        if(imagesSelected.length >0){
        for(int i=0;i<imagesSelected.length;i++){
            int start = imagesSelected[i].indexOf("WB/");
            imageLocation[i] = imagesSelected[i].substring(start + 3);
        }
        }
3
  • You need to initialize the imageLocation array to the right size. Commented Dec 12, 2012 at 4:33
  • 1
    You didn't initialize your string of array String[] imageLocation = new String[x]; is it? Commented Dec 12, 2012 at 4:34
  • Yes. It seems that way, I didn't initialize the string array that's the reason for NullPointerException. Thanks for the comment. Commented Dec 12, 2012 at 7:07

5 Answers 5

5

You need to do something like this:

String[] imageLocation = new String[imagesSelected.length];

Otherwise imageLocation will be null.

By the way, you don't need the if around your loop. It's completely redundant, as that will be the same logic that will be used at the start of the loop.

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

Comments

4

imageLocation[i]

have you initialized imageLocation?

I believe this error is because you are trying to point to a location in the string array that does not exist. imageLocation[0,1,2,3...etc] do not exist yet because the string array has not been initialized.

Try String[] imageLocation[however long you want the array to be]

Comments

2

You must allocate memory for imageLocation.

imageLocation = new String[LENGTH];

1 Comment

I solved my problem already. But still thank you for the answer and your time!
1

Your final solution code should be like as below, or compiler will give you an error that imageLocation may not have been initialized

    String[] imageLocation = new String[imagesSelected != null ? imagesSelected.length : 0];

    if (imagesSelected.length > 0) {
        for (int i = 0; i < imagesSelected.length; i++) {
            int start = imagesSelected[i].indexOf("WB/");
            imageLocation[i] = imagesSelected[i].substring(start + 3);
        }
    }

Comments

1

look at this code

String[] imageLocation;

        if(imagesSelected.length >0){
          imageLocation = new String[imageSelected.length];
        for(int i=0;i<imagesSelected.length;i++){
            int start = imagesSelected[i].indexOf("WB/");
            imageLocation[i] = imagesSelected[i].substring(start + 3);
        }
        }

1 Comment

I got it working already. Thanks for the answer and your time!

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.