Reputation: 21
whenever i run this loop:
System.out.println("How many gerbils are in the lab?");
int population = keyboard.nextInt();
Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil = new Gerbil[population];
idnumber:
for
(int b = 0; b<population; b++){
System.out.println("What is the id number of gerbil " + (b+1));
String idnumberx = keyboard.next();
if (b>0){
for (int c = 0; c<gerbil.length; c++){
if (idnumberx.equals(gerbil[c].getId())){
System.out.println("Repeat, try again");
c--;
}
else {
return;
}
}
}
}
i end up with this output:
How many gerbils are in the lab?
2
What is the id number of gerbil 1
123
What is the id number of gerbil 2
456
Exception in thread "main" java.lang.NullPointerException
at assignment4.Assignment4.main(Assignment4.java:57)
line 57 is this: if (idnumberx.equals(gerbil[c].getId())){
essentially, my program asks the user to input id's for x number of gerbils. if one of the gerbil ids matches a previous id, the output is supposed to read
repeat, try again
here is the rest of my code for 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 do the gerbils eat?");
int F = keyboard.nextInt();
food = new food[F];
for
(int a = 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 are in the lab?");
int population = keyboard.nextInt();
Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil = new Gerbil[population];
idnumber:
for
(int b = 0; b<population; b++){
System.out.println("What is the id number of gerbil " + (b+1));
String idnumberx = keyboard.next();
if (b>0){
for (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 is the name for gerbil " + (i+1));
String nicknamex = keyboard.next();
foodeats = new int[F];
for
(int c = 0; c<foodeats.length; c++){
System.out.println("how much " + food[c].foodname + " does " + nicknamex + " eat");
int gerbileats = keyboard.nextInt();
foodeats[c] = gerbileats;
if (gerbileats > maximum){
do{
System.out.println("You stoopid, try again");
c--;
}
while (gerbileats <= maximum);
}
}
for
(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--;
}
for
(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 would you like to 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 a gerbil lab ID");
Scanner keyboard = new Scanner(System.in);
String searchgerbil = keyboard.next();
boolean found = false;
int i = 0;
int location = 0;
for (i = 0; i <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 + " does 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 + "]";
}
}
Upvotes: 0
Views: 70
Reputation: 2911
You are referencing an empty array. There is no object of class Gerbil in the gerbil array. So, your condition below will throw a NullPointerException:
if (idnumberx.equals(gerbil[c].getId())){
I believe you should change the following line (49), create an object of class gerbil and put it in the array.
gerbil = new Gerbil[population];
Upvotes: 1
Reputation: 13596
if (idnumberx.equals(gerbil[c].getId()))
Your problem is that gerbil[c]
is null. This is because arrays in Java initialize all their values to null
by default. You can fix this by checking for null.
for (int c=0;c<gerbil.length;c++) {
if (gerbil[c] == null) // Tests if null.
break; // If null exit loop.
if (idnumberx.equals(gerbil[c].getId())) {
System.out.println("Repeat, try again");
c--;
} else {
return;
}
}
Upvotes: 1
Reputation: 3797
This is your issue:
Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil = new Gerbil[population]; // reinitilizing array
I think you meant something like this:
Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil[i] = new Gerbil(); // initilizing gerbil
Upvotes: 2