-3

I am getting

**Exception in thread "main" java.lang.NullPointerException
    at sources.Own_gui.<init>(Own_gui.java:32)
    at sources.Maingui.main(Maingui.java:6)**

error when i am doing my project. I tried to figure it out but didnt succeeded.

Code for own_gui class:

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

code for playercui class:

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

}

code for main class

package sources;
public class Maingui
{
public static void main(String []args)
{
    Own_gui owg=new Own_gui();
    playergui pgi= new playergui();
}
}

i am running this program in netbeans and it uses buttons to take input .

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Zayant
  • 1
  • 2
    Declaring and initializing are two different things. Where do you initialize `buttonBoat` array? – Pshemo Jul 12 '14 at 11:46

1 Answers1

2

Problem is with this code

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

You have not initialized the buttonBoat array before using it.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34