我认为您想移除一条鱼,一旦该鱼种被全部捞出,fish[num] == 0,然后移除这条鱼. public Fish catchAFish(){int num= (int)(Math.random() * (numFish-1));if(fish[num]!=null &&numFish>0){fff=鱼[数量];鱼[数量]--;如果(鱼[数量] == 0){//不再有这种鱼,将其从池塘中移除.鱼[num]=鱼[numFish-1];鱼[numFish-1]=空;numFish--;}返回 fff;}别的返回空;}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. 解决方案 catchAFish() is returning null and so your likes() is getting a null reference in likes when you call if(f1.getSpecies()=="Sunfish") because you are passing in if(likes(p.catchAFish())){ // the problem happens here.I don't see how goFishingIn() is is called, you don't supply that code. But you are most likely calling it with an sad empty pond.You either need to check for null before using the return value of catchAFish, or ensure that catchAFish will never return null. Right now it will.Okay - I see your main function. The problem is you are out fishing your pond. 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; }You initalize your array to 20, but each time you fish you are removing a whole Fish class, so when you call goFishingIn you are removing one Fish object from your fish array in pond.I think you want to remove a single fish, and once that fish species is all fished out fish[num] == 0, then remove the fish. public Fish catchAFish(){ int num= (int)(Math.random() * (numFish-1)); if(fish[num]!=null && numFish>0){ fff= fish[num]; fish[num]--; if (fish[num] == 0) { // No more of this fish type, remove it from the pond. fish[num]= fish[numFish-1]; fish[numFish-1]=null; numFish--; } return fff; } else return null; } 这篇关于初始化所有变量但仍然得到 NullPointerExceptions 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-15 10:28