无论何时我运行此循环:
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;
}
}
}
}
我最终得到了这个输出:
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)
第57行是:if (idnumberx.equals(gerbil[c].getId())){
基本上,我的程序要求用户输入x个沙鼠的id。如果其中一个沙鼠id与之前的id匹配,则输出应该是
repeat, try again
这是我的其余代码供参考:
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 + "]";
}
}
答案 0 :(得分:2)
这是你的问题:
Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil = new Gerbil[population]; // reinitilizing array
我认为你的意思是这样的:
Gerbil[] gerbil = new Gerbil[population];
for (int i=0; i<population; i++){
gerbil[i] = new Gerbil(); // initilizing gerbil
答案 1 :(得分:1)
if (idnumberx.equals(gerbil[c].getId()))
您的问题是gerbil[c]
为空。这是因为Java中的数组默认将其所有值初始化为null
。您可以通过检查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;
}
}
答案 2 :(得分:1)
您正在引用一个空数组。沙鼠阵列中没有类Gerbil的对象。因此,您的下面的条件将抛出NullPointerException:
if (idnumberx.equals(gerbil[c].getId())){
我相信您应该更改以下行(49),创建类沙鼠的对象并将其放入数组中。
gerbil = new Gerbil[population];