“线程中的异常”;“主要”;java上的java.lang.NullPointerException错误

“线程中的异常”;“主要”;java上的java.lang.NullPointerException错误,java,nullpointerexception,Java,Nullpointerexception,当我试图创建一个对象数组时,我在Java程序中遇到了一个问题。 线程“main”java.lang.NullPointerException错误中的异常就是我得到的错误。 我真的需要一些帮助,因为我在这里被困了几个小时 谢谢你的帮助 public class Placement extends JFrame { private JPanel placementPanel; private JFrame gameEngineFrame; private Ship

当我试图创建一个对象数组时,我在Java程序中遇到了一个问题。 线程“main”java.lang.NullPointerException错误中的异常就是我得到的错误。 我真的需要一些帮助,因为我在这里被困了几个小时

谢谢你的帮助

public class Placement extends JFrame  {
    private JPanel placementPanel;

    private JFrame gameEngineFrame;




    private Ship[] boardShips; //Array of ships objects to save ships
    private JButton[] shipButton; //Array of buttons for the ships
    private JLabel selectShipLabel;
    private Ship shipSelected;
    private Ship highlightedShip;





        JPanel shipSelect = new JPanel();
        shipSelect.setLayout(new GridLayout(14, 1));
        shipSelect.setBackground(Color.getHSBColor(0.0F, 0.0F, 0.75F)); //Setting background color. Hue, saturation, brightness format. (HSB)
        shipSelect.setBounds(320, 20, 200, 300); //Setting postiion of the panel inside the JPanel and setting width & height
        this.placementPanel.add(shipSelect);     



     makeShips(shipSelect);

  private void makeShips(JPanel inPanel)
  {
      System.out.println("make ships testing");
    this.shipButton[0] = new JButton("Aircraft Carrier");
    boardShips[0] = new Ship(5, "Aircraft Carrier", 0);

    this.shipButton[1] = new JButton("Battleship");
    boardShips[1] = new Ship(4, "Battleship", 1);

    this.shipButton[2] = new JButton("Cruiser");
  boardShips[2] = new Ship(3, "Cruiser", 2);

    this.shipButton[3] = new JButton("Destroyer 1");
    boardShips[3] = new Ship(2, "Destroyer", 3);


    this.shipButton[4] = new JButton("Submarine 1");
    boardShips[4] = new Ship(1, "Submarine", 4);

  System.out.println("make ships testing22");

    for (int i = 0; i < 5; i++)
    {
      this.shipButton[i].setName("" + i);
     this.shipButton[i].addActionListener((ActionListener) this);
      inPanel.add(this.shipButton[i]);
      this.boardShips[i].makeIcons();
    }

  }

您有这个
私有JButton[]shipputton
这是Jbutton的数组,在任何地方都没有初始化,您正在尝试这样做原因NPE

我看到你有5个舰载按钮阵列

  JButton[] shipButton=new JButton[5];
编辑


不能从类主体调用方法。您只能在那里定义方法。要调用您的方法,您必须使用构造函数或通过其他方法。

因此,当某些内容未初始化时,您会出现此错误?我修正了我问的错误,但是我在makeShips(shipSelect)上得到了一个新的NPE;命令
  JButton[] shipButton=new JButton[5];