有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java未经检查或编译时不安全的操作,在尝试运行时异常

当我试图编译代码时,会出现以下错误:

Note: GoFishEdited.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

当我试图运行它时,出现以下错误消息:

Exception in thread "main" java.lang.NullPointerException
at Habitat.stockUp(Habitat.java:20)
at GoFishEdited.main(GoFishEdited.java:14)

我很确定这个错误在Stockup方法中,但我不明白它可能有什么问题

这是我的代码:

public class GoFishEdited {
public static void main(String[] args) {

  System.out.println("\nProject 1, Stage 3\n");

  int[] fishArray;
  ArrayList<Fish> catchF = new ArrayList<Fish>();

  Habitat h1 = new Habitat();
  Habitat h2 = new Habitat();

  fishArray = h1.stockUp();     

  System.out.println("Start with some weights:");  
  for (int i : fishArray) {
     System.out.print(i + " ");
  }

  System.out.println("\n\nMake fish of those weights.\n");

  Fish[] fishGroup = new Fish[fishArray.length]; // array of Fish

  for (int i=0; i < fishArray.length; i++) {
     fishGroup[i] = new Fish(fishArray[i]);  // make fish 
  }

  System.out.println("Fish are made.  Now put them in a habitat:\n");

  for (Fish f : fishGroup) { 
     h1.addFish(f);
  }

  System.out.println("\nAll in. The habitat displays them:\n");
  h1.printFish();

  System.out.println("\nMove some fish to the second habitat.\n");
  for(Fish f : fishGroup){
     h2.addFish(f);
  }

  System.out.println("\nPrint both habitats:\n");
  h1.printFish();
  h2.printFish();

  System.out.println("\nCatch some fish.\n");
  for(Fish f : fishGroup){
     catchF = h1.catchFish(f);
  }

}
}

以及:

public class Habitat {

ArrayList<Fish> stringer = new ArrayList<Fish>();
int[] fishArr;
public int maxCount=25; 
public int minCount=9; 
public int maxWeight=10; 
public int minWeight=1; 
public int catchProbability=30; //0.3 
public ArrayList<Fish> catches = new ArrayList<Fish>();

public int[] stockUp(){

  int numofF = minCount + (int)(Math.random() * ((maxCount - minCount) + 1));

  for(int i = 0; i<numofF; i++){
     fishArr[i] = minWeight + (int)(Math.random() * ((maxWeight - minWeight) + 1));


  }

  return fishArr;
}


public Habitat(){

  int[] hab;

}

public void addFish(Fish f) {
  stringer.add(f);
}

 public void removeFish(Fish f){
   stringer.remove(f);
 }

 public void printFish(){
     System.out.println(stringer);
  }

 public ArrayList catchFish(Fish f){

  int caught = 0 + (int)(Math.random() * ((100 - 0) + 1));

  if(caught < catchProbability){

     catches.add(f);
     stringer.remove(f);

  }

  return catches;

}

public void printGrid(int[][] twoD){
  int i, j;        

  for ( i = 0 ; i < twoD.length ; i++ ){
     for ( j = 0 ; j < twoD[i].length ; j++ ){
        System.out.print( twoD[i][j] + "   " );
     }
     System.out.println();
  }
}

public void toGrid(String[] oneD){
  int cols = (int) Math.floor(Math.sqrt(oneD.length));
  int currentCol = 0;
  for(String element : oneD) {
    System.out.print(element + "\t");
    if(currentCol >= cols) {
        System.out.println("");
        currentCol = 0;
    }
    else {
        currentCol++;
    }
  }
}
}

我希望你能给我一个解释,因为我完全糊涂了,只想看看它是否能正常工作


共 (0) 个答案