开发者

String to int gives nullpointerexception

开发者 https://www.devze.com 2023-03-18 05:39 出处:网络
When I run this code the conversion of a string to int is printing out NULL ? When I print out the string it gives me a string number, but when I try to convert that string into a int it says null, wh

When I run this code the conversion of a string to int is printing out NULL ? When I print out the string it gives me a string number, but when I try to convert that string into a int it says null, why is that?

for(int j = 0; j < removetrack.size(); j++){
   String removetrackArray[] = removetrack.get(j).split(" ");
   String candidateBefore = "";
   int removetracklocation = Arrays.asList(removetrackArray).indexOf(past)-1;

   if(removetracklocation != 1) {
      String candidateBefore = "";
      System.out.println(removetrack.get(j)+" lo开发者_如何学编程cation =  "+ removetracklocation +" "+ 
         (past)+" candidate name "+dictionary.get(votedfor) );
      candidateBefore= Arrays.asList(removetrackArray).get(removetracklocation+1);
      System.out.println(" this is a string "+candidateBefore);
      System.out.println( Integer.getInteger(candidateBefore));                         
   }    
}


Integer.getInteger does not cast your string into an int, int returns the value of a system property (see http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#getInteger(java.lang.String)). You should be using Integer.parseInt instead.


Javadoc to the rescue:

Determines the integer value of the system property with the specified name.

Use Integer.parseInt to transform the string into an int, and Integer.valueOf to transform the string into an Integer.


From the docs for Integer.getInteger:

Determines the integer value of the system property with the specified name. The first argument is treated as the name of a system property. System properties are accessible through the System.getProperty(java.lang.String) method. The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. Details of possible numeric formats can be found with the definition of getProperty.

If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null is returned.

In other words, it doesn't parse an integer. To parse an integer, either use Integer.parseInt (to get an int) or Integer.valueOf (to get an Integer).

However, even your description is slightly odd - you claim in the title that it's giving you a NullPointerException, but you then say it's printing null. Which is it? They're very different. I can't see how the code you've given us would throw a NullPointerException at Integer.getInteger.

Alternatively, if this is a value entered by a user, you may want to use java.text.NumberFormat instead.


Integer.getInteger() is used for system properties:

Integer.getInteger("sun.arch.data.model"); 
0

精彩评论

暂无评论...
验证码 换一张
取 消