0

This question seems to be very basic, but I don't know why, there is something wrong and I can't figure it out.

int [] concept = null; 
int i = 0; 
for (Iterator iterator = conceptsListGeneral.iterator(); iterator.hasNext();) {  
    Map<String, Object> map = (Map<String, Object>) iterator.next();  
    String count = (String)map.get("count");  
//  concept[i] = new Integer(count).intValue();  
//  concept[i]= Integer.parseInt(count, 10);  
    Integer intObj2 = Integer.valueOf(count);  
    concept[i]= intObj2.intValue();  
    i++;
}

The comented lines are some of the thigs I have tried. I get a java.lang.NullPointerException on the last line. By debugging, I know String count has a value, and olso Integer intObj2.
DOes anybody know why?? Thanks in advance

1

8 Answers 8

4

It's just because your concept array is null. Replace your first line with

int[] concept = new int[conceptsListGeneral.size()] 

and you'll have it working.

EDIT

Oh, and like Edwin says, don't forget to assign i a value.

2
  • 1
    i might not have a value, but mujer might also be asking for Integer.valueOf(null); It's just too hard to tell without some idea of the values being passed around. That's 3 possible issues on the same line, and the only solution would be to get a bit more of the code, or to have a few key System.out.println(variable); statements to actually report the value as it was at the time it was running.
    – Edwin Buck
    Feb 17, 2011 at 15:37
  • Totally agree, but I was lucky to spot the issue the first. +1 to you
    – Riduidel
    Feb 17, 2011 at 16:17
4

--- edited to answer the specific question asked ---

You are pulling out a string, and the asking Integer to provide the integer value encoded in the string.

How do you know the String was not null? Maybe the Map doesn't have a value for the specific key you asked.

How do you know that the concept array exists? Maybe you should System.out.println(...) it to see it's object identifier.

How do you know the String contains characters which represent a number? Maybe you are dealing with a string that doesn't have a corresponding int value?

In cases like these, it is often helpful to add a few 'System.out.println(...)' methods to make sure you are dealing with the same items you think you are dealing with.

Good luck.

--- original post follows ---

In assigning concept[i], you must use the variable i to figure out which element of concept you wish to set. Did you define it? What is it's value?

3
  • The answer(s) are in there, the important part is to get you thinking about how to eliminate the possible problems. That way you'll always get what you want in the future.
    – Edwin Buck
    Feb 17, 2011 at 15:34
  • By debugging, I know String count has a value, and olso Integer intObj2. Feb 17, 2011 at 15:39
  • Did you take a look at the value of concept in your debugger before you attempted to assign it's first element concept[0]?
    – Edwin Buck
    Feb 17, 2011 at 15:41
1

Create the int[] array.

int[] concept = new int[conceptsListGeneral.size()];
0
0

Use:

System.out.println(count);

or your preferred logging framework to see if you're definitely getting something in your Map at key "count". Your results look consistent with that not being the case.

Plus of course you need to initialise your concept[] array.

0

from the code you pasted you're concept array is still null. You need to initialize the concept array.

0

i think you must first initialize your array with some dimension like "concept=new int[20]"

0

You must define concept so it will have space for all elements you plan to put into it. According to your code, it must something like

int[] concept = new int[conceptsListGeneral.size()];
0

You get a NullPointerException because your concept array is null. You need to initialize it :

int[] concept = new int[conceptsListGeneral.size()];
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.