java - Noob Null Pointer Exception error -


this question has answer here:

i'm getting nullpointerexception error , cant figure out why. i've included 3 classes i'm working on , made ////note eclipse saying error coming in main method (two locations apparently).

i'm new coding , understand happens when trying pass considered null, believe i'm trying pass newly created fish earlier in code. i'm sure error caught 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 new fish tank manager!\n"+                        "what do?\n"+                        "(1)add fish\n(2)remove fish\n"+                        "(3)check tank");               userinput = stdin.nextint();          if (userinput == 1){             fish fish = new fish();              changetank(fish); //// says @ here             continue;         }         else if(userinput ==2){             removefish();         }         else{             checktank();         }     } }  private static void changetank(fish fish){     system.out.println("which tank put fish in? (1-10)");     userinput = stdin.nextint();     tanks[userinput-1].addfish(fish); ////and says @ here }  private static void removefish(){     system.out.println("which tank remove fish from? (1-10)");     userinput = stdin.nextint();     system.out.println("which fish flush down toilet?");     tanks[userinput-1].fishintank();     userinput2 = stdin.nextint();     tanks[userinput-1].flushfish(userinput2-1); }  private static void checktank(){     system.out.println("which tank 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 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 fishes name?");     userinput = stdin.next();     this.name = userinput;      system.out.println("is 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; } } 

tanks created array, without creating fishtank. due this, elements in tanks null. this: tanks[userinput-1].addfish(fish); won't work because tanks[userinput - 1] null. , locations: stacktrace tells methods 1 causing exception. "it happens here" , "also here" "this method calls method throws exception here" , "in method exception thrown here"


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -