1

I'm sorry I am very new to programming and have encountered what I hope is a simple problem to solve. I'll put my code in here and explain what I am trying to do below.

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

}

Error Message at runtime:

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

The thing I am most new to in this code is the static {} block to initialize my main ArrayLists of data from the data stored in the ArrayListPopulator class. It would appear that I am not initializing the ArrayList properly, or adding the elements properly, or I'm not assigning the reference variable properly.

Thank you so much for any help!

5
  • what does line 16 refer to? Commented Apr 16, 2014 at 15:31
  • Are you initializing your ArrayList collections anywhere? Commented Apr 16, 2014 at 15:32
  • 1
    "Static blocks execute first - and are great for initializing data" and are great for causing difficulties with unit testing, mocking, controlling initialisation order between modules etc. Use with care. I would much prefer initialisation in a constructor Commented Apr 16, 2014 at 15:36
  • line 16 refers to a function taking a customers name and comparing it to the database of interpreters and their distance from the customer - however it is not yet implemented. Commented Apr 16, 2014 at 15:43
  • Thank you very much for being so helpful in answering my first question here :) I hope one day to be able to share some knowledge in return! Commented Apr 16, 2014 at 15:47

4 Answers 4

8

The ints1ToGo list is not initialized.

Either do it in the constructor:

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

or before you try to add elements.

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.

Note that you will have to do the same for the custs1ToGo list, because (as I can see) you're not initializing it anywhere and it's used in the populateALcusts1 method.

Sign up to request clarification or add additional context in comments.

Comments

0

You never initialize ints1ToGo:

ints1ToGo = new ArrayList<Interpreter>();

Comments

0

ints1ToGo is never initialized. Change private ArrayList<Interpreter> ints1ToGo; to private ArrayList<Interpreter> ints1ToGo = new ArrayList<Interpreter>();

Comments

0

You will need to initialize both array instances

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.