0

When I attempt to compile my code, this error occurs:

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

When I tried to run it, this error message occurs:

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

I'm pretty sure the error is in the stoockUp method, but I don't understand what could be wrong with it.

Here is my code:

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);
  }

}
}

And:

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++;
    }
  }
}
}

I would appreciate an explanation, because I am thoroughly confused, and just want to see if it'll work properly.

1 Answers1

0

The problem is that you never initialize fishArr in Habitat

And the unchecked operation warning might come from public ArrayList catchFish(Fish f){, you should parameterize your return type

zibi
  • 2,709
  • 3
  • 24
  • 44
  • Doesn't `int[] fishArr;` initialize it? –  Feb 24 '14 at 22:15
  • No, this just defines variable and sets it to null, maybe you can read more in http://stackoverflow.com/questions/1200621/how-to-declare-an-array-in-java don't forget to rate my answer:) Also, personaly I would suggest to use List/ArrayList instead, as you will not have to play with its size. – zibi Feb 24 '14 at 22:17
  • How do I parameterize? `return fishArr;` ? I changed fishArr to an ArrayList `ArrayList fishArr = new ArrayList();` but isn't that the paramter of fishArr? –  Feb 24 '14 at 22:51
  • change `public ArrayList catchFish(Fish f){` to `public ArrayList catchFish(Fish f){` – zibi Feb 25 '14 at 08:16