Here is the code:
public class Fisher{
private String name;
private Fish [] fishCaught;
private int numFishCaught;
private int keepSize;
public static int LIMIT =10;
private boolean full;
public int getnumFishCaught(){
return numFishCaught;
}
public Fisher(String n,int k){
this.name=n;
this.keepSize=k;
this.fishCaught =new Fish[LIMIT];
}
public String toString(){
return this.name + " with " + numFishCaught+ " fish ";
}
public void keep(Fish f){
if(numFishCaught<LIMIT){
this.fishCaught[numFishCaught]=f;
numFishCaught++;
full=false;
}else{
full=true;
numFishCaught=LIMIT;
}
}
public boolean likes(Fish f1){
if(f1.getSpecies().equals("Sunfish")){
return false;
}else{
if(f1.getSize()>=this.keepSize){
return true;
}else{
return false;
}
}
}
public void listFish(){
System.out.println( this.name +" with "+ numFishCaught + " fish as follow: ");
for(int i=0;i<numFishCaught;i++){
System.out.println("A " + this.fishCaught[i].getSize() +" cm " + this.fishCaught[i].getSpecies());
}
}
public void goFishingIn(Pond p){
if(likes(p.catchAFish())){ // the problem happens here
keep(p.catchAFish());
if(full==true)
p.add(p.catchAFish());
}
}
public void giveAwayFish(Fisher f1,Pond pp){
this.keepSize=f1.keepSize;
for(int i=0;i<numFishCaught;i++){
if(likes(fishCaught[i])==true){
f1.keep(fishCaught[i]);
}else{
pp.add(fishCaught[i]);
}
}
}
}
And the other two classes, Fish and Pond, which are:
public class Pond {
private Fish [] fish;
private int numFish;
private int capacity;
private boolean full;
private Fish fff;
public Pond(int c){
capacity=c;
fish= new Fish[capacity];
fff= new Fish(0,"UNKNOWN");
}
public boolean isFull(){
if(numFish>capacity){
full=true;
return true;
}
else{
full=false;
return false;
}
}
public int getNumFish(){
return numFish;
}
public void add(Fish f){
if(full==false){
fish[numFish]=f;
numFish++;
}
}
public void listFish(){
System.out.println("Pond with " + numFish + " fish as follow: ");
for(int i=0;i<numFish;i++){
System.out.println("A " + fish[i].getSize() +" cm " + fish[i].getSpecies());
}
}
public Fish catchAFish(){
int num= (int)(Math.random() * (numFish-1));
if(fish[num]!=null && numFish>0){
fff= fish[num];
fish[num]= fish[numFish-1];
fish[numFish-1]=null;
numFish--;
return fff;
}
else
return null;
}
public String toString(){
return "Pond with "+ numFish +" "+ fish[numFish];
}
}
Stacktrace:
java.lang.NullPointerException
at Fisher.likes(Fisher.java:34)
at Fisher.goFishingIn(Fisher.java:53)
at FishingTestProgram2.main(FishingTestProgram2.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Could anyone tell me why p.catchAFish()
is not the same type in likes(Fish f1)
?
because I think catchAFish
method will return a Fish object which is same as the input type in likes
method.
public class Fish {
private String species;
private int size;
public String getSpecies(){
return species;
}
public int getSize(){
return size;
}
public Fish(int s, String ss){
size=s;
species=ss;
}
public String toString(){
return "A " + size+"cm "+ species;
}
}
Test class:
public class FishingTestProgram2 {
public static void main(String [] args) {
// Create a big pond with 15 fish
Pond bigPond = new Pond(15);
bigPond.add(new Fish(4, "Sunfish"));
bigPond.add(new Fish(25, "Pike"));
bigPond.add(new Fish(20, "Bass"));
bigPond.add(new Fish(30, "Perch"));
bigPond.add(new Fish(14, "Sunfish"));
bigPond.add(new Fish(15, "Pike"));
bigPond.add(new Fish(9, "Pike"));
bigPond.add(new Fish(12, "Bass"));
bigPond.add(new Fish(5, "Sunfish"));
bigPond.add(new Fish(12, "Sunfish"));
bigPond.add(new Fish(10, "Bass"));
bigPond.add(new Fish(2, "Bass"));
bigPond.add(new Fish(16, "Perch"));
bigPond.add(new Fish(30, "Sunfish"));
bigPond.add(new Fish(7, "Perch"));
bigPond.listFish();
// Create two people to fish in the pond
Fisher fred = new Fisher("Fred", 15);
Fisher suzy = new Fisher("Suzy", 10);
System.out.println("First Fred catches 20 fish in the big pond ...");
for (int i=0; i<20; i++)
fred.goFishingIn(bigPond);
fred.listFish();
System.out.println("Suzy now catches 20 fish in the big pond ...");
for (int i=0; i<20; i++)
suzy.goFishingIn(bigPond);
suzy.listFish();
System.out.println("Here is what is left of the pond ...");
bigPond.listFish();
// Now simulate Suzy giving her fish to Fred
suzy.giveAwayFish(fred, bigPond);
fred.listFish();
suzy.listFish();
bigPond.listFish();
}
}
The string compare line was right I change it whileI was trying to solve the problem. The debugging says this. means Fish and p. means (int, string) I need to initialize these two thing in same type, but I don't know how to change it.