如何将实例变量分配给对象数组

时间:2019-07-14 03:14:37

标签: java arrays object instance-variables mutators

我创建了一个带有两个子类的父类,这两个子类包含更改实例变量和.toString()方法的方法。在另一个包含我的主要功能的类中,我试图创建一个对象数组,然后可以对其进行排序。问题是当我尝试使用设置器更改实例变量时。我收到“ java.lang.NullPointerException”错误,找不到在线原因。

我尝试过仔细检查父类和子类的拼写错误。我还尝试过将实例变量更改为常量所涉及的变量。我试图更改存储对象的阵列中的乳液。


System.out.println("Which customer is it?");
customerNumber = scan.nextInt();  //scan is a scanner that is setup
customer[index].setAcctNum(customerNumber); //This is where the error is. .setAcctNum() is a mutator that I made to change a variable called AccountNumber

arr[index] = customer.toString(); //This is supposed to be an array to store the .toString(); value which is the AccountNumber variable changed in the code above.```

What I want this code to do is take an input, change the instance variable, and then store it in an array. The program is looping so when I input the list of numbers on different runs 3 4 7 2 and then print out all the contents of the array it prints out 3 4 7 2 to be sorted later.

0 个答案:

没有答案