方法中出现 NullPointerException

发布于 2025-01-07 03:33:57 字数 3352 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2025-01-14 03:33:57

该方法

public void main(String args[]) 

应该是静态的

public static void main(String args[]) 

另请参阅 main 时令人困惑的错误消息方法不是静态的

The method

public void main(String args[]) 

should be static

public static void main(String args[]) 

Also see Confusing Error Message when main Method not static

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文