-1

Hello I am currently working on a assignment for an exam in my Java programming course. Right now I am trying to make this program run without errors. Can anyone please help me to understand what I am doing wrong? I have this code and I get stuck with an NullPointerException:

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:272)

import java.util.Random;
import java.util.Scanner;

public class BattleShip {

    // Board size
    int boardxlength = 5;
    int boardylength = 5

    //ships 
    Ship submarine;
    Ship destroyer;
    Ship battleship;

    // Random number function
    Random random = new Random();

    // Begin Main
    public void main(String args[]) {

        // create Ships
        SetupShips();


        System.out.println(submarine.length);

    } // end Main function  

    // Create Ships function 
    public void SetupShips() {
        submarine = new Ship("submarine", random.nextInt(boardxlength), random.nextInt(boardylength), 2);
        destroyer = new Ship("destroyer", random.nextInt(boardxlength), random.nextInt(boardylength), 3);
        battleship = new Ship("battleship", random.nextInt(boardxlength), random.nextInt(boardylength), 4);
    }

    /**
     * **************************************
     */
       /*   CLASSES
      /******************************************/
    public class Ship {
        String type;
        int row;
        int col;
        int length;

        //Constructor
        public Ship(String strtype, int intx, int inty, int intlength) {
            type = strtype;
            row = intx - 1;
            col = inty - 1;
            length = intlength;
        }
    } // end Ship Class
}// end main class
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Kevin
  • 17
  • 1
  • 1
    And, to begin with, probably you want `main` to be `static`. And that will force you to instantiate `Battleship` inside `main` – SJuan76 Nov 12 '14 at 19:12
  • First look, you might need to change a lot to static. – WannaBeCoder Nov 12 '14 at 19:13
  • Additionally, you need to declare submarine, destroyer, and battleship as static Ship. The NPE is not really happenning while running your code; it's happening in the compiler, likely because of an improper main() method signature, as @SJuan76 said. – TubaBen Nov 12 '14 at 19:13
  • http://stackoverflow.com/questions/15057203/java-null-pointer-exception-in-constructor-call – zmf Nov 12 '14 at 19:14
  • You're also missing a semicolon on `int boardylength = 5`. At least fix the simple compiler errors before posting... – Mike Ounsworth Nov 12 '14 at 19:15
  • look at this http://ideone.com/3wUNju @Kevin – WannaBeCoder Nov 12 '14 at 19:17
  • 1
    @MikeOunsworth, I don't think OP can see (and then correct) the compiler errors, since compiler is throwing a NPE :-) – TubaBen Nov 12 '14 at 19:19
  • @Jon Skeet I don't think http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it contains the answer to the question, as the NullPointerException occurs due to the DrJava environment invoking the Java compiler and is not actually happening in the code itself. I'm not sure what causes it exactly as the code has various issues, but one guess is, it might be the lack of a _static_ main method. – godfatherofpolka Nov 12 '14 at 19:36
  • @godfatherofpolka: Yes, you're right. I've reopened. – Jon Skeet Nov 12 '14 at 19:45
  • Huh, I copy & paste'd his code into my compiler and it complained about missing semicolon and nook-static main. I'm just commenting on what I saw... – Mike Ounsworth Nov 12 '14 at 19:51

2 Answers2

0

Here is your code. I have marked the edits. I don't usually do homework problems. But you were close enough.

import java.util.Random;
import java.util.Scanner;

public class BattleShip {

// Board size
int boardxlength = 5;
int boardylength = 5;

//ships 
Ship submarine;
Ship destroyer;
Ship battleship;

// Random number function
Random random = new Random();

// Begin Main 
// Main methods have to be static
public static void main(String args[]) {

    // create Ships

    BattleShip shipGame = new BattleShip();

    shipGame.setUpShips();
    //The problem was here. where you tried to statically call 
    //a non static variable
    System.out.println(shipGame.submarine.length);

} // end Main function  

// Create Ships function 
public void setUpShips() {
    submarine = new Ship("submarine", random.nextInt(boardxlength), random.nextInt(boardylength), 2);
    destroyer = new Ship("destroyer", random.nextInt(boardxlength), random.nextInt(boardylength), 3);
    battleship = new Ship("battleship", random.nextInt(boardxlength), random.nextInt(boardylength), 4);
}

/**
 * **************************************
 */
   /*   CLASSES
  /******************************************/
public class Ship {
    String type;
    int row;
    int col;
    int length;

    //Constructor
    public Ship(String strtype, int intx, int inty, int intlength) {
        type = strtype;
        row = intx - 1;
        col = inty - 1;
        length = intlength;
    }
} // end Ship Class
}// end main class
DejaVuSansMono
  • 787
  • 5
  • 14
0
    import java.util.Random;

    public class BattleShip {

   // Board size
    int boardxlength = 5;
    int boardylength = 5;

  // ships
  Ship submarine;
  Ship destroyer;
  Ship battleship;

  // Random number function
  Random random = new Random();

  // Begin Main
  public static void main(String args[]) {

    // create Ships
    BattleShip battleShip = new BattleShip();
    battleShip.SetupShips();

    System.out.println(battleShip.submarine.length);

  } // end Main function

  // Create Ships function
  public void SetupShips() {
    submarine = new Ship("submarine", random.nextInt(boardxlength),
        random.nextInt(boardylength), 2);
    destroyer = new Ship("destroyer", random.nextInt(boardxlength),
        random.nextInt(boardylength), 3);
    battleship = new Ship("battleship", random.nextInt(boardxlength),
        random.nextInt(boardylength), 4);
  }

  /**
   * **************************************
   */
  /*
   * CLASSES /*****************************************
   */
  public class Ship {
    String type;
    int row;
    int col;
    int length;

    // Constructor
    public Ship(String strtype, int intx, int inty, int intlength) {
      type = strtype;
      row = intx - 1;
      col = inty - 1;
      length = intlength;
    }
  } // end Ship Class
}// end main class