简体   繁体   English

int和字符串之间的解析给出了意外的结果

[英]Parsing between int and string giving unexpected results

Below line of code gives NullPointerException in java 下面的代码行给出了Java中的NullPointerException

for any input (07,11 etc in src) 对于任何输入 (src中的07,11等)

hrs = Integer.toString((Integer.getInteger(hrs)) + 12);

So i changed it to : 所以我将其更改为:

Integer  H = Integer.parseInt(hrs);
H = H + 12;
hrs = H.toString();

Which is working fine. 哪个工作正常。

Can anyone tell me why first line is giving NullpointerException ? 谁能告诉我为什么第一行给出NullpointerException吗?

Simple: you are calling two different methods in your two examples. 很简单:您在两个示例中调用了两种不同的方法。

Integer.getInteger(hrs)

Is not the same as 与...不同

Integer.parseInt(hrs);

See the javadoc for getInteger() : 参见javadoc中的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. 系统属性可通过System.getProperty(java.lang.String)方法访问。 The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. 然后,将此属性的字符串值解释为整数值,并返回表示该值的Integer对象。 Details of possible numeric formats can be found with the definition of getProperty. 可以使用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. 如果没有具有指定名称的属性,或者指定的名称为空或null,或者该属性的格式不正确,则返回null。

Then null is returned! 然后返回null

That is all there is to this. 这就是全部。 So, the real answer is probably: pay more attention to the "code" suggestions that your IDE gives to you. 因此,真正的答案可能是:更加注意IDE为您提供的“代码”建议。 Don't blindly accept them; 不要盲目接受它们;不要盲目接受它们。 but ensure that you are really putting down that method you intend to use; 但请确保您确实放下了打算使用的方法; and not some other one that happens to have a similar signature (regarding input parameters and return types). 而不是碰巧具有相似签名(关于输入参数和返回类型)的其他签名。

 public static Integer getInteger(String nm)

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. 系统属性可通过System.getProperty(java.lang.String)方法访问。 The string value of this property is then interpreted as an integer value and an Integer object representing this value is returned. 然后,将此属性的字符串值解释为整数值,并返回表示该值的Integer对象。 Details of possible numeric formats can be found with the definition of getProperty. 可以使用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. 如果没有具有指定名称的属性,或者指定的名称为空或null,或者该属性的格式不正确,则返回null。

In your case most probably you need to use 在您的情况下,很可能您需要使用

public static int parseInt(String s,
                           int radix)
                    throws NumberFormatException

For details of the both please use following link 有关两者的详细信息,请使用以下链接

You have to use Integer.parseInt(String) like this : 您必须像这样使用Integer.parseInt(String)

hrs = Integer.toString((Integer.parseInt(hrs)) + 12);

But you can use this instead : 但是您可以改用:

hrs = (Integer.parseInt(hrs) + 12) + "";

一个简单的解决方案是

hrs = "" + (Integer.parseInt(hrs) + 12);

Integer.getInteger() doesn't serve your purpose in this case. 在这种情况下,Integer.getInteger()无法满足您的目的。 If you see the documentation of this method, it is mainly to determine the integer value of the system property with the specified name. 如果您看到此方法的文档,则主要是确定具有指定名称的系统属性的整数值。 If the given name does not matches with any of the system properties then it returns null. 如果给定名称与任何系统属性都不匹配,则返回null。

In your code, (Integer.getInteger(hrs))+12 this is basically causing NPE as you are trying to concatenate null with 12 在您的代码中,(Integer.getInteger(hrs))+ 12基本上是导致NPE的原因,因为您尝试将null与12连接

Please see this link for more details 请查看此链接以获取更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM