Java ArrayList java.lang.NullPointerException

时间:2014-04-16 15:30:22

标签: java arraylist static 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");
}

}

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;
}

}

运行时的错误消息:

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

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

非常感谢您的帮助!

4 个答案:

答案 0 :(得分:8)

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列表执行相同的操作,因为(我可以看到)您没有在任何地方对其进行初始化,而是在{{1}中使用它方法。

答案 1 :(得分:0)

您永远不会初始化ints1ToGo

ints1ToGo = new ArrayList<Interpreter>();

答案 2 :(得分:0)

ints1ToGo永远不会被初始化。将private ArrayList<Interpreter> ints1ToGo;更改为private ArrayList<Interpreter> ints1ToGo = new ArrayList<Interpreter>();

答案 3 :(得分:0)

您需要初始化两个数组实例

private ArrayList<Interpreter> ints1ToGo = new ArrayList<Interperter>();
private ArrayList<Customer> custs1ToGo = new ArrayList<Customer>();