所以我正在使用2个类和测试器类在Arrays上做一个项目但是我一直收到nullPointerException错误。我知道错误意味着什么,我相信问题在于我在下面提供的Tank类及其构造函数更具体。 Tank类使用我的类Fish并且应该创建一系列鱼,然后测试者创建一个新的Tank对象。
public class Fish{
private int weight;
private String name;
public Fish(int w, String n){
weight = w;
name = n;
}
public void feed(){
weight = weight + 5;
}
public int getWeight(){
return weight;
}
public void setWeight(int w){
weight = w;
}
public void setName(String n){
name = n;
}
public String getName(){
return name;
}
public void starve(){
weight = weight -5;
}
public String toString(){
return name +"'s weight is: " + getWeight();
}
}
这是Tank类:
import java.util.ArrayList;
public class Tank{
private boolean isFish = true;
private int size;
private int index = 0;
private Fish[] fishArray;
private ArrayList<Fish> fishTank = new ArrayList<Fish>();
public Tank(int i){
fishArray = new Fish[i];
}
public Tank(){
fishArray = new Fish[10];
}
public void feedFish(int i){
fishArray[i].feed();
}
public void addFish(Fish f){
fishArray[index] = f;
size++;
}
public void starveFish(int i){
fishArray[i].starve();
}
public String getNameFor(int i){
return (fishArray[i].getName());
}
public int getWeightFor(int i){
return (fishArray[i].getWeight());
}
public int getTotalWeight(){
int w = 0;
for(int i =0; i< fishArray.length; i++){
w = fishArray[i].getWeight() + w;
}
return w;
}
public Fish getLargest(){
int heaviest = Integer.MIN_VALUE;
for(int i = 0; i < fishArray.length; i++){
if (fishArray[i].getWeight() > heaviest)
heaviest = fishArray[i].getWeight();
index = i;
}
return fishArray[index];
}
public Fish getSmallest(){
int small = Integer.MAX_VALUE;
for(int i = 0; i < fishArray.length; i++){
if (fishArray[i].getWeight() < small)
small = fishArray[i].getWeight();
index=i;
}
return fishArray[index];
}
public boolean doesExist(Fish f){
for(int i = 0; i < fishArray.length; i++){
if(fishArray[i].getWeight() == f.getWeight() && fishArray[i].getName().equalsIgnoreCase(f.getName()))
isFish = true;
else isFish = false;
}
return isFish;
}
public ArrayList<Fish> transfer(){
for(int i = 0; i < fishArray.length; i ++){
fishTank.add(fishArray[i]);}
return fishTank;
}
}
正如你所看到的,我正在尝试在tank类中构建一个重载的构造函数,它创建了一个fish对象的数组,但是我在使它工作时遇到了一些麻烦。我相信这是我的测试人员课程中出现错误的地方:
public class TankTest{
public static void main(String[] args) {
Tank tank = new Tank();
tank.addFish(new Fish(10, "Doris"));
tank.addFish(new Fish(12, "Marlin"));
tank.addFish(new Fish(7, "nemo"));
tank.addFish(new Fish(15, "bubbles"));
tank.addFish(new Fish(18, "gill"));
tank.addFish(new Fish(5, "peach"));
tank.feedFish(3);
tank.starveFish(2);
tank.starveFish(4);
tank.feedFish(5);
tank.starveFish(1);
tank.feedFish(0);
tank.feedFish(2);
tank.starveFish(5);
tank.starveFish(1);
tank.feedFish(3);
tank.feedFish(1);
tank.feedFish(5);
tank.starveFish(0);
System.out.println("The total weight is : " + tank.getTotalWeight());
System.out.println("The largest Fish is : " + tank.getLargest().getName() );
System.out.println("The smallest Fish is : " + tank.getSmallest().getName() );
tank.transfer();
}
}
任何帮助将不胜感激
答案 0 :(得分:0)
我注意到您的addFish
方法存在问题:
public void addFish(Fish f){
fishArray[index] = f;
size++;
}
所以你的代码应该是这样的:
public void addFish(Fish f){
fishArray[index] = f;
index++;
size++;
}
您也应该增加index
变量。目前,您要将所有Fish
对象添加到数组的0位置。
您还应该使用size
变量而不是fishArray.length
来避免到达数组的null元素。 此变量应在构造函数中初始化为0,与index
变量相同。
你已经实例化了6个对象(fish),但是数组有10个元素,其中4个没有被实例化。
public Tank(){
fishArray = new Fish[10];
}
这将create 10 fish
fishArray
fishArray[0] = new fish()
fishArray[1] = new fish()
...
fishArray[5] = new fish();
但是
fishArray[6] = null
fishArray[7] = null
fishArray[8] = null
fishArray[9] = null
尝试将getTotalWeight(), getLargest() and getSmallest()
更改为:
public int getTotalWeight(){
int w = 0;
for(int i =0; i< size; i++){
w = fishArray[i].getWeight() + w;
}
return w;
}
public Fish getLargest(){
int heaviest = Integer.MIN_VALUE;
for(int i = 0; i < size; i++){
if (fishArray[i].getWeight() > heaviest) {
heaviest = fishArray[i].getWeight();
index = i;
}
}
return fishArray[index];
}
public Fish getSmallest(){
int small = Integer.MAX_VALUE;
for(int i = 0; i < size; i++){
if (fishArray[i].getWeight() < small) {
small = fishArray[i].getWeight();
index=i;
}
}
return fishArray[index];
}
&#13;