方法中出现 NullPointerException
import java.util.Random;
import java.util. * ;
public class shooter {
int myHP = 100;
int enHP = 100;
int myBAND = 5;
int enBAND = 5;
int turn = 10;
public void main(String args[]) {
setup();
if (turn % 2 == 0) {
myTurn();
} else {
enTurn();
}
}
public void setup() {
}
public void myTurn() {
System.out.println("You have " + myHP + " HP remaining and the enemy has " + enHP + " HP remaining and you have " + myBAND + " bandages remaining");
System.out.println("Your turn. What would you like to do?");
System.out.println("You can aim for the head(1), the arms(2), the torso(3), or the legs(4). 5 for bandage.");
Scanner scanner = new Scanner(System. in );
int i = scanner.nextInt();
turn++;
if (i == 1) { //Head
Random diceRoller = new Random();
int roll = diceRoller.nextInt(6) + 1;
if (roll == 6) {
System.out.println("Shot at head, hit head.");
enHP -= 70;
if (enHP < 1) {
System.out.println("You Win!");
} else {
System.out.println("Missed.");
}
//set turn to enemy 1?}
} else if (i == 2) { //Arms
Random diceRoller2 = new Random();
int roll2 = diceRoller2.nextInt(6) + 1;
if (roll2 == 6 || roll2 == 5) {
System.out.println("Shot at arms, hit arms.");
enHP -= 40;
if (enHP < 1) {
System.out.println("You Win!");
} else {
System.out.println("Missed.");
}
//set turn to enemy 1?}
} else if (i == 3) { //torso
Random diceRoller3 = new Random();
int roll3 = diceRoller3.nextInt(6) + 1;
if (roll3 == 6 || roll3 == 5 || roll3 == 4) {
System.out.println("Shot at torso, hit torso.");
}
//set turn to enemy 1?}
} else if (i == 4) { //Legs
Random diceRoller4 = new Random();
int roll4 = diceRoller4.nextInt(6) + 1;
if (roll4 == 6 || roll4 == 5) {
System.out.println("Shot at legs, hit legs.");
} else {
System.out.println("Missed.");
}
} else if (i == 5) { // bandages here
System.out.println("You use a bandage.");
myBAND--;
}
}
}
}
public void enTurn() //Enemy turn loop
{
System.out.println("Enemy's turn.");
}
}
这是我的代码,编译得很好。但在我尝试运行它的开始时,它返回给我:
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
import java.util.Random;
import java.util. * ;
public class shooter {
int myHP = 100;
int enHP = 100;
int myBAND = 5;
int enBAND = 5;
int turn = 10;
public void main(String args[]) {
setup();
if (turn % 2 == 0) {
myTurn();
} else {
enTurn();
}
}
public void setup() {
}
public void myTurn() {
System.out.println("You have " + myHP + " HP remaining and the enemy has " + enHP + " HP remaining and you have " + myBAND + " bandages remaining");
System.out.println("Your turn. What would you like to do?");
System.out.println("You can aim for the head(1), the arms(2), the torso(3), or the legs(4). 5 for bandage.");
Scanner scanner = new Scanner(System. in );
int i = scanner.nextInt();
turn++;
if (i == 1) { //Head
Random diceRoller = new Random();
int roll = diceRoller.nextInt(6) + 1;
if (roll == 6) {
System.out.println("Shot at head, hit head.");
enHP -= 70;
if (enHP < 1) {
System.out.println("You Win!");
} else {
System.out.println("Missed.");
}
//set turn to enemy 1?}
} else if (i == 2) { //Arms
Random diceRoller2 = new Random();
int roll2 = diceRoller2.nextInt(6) + 1;
if (roll2 == 6 || roll2 == 5) {
System.out.println("Shot at arms, hit arms.");
enHP -= 40;
if (enHP < 1) {
System.out.println("You Win!");
} else {
System.out.println("Missed.");
}
//set turn to enemy 1?}
} else if (i == 3) { //torso
Random diceRoller3 = new Random();
int roll3 = diceRoller3.nextInt(6) + 1;
if (roll3 == 6 || roll3 == 5 || roll3 == 4) {
System.out.println("Shot at torso, hit torso.");
}
//set turn to enemy 1?}
} else if (i == 4) { //Legs
Random diceRoller4 = new Random();
int roll4 = diceRoller4.nextInt(6) + 1;
if (roll4 == 6 || roll4 == 5) {
System.out.println("Shot at legs, hit legs.");
} else {
System.out.println("Missed.");
}
} else if (i == 5) { // bandages here
System.out.println("You use a bandage.");
myBAND--;
}
}
}
}
public void enTurn() //Enemy turn loop
{
System.out.println("Enemy's turn.");
}
}
That's my code, and compiles fine. But at the beginning of when i try to run it it returns me this:
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该方法
应该是静态的
另请参阅 main 时令人困惑的错误消息方法不是静态的
The method
should be static
Also see Confusing Error Message when main Method not static