So I'm doing a project on Arrays using 2 classes and a tester class but I keep getting a nullPointerException error. I know what the error means essentially and I believe the problem lies in my Tank class provided below and its constructor to be more specific. The Tank class uses my class Fish and is supposed to create an array of fishes, and the tester then creates a new Tank object.
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();
}
}
Here is the Tank class:
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;
}
}
as you can see, I am trying to build an overloaded constructor inside the tank class that creates an Array of fish objects but I'm having some trouble getting it to work. I believe that this is where my error is coming from in my tester class:
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();
}
}
Any help would be appreciated