0

I'm getting an NullPointerException error and cant figure out why. I've included all three classes which I'm working on and made ////note where eclipse is saying the error is coming from in the main method (two locations apparently).

I'm new to coding and from what I understand this happens when you are trying to pass something that is considered null, but I believe I'm trying to pass the newly created fish from earlier in the code. I'm sure the error is very easily caught by someone with an experienced eye.

Thank you!

import java.util.Scanner;

public class FishTankManager {

private static Scanner stdin = new Scanner(System.in);
private static int userInput, userInput2;
private static FishTank[] tanks = new FishTank[10];

public static void main (String[] args) {
    while (true){
    System.out.println("Welcome to your new fish tank manager!\n"+
                       "What would you like to do?\n"+
                       "(1)Add a Fish\n(2)Remove a Fish\n"+
                       "(3)Check a tank");          
    userInput = stdin.nextInt();

        if (userInput == 1){
            Fish fish = new Fish(); 
            changeTank(fish); //// Says its at here
            continue;
        }
        else if(userInput ==2){
            removeFish();
        }
        else{
            checkTank();
        }
    }
}

private static void changeTank(Fish fish){
    System.out.println("Which tank would you like to put this fish in? (1-10)");
    userInput = stdin.nextInt();
    tanks[userInput-1].addFish(fish); ////and says its at here also
}

private static void removeFish(){
    System.out.println("Which tank would you like to remove the fish from? (1-10)");
    userInput = stdin.nextInt();
    System.out.println("Which fish would you like to flush down the toilet?");
    tanks[userInput-1].fishInTank();
    userInput2 = stdin.nextInt();
    tanks[userInput-1].flushFish(userInput2-1);
}

private static void checkTank(){
    System.out.println("Which tank would you like to check?");
    userInput = stdin.nextInt();
    tanks[userInput-1].fishInTank();
}
}

public class FishTank {

private Fish[] tank;
private int fishCount = 0;

public FishTank(){
    this.tank = new Fish[5];
    this.fishCount = 0;
}

public void addFish(Fish fish){
    if (this.fishCount >=5 ){
        System.out.println("This tank is full! Try another");
            return;
    }
    else {
        this.tank[fishCount] = fish;
        this.fishCount++;
    }
}

public void fishInTank(){
    for(int i=0; i<5; i++)
        if (this.tank[i] == null){
            continue;
        }
        else{
        System.out.println("("+(i+1)+")"+this.tank[1].getName());
        }
}

public void flushFish(int f){
    this.tank[f] = null;
}
}

import java.util.Scanner;

public class Fish {

private static Scanner stdin = new Scanner(System.in);
private String userInput;
private int userInput2;
private boolean mean;
private String name;

public Fish(){      
    System.out.println("What is your fishes name?");
    userInput = stdin.next();
    this.name = userInput;

    System.out.println("Is this fish aggressive?\n"+
                       "(1)Yes\n(2)No");        
    userInput2 = stdin.nextInt();
    if (userInput2 == 1)
        this.mean = true;
    else
        this.mean = false;
}

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

public boolean getMean(){
    return this.mean;
}
}
DojoOria
  • 141
  • 7

1 Answers1

1

tanks is only created as array, but without creating any FishTank. Due to this, all elements in tanks are null. So this: tanks[userInput-1].addFish(fish); won't work because tanks[userInput - 1] is null. And for the locations: the stacktrace tells you all methods up to the one causing the exception. So "it happens here" and "also here" is actually "this method calls this method which throws the exception here" and "in this method the exception is thrown here"

  • so if i used a for loop (int i = 0; i<10; i++) { tanks[i]= new FishTank } this should fix my problem? because I've declared the array of tanks, but i haven't initialized it? – DojoOria May 14 '15 at 01:31
  • depends upon what you do in the for-loop. simplest approach would be to intialize the array in a for-loop (i guess thats what you mean) like this: `for(int i = 0 ; i < tanks.length ; i++)tanks[i] = new FishTank();` –  May 14 '15 at 01:32
  • awesome! that's what i was editing into my previous comment! This concept is new/confusing to me still.... i feel like because I've declared the array as FishTanks that it should already be comprised of them.... having to individually initialize them is just confusing to me, seems like a double standard. – DojoOria May 14 '15 at 01:34
  • So in my FishTank class I've declared in the constructor that each FishTank() will be comprised of 5 Fish(). Will i need to do a for loop to properly use these as well? – DojoOria May 14 '15 at 01:38
  • nope, actually it makes sense. assume `FishTank` would only provide constructors with one or more arguments that must not be `null`. how should this array be created... . Apart from that you have a lot more options for creating the array, if you have to create the single elements aswell. You'll have to create the `Fish` aswell for the `FishTank` (again with a for-loop) –  May 14 '15 at 01:39
  • Thank you very much for the help! Little by little I'm learning! only 6 weeks into this programming business and still trying to understand the basics! i appreciate it – DojoOria May 14 '15 at 01:45
  • one more thing.. how did you get the `class` names and the for loop earlier with a grey back round? Never mind! got it! – DojoOria May 14 '15 at 01:46
  • well for the learning: good luck and be confident. I've taught myself java, c++ and some other languages, just takes some time. For the formatting: just put the code between two back backquotes (atleast that's what i think they're called - just this here: ` ) –  May 14 '15 at 01:57