New to java: I'm not sure how to store the objects in the arrays after it is created. The program gets rid of it, making it null

Viewed 70

The main objective was to have a lake and add fish objects to it, which have details about their weight etc. But however, every time a new fish is created, the previous object appears as null instead of its location, which I could use my getName to get the name.

package com. company;

public class Main {

    public static void main(String[] args) {
        Lake l = new Lake("jersey", 1, 2, 3,4 );
        l.addFish("carp", 20, 1);
        l.addFish("josh",20,1);

        Lake n = new Lake("york",1,2,4,20);
        n.addFish("catfish",20,30);
        n.addFish("tot",20,30);
        n.addFish("salmon",20,30);
        n.addFish("shrimp",20,30);
        n.addFish("swordfish",20,30);


    }
}
package com. company;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Lake {
    private String name;
    private double length;
    private double width;
    private double depth;
    private final int amountOfFish;
    private int currentFishAmount = 0 ;

    Lake(String name, double length, double width, double depth, int amountOfFish)
    {
        this.name = name;
        this.length = length;
        this.width = width;
        this.depth = depth;
        this.amountOfFish = amountOfFish;
    }

    public void addFish (String name, double length, double weight)
    {
        Fish fishInLake []  = new Fish[amountOfFish];
        fishInLake[currentFishAmount]= new Fish(name, length, weight);
        System.out.println("Lake: " + this.name +  "|" + "Fish:" + ""+ fishInLake[currentFishAmount].getName() + fishInLake[currentFishAmount].getLength());
        System.out.println(currentFishAmount);
        currentFishAmount++;
        System.out.println(Arrays.toString(fishInLake));
    }


}
package com.company;

import java.lang.reflect.Array;

public class Fish {
    private String name;
    private double length;
    private double weight;


    Fish (String name, double length, double weight){
        this.name = name;
        this.length = length;
        this.weight = weight;
    }

   public String getName(){
        return this.name;
   }

   public double getLength(){
        return this.length;
   }

   public double getWeight(){
        return this.weight;
   }

}

The main objective was to have a lake and add fish objects to it, which have details about their weight etc. But however, every time a new fish is created, the previous object appears as null instead of its location, which I could use my getName to get the name.

2 Answers

First off, to make your output a bit more readable, add a toString() method in your Fish class:

 public String toString(){
    return this.name;
  }

Next, since you want the array to be saved every time a new fish is added to the lake, we want to make the array a private instance variable, and then initialize it in our constructor:

package com. company;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Lake {
    private String name;
    private double length;
    private double width;
    private double depth;
    private final int amountOfFish;
    private int currentFishAmount = 0 ;
    private Fish[] fishInLake;

    Lake(String name, double length, double width, double depth, int amountOfFish)
    {
        this.name = name;
        this.length = length;
        this.width = width;
        this.depth = depth;
        this.amountOfFish = amountOfFish;
        this.fishInLake = new Fish[amountOfFish];
    }

    public void addFish (String name, double length, double weight)
    {
        fishInLake[currentFishAmount]= new Fish(name, length, weight);
        System.out.println("Lake: " + this.name +  "|" + "Fish:" + ""+ fishInLake[currentFishAmount].getName() + fishInLake[currentFishAmount].getLength());
        System.out.println(currentFishAmount);
        currentFishAmount++;
        System.out.println(Arrays.toString(fishInLake));
    }
}

better to create a static method or a static Array, otherwise every time that tries to insert data, the previous data will be disappearing, with a static Array the whole data will be there until program execution finish.

Related