eclipse - loop issue for java -
whenever run loop:
system.out.println("how many gerbils in lab?"); int population = keyboard.nextint(); gerbil[] gerbil = new gerbil[population]; (int i=0; i<population; i++){ gerbil = new gerbil[population]; idnumber: (int b = 0; b<population; b++){ system.out.println("what id number of gerbil " + (b+1)); string idnumberx = keyboard.next(); if (b>0){ (int c = 0; c<gerbil.length; c++){ if (idnumberx.equals(gerbil[c].getid())){ system.out.println("repeat, try again"); c--; } else { return; } } } }
i end output:
how many gerbils in lab? 2 id number of gerbil 1 123 id number of gerbil 2 456 exception in thread "main" java.lang.nullpointerexception @ assignment4.assignment4.main(assignment4.java:57)
line 57 this: if (idnumberx.equals(gerbil[c].getid())){
essentially, program asks user input id's x number of gerbils. if 1 of gerbil ids matches previous id, output supposed read
repeat, try again
here rest of code reference:
package assignment4; import java.util.scanner; import java.util.arrays; import assignment4.gerbil; public class assignment4 { public static int population; public static int[] foodeats; public static int types; public static string idnumberx; public static string nicknamex; public static string g; public static string gerbilid; public static gerbil[] gerbil; public static string amountoffoodeaten; public static string gerbilsearch; public static string thisgerbil; public static string foodname; public static int[] totalfood; public static food[] food; public static int maximum; public static boolean bite; public static boolean escape; public static void main(string args[]){ scanner keyboard = new scanner(system.in); system.out.println("how many types of food gerbils eat?"); int f = keyboard.nextint(); food = new food[f]; (int = 0; a<food.length; a++){ system.out.println("name of food number " + (a+1)); foodname = keyboard.next(); system.out.println("max amount of food " + (a+1)); maximum = keyboard.nextint(); food[a] = new food(foodname, maximum); } system.out.println("how many gerbils in lab?"); int population = keyboard.nextint(); gerbil[] gerbil = new gerbil[population]; (int i=0; i<population; i++){ gerbil = new gerbil[population]; idnumber: (int b = 0; b<population; b++){ system.out.println("what id number of gerbil " + (b+1)); string idnumberx = keyboard.next(); if (b>0){ (int c = 0; c<gerbil.length; c++){ if (idnumberx.equals(gerbil[c].getid())){ system.out.println("repeat, try again"); c--; } else { return; } } } } system.out.println("what name gerbil " + (i+1)); string nicknamex = keyboard.next(); foodeats = new int[f]; (int c = 0; c<foodeats.length; c++){ system.out.println("how " + food[c].foodname + " " + nicknamex + " eat"); int gerbileats = keyboard.nextint(); foodeats[c] = gerbileats; if (gerbileats > maximum){ do{ system.out.println("you stoopid, try again"); c--; } while (gerbileats <= maximum); } } (int d = 0; d<population; d++){ system.out.println("does " + nicknamex + " bite? please enter true or false"); string doesitbite = keyboard.next(); if (doesitbite.equalsignorecase("true")){ bite = boolean.parseboolean(doesitbite); break; } else if (doesitbite.equalsignorecase("false")){ bite = boolean.parseboolean(doesitbite); break; } else system.out.println("repeat, try again"); d--; } (int d = 0; d<population; d++){ system.out.println("does " + nicknamex + " escape? please enter true or false"); string doesitescape = keyboard.next(); if (doesitescape.equalsignorecase("true")){ escape = boolean.parseboolean(doesitescape); break; } else if (doesitescape.equalsignorecase("false")){ escape = boolean.parseboolean(doesitescape); break; } else system.out.println("repeat, try again"); d--; } gerbil[i] = new gerbil(idnumberx, nicknamex, foodeats, bite, escape); } while (true){ system.out.println("what know?"); string question = keyboard.nextline(); string search = "search"; string average = "average"; string end = "end"; string restart = "restart"; if (question.equalsignorecase(search)){ new assignment4().searchforgerbil(); } else if (question.equalsignorecase(average)){ //new assignment4().averagefood(); } else if (question.equalsignorecase(end)){ system.exit(0); } else if (question.equalsignorecase(restart)){ new assignment4().main(args); } else system.out.println("try again"); } } public static void searchforgerbil() { system.out.println("please type in gerbil lab id"); scanner keyboard = new scanner(system.in); string searchgerbil = keyboard.next(); boolean found = false; int = 0; int location = 0; (i = 0; <gerbil.length; i++){ if ( searchgerbil.equals(gerbil[i].getid())) { found = true; location = i; break; } else { found = false; } } if (found = true){ system.out.println(gerbil[location]);} else { system.out.println("gerbil " + searchgerbil + " not exist"); } } //return everything; // }
class gerbil
package assignment4; import java.util.arrays; public class gerbil { public static int[] foodeats; public static string idnumberx; public static string nicknamex; public static string gerbilsearch; public static boolean bite; public static boolean escape; public static string foodname; public gerbil(string idnumberx, string nicknamex, int[]foodeats,boolean bite, boolean escape) { this.idnumberx = idnumberx; this.nicknamex = nicknamex; this.foodeats = foodeats; this.escape = escape; this.bite = bite; } public boolean getbite() { return bite; } public boolean getescape() { return escape; } public string getid() { return idnumberx; } public string getname() { return nicknamex; } public void setid(string[] newid) { idnumberx = this.idnumberx; } public void setname(string[] newname) { nicknamex = this.nicknamex; } public string tostring() { return "gerbil [idnumber=" + idnumberx + ", nickname=" + nicknamex + ", totalfood=" + arrays.tostring(foodeats) + ", foodname=" + arrays.deeptostring(foodname) + ", escape=" + escape + ", bite=" + bite + ", foodeats=" + arrays.tostring(foodeats) + ", gerbilsearch=" + gerbilsearch + "]"; } }
this issue:
gerbil[] gerbil = new gerbil[population]; (int i=0; i<population; i++){ gerbil = new gerbil[population]; // reinitilizing array
i think meant this:
gerbil[] gerbil = new gerbil[population]; (int i=0; i<population; i++){ gerbil[i] = new gerbil(); // initilizing gerbil
Comments
Post a Comment