Why am I receiving a NullPointerException during compilation?

539 views Asked by At

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
2

There are 2 answers

0
Vaibhav Tekade On
    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
0
DejaVuSansMono On

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