0

I am having trouble initializing an array of objects. I keep getting an error(NullPointerException), I have tried a few different approaches to initialization. I am fairly new to java ( started from c++). I was thinking maybe I need to create a constructor for the class Customer, but that didn't work when I tried it. Any ideas? Thanks in advance. (these are only snippets of my code)

Update: Ok so I replaced the declaration and I am still getting nullpointerexception errors on a few lines, here is the updated code. Am I initializing the privates of the Customer class wrong? Thanks again for the help people, much appreciated.

Update 2: So after some tinkering, I created a for loop that individually initializes each index of the class object array. Thanks a bunch!

public static class ATM{
    private double funds = 100;
    private int serial;
    private String location = "UNKNOWN", bank_name;
    private int succ_with = 0, fail_with =0;
    private Customer[] customers = new Customer[10];///////updated here
    public void custSet(String u, String b, int p, double bal, Customer[] customer, int index)
    {
        customer[index].user_name = u;
        customer[index].bank_name = b;//second exception 
        customer[index].balance = bal;
        customer[index].pin = p;
    }
    public void populateCust()
    {

        custSet("Alice","OtterUnion",1234,5000.0,customers,0);//3rd exception
        custSet("Tom","OtterUnion",2000,200.0,customers,1);
        custSet("Monica","OtterUnion",3000,50.0,customers,2);
        custSet("Michael", "OtterUnion",7777,0.00,customers,3);
        custSet("John","OtterUnion",8000,500.00,customers,4);
        custSet("Jane","OtterUnion",2222,500.00,customers,5);
        custSet("Robert","BOA",2323,200.00,customers,6);
        custSet("Owen","BOA",4455,50.00,customers,7);
        custSet("Chris","BOA",8787,10.00,customers,8);
        custSet("Rebecca","BOA",8080,555.55, customers,9);
    }

    }
public static class Customer{
    private String user_name, bank_name;
    private int pin;//(first exception complaining about this, but when i 
    // change it to public the error goes away, so Ill just need to make a setter
    private double balance;
}
jay_rem21
  • 1
  • 1
  • 2
    you never initialized the array, only declared it. `private Customer[] customers = new Customer[10];` – jb. Feb 19 '16 at 23:19
  • Ok so I've updated my post with this. I still seem to be getting this exception. I marked the lines where the compiler complains about it. Thanks – jay_rem21 Feb 20 '16 at 02:36

1 Answers1

0

You need to instantiate your array before using it: customers = new Cusomter[size]

dbrown93
  • 344
  • 2
  • 10