我得到
**Exception in thread "main" java.lang.NullPointerException
at sources.Own_gui.<init>(Own_gui.java:32)
at sources.Maingui.main(Maingui.java:6)**
错误,当我做我的项目。我试图弄明白,但没有成功。
own_gui类的代码: package sources;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Own_gui extends JFrame implements ActionListener
{
JFrame frameOwn;
JPanel panelHit,panelPlace,panelButtons,panelTable;
JButton buttonBoat[][],buttonAttack[][];
public Own_gui()
{
this.setLayout(new GridLayout(2,2));
this.setTitle("First Player");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
panelButtons=new JPanel();
panelHit=new JPanel();
panelPlace=new JPanel();
panelTable=new JPanel();
panelHit.setLayout(new GridLayout(10,10));
panelPlace.setLayout(new GridLayout(10,10));
panelButtons.setLayout(new GridLayout(3,2));
for(int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
buttonBoat[i][j]=new JButton();
buttonAttack[i][j]=new JButton();
panelPlace.add(buttonBoat[i][j]);
panelHit.add(buttonAttack[i][j]);
buttonBoat[i][j].setActionCommand(i+","+j);
buttonBoat[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setActionCommand(i+","+j);
buttonAttack[i][j].setEnabled(false);
buttonBoat[i][j].addActionListener(this);
buttonAttack[i][j].addActionListener(this);
}
}
this.add(panelButtons);
this.add(panelHit);
this.add(panelPlace);
this.add(panelTable);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
playercui类的代码:
package sources;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class playergui extends JFrame implements ActionListener
{
JFrame framePlayer;
JPanel panelHit,panelPlace,panelButtons,panelTable;
JButton buttonBoat[][],buttonAttack[][];
public playergui()
{
this.setLayout(new GridLayout(2,2));
this.setTitle("Second Player");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
panelButtons=new JPanel();
panelHit=new JPanel();
panelPlace=new JPanel();
panelTable=new JPanel();
panelHit.setLayout(new GridLayout(10,10));
panelPlace.setLayout(new GridLayout(10,10));
panelButtons.setLayout(new GridLayout(3,2));
for(int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
buttonBoat[i][j]=new JButton();
panelPlace.add(buttonBoat[i][j]);
buttonAttack[i][j]=new JButton();
panelHit.add(buttonAttack[i][j]);
buttonBoat[i][j].setActionCommand(i+","+j);
buttonBoat[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setActionCommand(i+","+j);
buttonAttack[i][j].setEnabled(false);
buttonBoat[i][j].addActionListener(this);
buttonAttack[i][j].addActionListener(this);
}
}
this.add(panelButtons);
this.add(panelHit);
this.add(panelPlace);
this.add(panelTable);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
主类
代码package sources;
public class Maingui
{
public static void main(String []args)
{
Own_gui owg=new Own_gui();
playergui pgi= new playergui();
}
}
我在netbeans中运行这个程序,它使用按钮来输入。
这段代码有问题
buttonBoat[i][j]=new JButton();
在使用buttonBoat数组之前还没有初始化它