0

I am trying to create a new array in this way:

final boolean[] arr = new boolean[list.size()];

This line is throwing a null pointer in my code in some cases. Under what situations can this happen? Can it happen when the list has zero elements? I am sure the list is not null at this stage.

6
  • 2
    It will throw NPE if list is null. Commented Aug 29, 2013 at 10:01
  • 1
    Check again, the only way that exact line will throw a nullpointer exception is that list is null. Commented Aug 29, 2013 at 10:01
  • Make sure you have initialized your list variable Commented Aug 29, 2013 at 10:07
  • I can't imagine another reason than list is null. How do you checked it? Can you show some code that you use to initialize list? Commented Aug 29, 2013 at 10:08
  • your list is null for sure as there are no statements which can throw NPE except for list.size() try to print size of your list to check whether its null or not Commented Aug 29, 2013 at 10:11

7 Answers 7

1

Having a NullPointerException thrown, means you have an uninitialized object. It has nothing to do with anything else. If the exception is thrown out of the line you mentioned, then as the comments on your question says, your list hasn't been initialized.

To create an array using a dynamic length (Note that an array size CANNOT change after is has been initialized), you can do this:

ArrayList<String> list = new ArrayList<String>();
//Add elements to your list
String[] arr = list.toArray(new String[list.size()]);
Sign up to request clarification or add additional context in comments.

Comments

0

This can only happen, if list is null. If list is empty, list.size() will return 0.

1 Comment

final boolean[] arr = new boolean[0]; will not throw any exception.
0
boolean[] arr = new boolean[0];

Even it is a valid syntax. you can create array with 0 size. So untill and unless list null it will not produce NullPointerException

Comments

0

The null point can occur when list is null, if it's only occurring on that line. If that is the exception you're getting, then list is null. Have another look at your code, and I'm sure you'll find out. Alternatively, debug your code and check the value of list at this line.

Comments

0

list was never instantiated. So, it is null. Check if you instantiated the list after declaration.

Comments

0

Please use List instead of array and finally get the array from the list ..

2 Comments

Why? If the size if fixed I would go for array.
if the size is fixed, then its fine. but here the size is decided dynamically according to a list .. `list.size()' decides the size and hence it is not fixed, i assumed
0

Make sure you have initialized your list variable. The only way it will throw a NPE is due to the list itself being null.

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.