Почему я получаю исключение NullPointerException во время компиляции?
Здравствуйте! В настоящее время я работаю над заданием для экзамена по курсу программирования Java. Сейчас я пытаюсь заставить эту программу работать без ошибок. Может кто-нибудь помочь мне понять, что я делаю не так? У меня есть этот код, и я застреваю с NullPointerException:
java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0 (нативный метод) в sun.reflect.NativeMethodAccessorImpl.invoke(неизвестный источник) в sun.reflect.DelegatingMethodAccessorImpl.indref. invoke (Неизвестный источник) по адресу 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 ответа
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
Вот твой код. Я отметил изменения. Я обычно не делаю домашних заданий. Но вы были достаточно близко.
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