java线程暂挂,线程 [main](已暂挂(异常 NullPointerException))怎么解决?

线程 main (已暂挂(异常 NullPointerException)):

package socket;

public class Count4 {

public static int counter;

static{

counter=(Integer.getInteger( "myApp.Count.counter ").intValue());//出错

}

}

package socket;

public class TestStaticInit{

public static void main(String[] args){

System.out.println( "counter = "+ Count4.counter);//出错

}

}

------解决方法--------------------------------------------------------

counter=(Integer.getInteger( "myApp.Count.counter ").intValue());//出错

============

Integer.getInteger( "myApp.Count.counter ")

确定具有指定名称的系统属性的整数值。

第一个参数被视为系统属性的名称。通过 System.getProperty(java.lang.String) 方法可以访问系统属性。然后,将该属性的字符串值解释为一个整数值,并返回表示该值的 Integer 对象。使用 getProperty 的定义可以找到可能出现的数字格式的详细信息。

如果没有具有指定名称的属性,或者指定名称为空或 null,或者属性的数字格式不正确,则返回 null。

这样你上面那个错就很明显了。

myApp.Count.counter 是个什么东西?你定义在什么地方?

System.out.println( "counter = "+ Count4.counter);//出错

是由于上面那个错误引起的,不属于讨论范围。

------解决方法--------------------------------------------------------

Java doc:

getInteger

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. 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, this method returns an Integer object equal to the value of:

getInteger(nm, null)

Parameters:

nm - property name.

Returns:

the Integer value of the property.

See Also:

System.getProperty(java.lang.String), System.getProperty(java.lang.String, java.lang.String)

------解决方法--------------------------------------------------------

中文的来了,既然是初学, 就不要整这么复杂的

getInteger

public static Integer getInteger(String nm,

int val)确定具有指定名称的系统属性的整数值。

第一个参数被视为系统属性的名称。通过 System.getProperty(java.lang.String) 方法可以访问系统属性。然后,将该属性的字符串值解释为一个整数值,并返回表示该值的 Integer 对象。使用 getProperty 的定义可以找到可能出现的数字格式的详细信息。

第二个参数是默认值。如果未具有指定名称的属性,或者属性的数字格式不正确,或者指定名称为空或 null,则返回一个表示第二个参数的值的 Integer 对象。

换句话说,该方法返回一个等于以下值的 Integer 对象:

getInteger(nm, new Integer(val))

但在实践中可能会用以下类似方式实现它:

Integer result = getInteger(nm, null);

return (result == null) ? new Integer(val) : result;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值