Java 为什么我在编译期间收到NullPointerException?

Java 为什么我在编译期间收到NullPointerException?,java,class,drjava,Java,Class,Drjava,您好,我目前正在为Java编程课程的考试做作业。 现在,我正在努力使这个程序运行没有错误。 谁能帮我理解我做错了什么? 我有这段代码,我被一个NullPointerException卡住了: 位于的java.lang.NullPointerException sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.Del

您好,我目前正在为Java编程课程的考试做作业。 现在,我正在努力使这个程序运行没有错误。 谁能帮我理解我做错了什么? 我有这段代码,我被一个NullPointerException卡住了:

位于的java.lang.NullPointerException sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.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 
// 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

首先,您可能希望
main
成为
static
。这将迫使您在
main
内部实例化
Battleship
。首先,您可能需要将许多内容更改为静态。此外,您需要将潜艇、驱逐舰和战列舰声明为静态舰。运行代码时,NPE并没有真正发生;它发生在编译器中,可能是因为不正确的main()方法签名,正如@SJuan76所说的那样。@MikeOunsworth,我认为OP无法看到(然后纠正)编译器错误,因为编译器抛出了一个NPE:-)
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
    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