Blanca Hdez
Blanca Hdez

Reputation: 3563

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 = 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

Upvotes: 0

Views: 106

Answers (8)

JB Nizet
JB Nizet

Reputation: 691765

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

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

Upvotes: 0

Victor Sorokin
Victor Sorokin

Reputation: 12006

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()];

Upvotes: 0

Gatekeeper
Gatekeeper

Reputation: 1616

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

Upvotes: 0

Timon
Timon

Reputation: 376

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

Upvotes: 0

Brian
Brian

Reputation: 6450

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.

Upvotes: 0

Erik
Erik

Reputation: 91270

Create the int[] array.

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

Upvotes: 1

Riduidel
Riduidel

Reputation: 22292

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.

Upvotes: 4

Edwin Buck
Edwin Buck

Reputation: 70909

--- 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?

Upvotes: 4

Related Questions