2014-08-31 57 views
0

我以为我明白NullPointerException,但显然不是。在这里,这将引发错误: (main是一类)为什么链式声明之后是实例化会导致NullPointerException?

main.topicActionWeight.add(
    Float.parseFloat(this.actionGenreWeightCombo.getSelectedItem().toString())); 

TopicActionWeight是一个列表。这里是我的清单声明:

public static List<Float> topicActionWeight, 
     topicAdventureWeight, 
     topicRPGWeight, 
     topicStrategyWeight, 
     topicSimulationWeight = new ArrayList<>(); 

我声明的列表不是指针,它们是?他们创造了..

是的,我已经试过new ArrayList<Float>();

+0

可能'getSelectedItem()'返回null。甚至是'actionGenreWeightCombo'(这就是我讨厌链接函数调用的原因之一,因为你现在从来没有发生过这样的错误) – 2014-08-31 10:10:01

+1

首先,Java语言中没有“指针”。但是,所有对象通常都是使用指针**实现的。** – 2014-08-31 10:10:25

回答

1

通过执行以下行,你只申报所有相应的ArrayList,但为什么你topicActionWeight为空,因此不进行初始化没有人,除了topicSimulationWeight这是NPE。

public static List<Float> topicActionWeight, 
     topicAdventureWeight, 
     topicRPGWeight, 
     topicStrategyWeight, 
     topicSimulationWeight = new ArrayList<>(); 

初始化的正确的方法是: -

public static List<Float> topicActionWeight = new ArrayList<>(); 
public static List<Float> topicAdventureWeight = new ArrayList<>(); 
public static List<Float> topicRPGWeight = new ArrayList<>(); 
public static List<Float> topicStrategyWeight = new ArrayList<>(); 
public static List<Float> topicSimulationWeight = new ArrayList<>(); 
+0

谢谢!我的'密码伙伴'告诉我,如果我在一个“声明”中做到这一点,速度更快,效率更高。 – user3902017 2014-08-31 10:20:04

+1

从现在开始,你的代码好友用一粒盐来说吧。 – 2014-08-31 10:25:45

相关问题