Java ArrayList java.lang.NullPointerException

1 java static arraylist nullpointerexception

对不起,我对编程很新,遇到了我希望解决的一个简单问题.我将把我的代码放在这里并解释我在下面尝试做什么.

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");
}
Run Code Online (Sandbox Code Playgroud)

}

ArrayList Populator:

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;
}
Run Code Online (Sandbox Code Playgroud)

}

运行时的错误消息:

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

我在这段代码中最新的东西是static {}块,用于从存储在ArrayListPopulator类中的数据初始化我的主数据列表.看来我没有正确初始化ArrayList,或正确添加元素,或者我没有正确地分配引用变量.

非常感谢你的帮助!

Kon*_*kov 8

ints1ToGo列表未初始化.

要么在构造函数中执行:

public ArrayListPopulator() {
    ints1ToGo = new ArrayList<Interpreter>();
}
Run Code Online (Sandbox Code Playgroud)

或者在尝试添加元素之前.

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.
Run Code Online (Sandbox Code Playgroud)

请注意,您必须对custs1ToGo列表执行相同操作,因为(我可以看到)您没有在任何地方初始化它并且在populateALcusts1方法中使用它.