在Java中解决Null Point异常

时间:2014-07-12 11:42:35

标签: java eclipse swing netbeans import

我正在

**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) {
        }
}

播放器类的代码:

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中运行这个程序,它使用按钮来输入。

1 个答案:

答案 0 :(得分:2)

问题在于此代码

 buttonBoat[i][j]=new JButton();

在使用之前,您尚未初始化buttonBoat数组。