-4
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)
nickb
  • 59,313
  • 13
  • 108
  • 143
  • 6
    there is no valid main method. static is missing for the one you wrote – Jayy Feb 19 '12 at 05:07
  • Not to mention the code formatting is poor making the code very difficult for us to read and debug. Surely your code isn't always fully left justified. – Hovercraft Full Of Eels Feb 19 '12 at 05:08
  • 2
    Also the exception does not have anything to do with the code shown above... – havexz Feb 19 '12 at 05:46
  • Possibly related to this previous question as the error is exactly the same: http://stackoverflow.com/q/7911004/996405 – Kavka Feb 19 '12 at 06:07
  • For a starter, I think u're great,.. But First Read **Clean Code**, and your code will be beautiful. For your problem, main function should be static. If you're beginner, don't ask why. If you're in the next stage, read this one [Why is the java main method static](http://stackoverflow.com/questions/146576/why-is-the-java-main-method-static) – Yeo Feb 19 '12 at 06:55

1 Answers1

0

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

Kavka
  • 4,191
  • 16
  • 33