0

I would like to know why I have got this error. Have you got idea and do you know how to fix it?

Error(I get this error in put()):

Exception in thread "main" java.lang.NullPointerException
at nacs.put(nacs.put:36)
at Program.main(Program.java:69)

Program: (Main)

Nac nacs = new Nac();
nacs.put(new Intsult(1));
nacs.put(new Intsult(2));

Class:

public class Nac implement IPoraw
{
public List<IAbstrsUlt> abs;
public void put(IAbstrsUlt value)
{
abs.add(value);
}
}
4

3 回答 3

10

Initialize list.

private List<IAbstrsUlt> abs = new ArrayList<IAbstrsUlt>();

Note: You should always try to declare your members more restrictive. Default value for Object is null. Refer Oracle tutorial to know default values.

于 2012-11-03T17:23:50.037 回答
2

变量 abs 永远不会初始化。

尝试这个

public List<IAbstrsUlt> abs = new ArrayList<>();
于 2012-11-03T17:25:06.580 回答
1
public List<IAbstrsUlt> abs;

-在上面的语句中,您只是简单地声明了一个List Reference Variable名为abstype的名称IAbstrsUlt,并且由于Object Reference Variable它的默认值为null所以在这种情况下也是如此。

-你必须初始化它。

public List<IAbstrsUlt> abs = new List<IAbstrsUlt>();

于 2012-11-03T17:29:01.657 回答