gpt4 book ai didi

java - Noob 空指针异常错误

转载 作者:行者123 更新时间:2023-12-01 11:33:35 26 4
gpt4 key购买 nike

我收到 NullPointerException 错误,但无法找出原因。我已经包含了我正在处理的所有三个类,并////note eclipse 说错误来自主方法(显然是两个位置)。

我是编码新手,据我了解,当您尝试传递被视为空的内容时,会发生这种情况,但我相信我正在尝试传递代码早期新创建的鱼。我相信有经验的人很容易发现这个错误。

谢谢!

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;
}
}

最佳答案

tanks 仅创建为数组,但不创建任何 FishTank。因此,tanks 中的所有元素均为 null。因此,tanks[userInput-1].addFish(fish); 不起作用,因为 tanks[userInput - 1]null。对于位置:堆栈跟踪会告诉您直到导致异常的方法为止的所有方法。所以“它发生在这里”和“也在这里”实际上是“这个方法调用这个方法,在这里抛出异常”和“在这个方法中,异常在这里抛出”

关于java - Noob 空指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com