Issue changing data types Java
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 = nu开发者_StackOverflow社区ll;
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 advanceIt'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.
--- 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?
Create the int[] array.
int[] concept = new int[conceptsListGeneral.size()];
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.
from the code you pasted you're concept array is still null. You need to initialize the concept array.
i think you must first initialize your array with some dimension like "concept=new int[20]"
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()];
You get a NullPointerException because your concept
array is null. You need to initialize it :
int[] concept = new int[conceptsListGeneral.size()];
精彩评论