Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在实例化之后链接声明会导致NullPointerException?_Java - Fatal编程技术网

Java 为什么在实例化之后链接声明会导致NullPointerException?

Java 为什么在实例化之后链接声明会导致NullPointerException?,java,Java,我以为我理解了NullPointerException,但显然不理解。这将抛出一个错误: (main是一个类) TopicActionWeight是一个列表。以下是我对名单的声明: public static List<Float> topicActionWeight, topicAdventureWeight, topicRPGWeight, topicStrategyWeight, topicSimulationWe

我以为我理解了NullPointerException,但显然不理解。这将抛出一个错误: (
main
是一个类)

TopicActionWeight
是一个列表。以下是我对名单的声明:

public static List<Float> topicActionWeight,
        topicAdventureWeight,
        topicRPGWeight,
        topicStrategyWeight,
        topicSimulationWeight = new ArrayList<>();
公共静态列表topicActionWeight,
主题权重,
头重,
主题战略权重,
topicSimulationWeight=新的ArrayList();
我声明的列表不是指针,是吗?他们是被创造出来的


是的,我试过
newarraylist()

通过执行下面的行,您只声明了所有相应的ArrayList,而没有初始化除topicSimulationWeight之外的任何ArrayList,这就是为什么您的topicActionWeight为null,因此NPE为空

public static List<Float> topicActionWeight,
        topicAdventureWeight,
        topicRPGWeight,
        topicStrategyWeight,
        topicSimulationWeight = new ArrayList<>();
公共静态列表topicActionWeight,
主题权重,
头重,
主题战略权重,
topicSimulationWeight=新的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<>();
publicstaticlist topicActionWeight=newarraylist();
public static List topicadventurewweight=new ArrayList();
public static List topicRPGWeight=new ArrayList();
public static List topicStrategyWeight=new ArrayList();
public static List topicSimulationWeight=new ArrayList();

可能
getSelectedItem()
返回null。甚至是
actionGenreWeightCombo
(这就是我讨厌链接函数调用的原因之一,因为现在你从来没有遇到过这样的错误)首先,Java语言中没有“指针”。不过,所有对象通常都是使用指针实现的。谢谢!我的“代码伙伴”告诉我,如果我在一次“声明”中就做到了,那么它会更快、更高效。从现在起,对你的代码伙伴所说的一切都要三缄其口。
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<>();