Java ArrayList java.lang.NullPointerException

TristonianDP

对不起,我对编程还很陌生,并且遇到了我希望解决的一个简单问题。我将把代码放在这里,并在下面解释我要做什么。

public class main {

static ArrayList<Interpreter> ints1;
static ArrayList<Customer> custs1;
static MainFunctions mainF;

static { //Static blocks execute first - and are great for initializing data!
    ArrayListPopulator ALP1 = new ArrayListPopulator();
    ints1 = ALP1.populateALints1(); // error occurs on this method call.
    custs1 = ALP1.populateALcusts1();
    mainF = new MainFunctions(ints1, custs1);
}

public static void main(String[] args) {

    mainF.findNearestInterp("Frank");
}

}

ArrayList填充器:

import java.util.ArrayList;
public class ArrayListPopulator {

private ArrayList<Interpreter> ints1ToGo;
private ArrayList<Customer> custs1ToGo;

public ArrayList<Interpreter> populateALints1() {
    //Format is "String pName, int pAge, String PGender,
            // int pSignLevel, boolean pDeafBlindExp, double pLatitude, double pLongitude, String pTown"

            //Gender must be "Male"||"Female"

            //In future this could be done by scanning a local config text file. Wish that I knew that stuff :l

    Interpreter Elliott = new Interpreter("Elliott", 23, "Male", 6, true, 52.098049, 0.277860, "Linton");
    ints1ToGo.add(Elliott); //error occurs here.

    Interpreter Sarah = new Interpreter("Sarah", 20, "Female", 3, true, 52.209950, 0.137774, "Cambridge");
    ints1ToGo.add(Sarah);

    Interpreter Argibarge = new Interpreter("Argibarge", 42, "Male", 3, false, 52.599199, -0.264226, "Peterborough");
    ints1ToGo.add(Argibarge);

    Interpreter Bruce = new Interpreter("Bruce", 30, "Male", 2, false, 50.717527, -3.540192, "Exeter");
    ints1ToGo.add(Bruce);

    Interpreter Medusa = new Interpreter("Medusa", 1009, "Female", 4, false, 55.867795, -4.267566, "Glasgow");
    ints1ToGo.add(Medusa);

    return ints1ToGo;
}

public ArrayList<Customer> populateALcusts1() {
    //Format is "String pName, int pAge, String PGender,
    //boolean pDeafBlind, double pLatitude, double pLongitude, String pTown"

    //Gender must be "Male"||"Female"

    //In future this could be done by scanning a local config text file. Wish that I knew that stuff :l

    Customer Frank = new Customer("Frank", 30, "Male", false, 56.113482, -3.934635, "Stirling");
    custs1ToGo.add(Frank);

    Customer Eleanor = new Customer("Eleanor", 23, "Female", true, 52.622439, 1.281124, "Norwich");
    custs1ToGo.add(Eleanor);

    Customer Pacha = new Customer("Pacha", 43, "Male", false, 52.397273, -0.727392, "Kettering");
    custs1ToGo.add(Pacha);

    Customer Roy = new Customer("Roy", 69, "Male", false, 51.746940, -1.257345, "Oxford");
    custs1ToGo.add(Roy);

    Customer Jenette = new Customer("Jenette", 16, "Male", false, 51.871877, 0.357845, "Great Dunmow");
    custs1ToGo.add(Jenette);
    return custs1ToGo;
}

}

运行时错误消息:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at ArrayListPopulator.populateALints1(ArrayListPopulator.java:16)
at main.<clinit>(main.java:23)

我在这段代码中最陌生的是静态{}块,用于根据ArrayListPopulator类中存储的数据初始化我的主要ArrayLists数据。看来我未正确初始化ArrayList或未正确添加元素,或者我未正确分配引用变量。

非常感谢您的帮助!

康斯坦丁·约夫科夫(Konstantin Yovkov)

ints1ToGo未初始化列表。

可以在构造函数中执行以下操作:

public ArrayListPopulator() {
    ints1ToGo = new ArrayList<Interpreter>();
}

尝试添加元素之前。

ints1ToGo = new ArrayList<Interpreter>();
Interpreter Elliott = new Interpreter("Elliott", 23, "Male", 6, true, 52.098049, 0.277860, "Linton");
ints1ToGo.add(Elliott); //error won't occur here anymore.

请注意,您必须对custs1ToGo列表执行相同的操作,因为(如我所见),您无需在任何地方对其进行初始化,并且该populateALcusts1方法已在其中使用了它

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章