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