您好我目前正在为Java编程课程完成考试作业。 现在我正在努力使这个程序运行没有错误。 谁能帮助我理解我做错了什么? 我有这个代码,我遇到了NullPointerException:
java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)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
答案 0 :(得分:0)
这是你的代码。我已经标记了编辑。我通常不会做作业问题。但你足够接近。
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
答案 1 :(得分: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