Search code examples
javamouselistenerjlayeredpane

Why am i not able to add jlayered pane?It is showing null pointer exception error


Edit: hadnt initilaised layeredpane silly of me. Sorry for the long code jsut at the start of mpanel constructor i add ajlayered pane but it doesn not get added gives error on eclipse why?
I am really new with java so i dont have any idea about layerd panes

import java.awt.event.*;
import java.util.*;
import java.awt.*;

import javax.swing.event.MouseInputAdapter;
import javax.swing.*;

// battleship.MainPanel.GridListener;

 public class mPanel extends JFrame implements MouseListener, MouseMotionListener
 {
public JLayeredPane layeredPane=null;
private static final int UNCLICKED = 6;
private static final int EMPTY = 7;
private static final int HIT = 8;
private static final int MISS = 9;
private static final Color COLOR_UNCLICKED = Color.white;
private static final Color COLOR_MISS = Color.blue;
private static final Color COLOR_HIT = Color.red;
private JLabel title;
private JPanel titlePanel;
private JButton[][] gridButton,gridButton1;
private JPanel gridPanel,gridPanel1;
private int[][] board,board1;
Dimension boardSize = new Dimension(340, 410);
 GridListener gridListener = new GridListener();
int count=0;
private JLabel ship1;



//private GridListen gridListen = new GridListen();


public mPanel()
{

here i get error as to null pointer exceptions

layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
layeredPane.setPreferredSize(boardSize);
getContentPane().add(layeredPane);
title = new JLabel("BATTLESHIP!");
ship1= new JLabel("Ship1");
titlePanel = new JPanel();
titlePanel.add(title);
gridButton = new JButton[10][10];
gridButton1 = new JButton[10][10];
gridPanel = new JPanel();
gridPanel1= new JPanel();
gridPanel.setLayout(new GridLayout(10, 10));
gridPanel1.setLayout(new GridLayout(10, 10));
for (int r = 0; r < gridButton.length; r++)
    for (int c = 0; c < gridButton[r].length; c++)
    {
    gridButton[r][c] = new JButton();
    gridButton[r][c].setBackground(COLOR_UNCLICKED);
    gridButton[r][c].setEnabled(true);
    gridButton[r][c].addActionListener(gridListener);
    gridPanel.add(gridButton[r][c]);
    }
gridPanel1.setLayout(new GridLayout(10, 10));
for (int r = 0; r < gridButton1.length; r++)
    for (int c = 0; c < gridButton1[r].length; c++)
    {
    gridButton1[r][c] = new JButton();
    gridButton1[r][c].setBackground(COLOR_UNCLICKED);
    gridButton1[r][c].setEnabled(true);
    gridButton1[r][c].addActionListener(gridListener);
    gridPanel1.add(gridButton1[r][c]);
    }
this.setLayout(new BorderLayout());
this.add(titlePanel, "North");
this.add(gridPanel, BorderLayout.LINE_START);
this.add(gridPanel1, BorderLayout.LINE_END);
this.setPreferredSize(new Dimension(1100, 400));
board = new int[10][10];
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board.length; c++)
{
board[r][c] = UNCLICKED;
gridButton[r][c].setEnabled(true);
}
board1 = new int[10][10];
for (int r = 0; r < board1.length; r++)
for (int c = 0; c < board1.length; c++)
{
board1[r][c] = UNCLICKED;
gridButton1[r][c].setEnabled(false);
}
/*add(ship1);
ship1.setBounds(100, 100, 150, 40);
ship1.addMouseMotionListener(new MouseAdapter(){

    public void mouseDragged(MouseEvent E)
    {
       int X=E.getX()+ship1.getX();
       int Y=E.getY()+ship1.getY();
       ship1.setBounds(X,Y,150,40);
       System.out.println(X+" "+Y);
    }
    });*/

  }
   class GridListener implements ActionListener
  {


@Override
 public void actionPerformed(ActionEvent evt) {

if(count%2==0)
{
    //System.out.println(count);

for (int r = 0; r < gridButton.length; r++)
for(int c = 0; c < gridButton[r].length; c++)
{

if (evt.getSource() != gridButton[r][c])
continue;
handleGridButton(r,c);
return;
}
}

else
{
//System.out.println(count);


for (int r = 0; r < gridButton1.length; r++)
for(int c = 0; c < gridButton1[r].length; c++)
{
if (evt.getSource() != gridButton1[r][c])
continue;
handleGridButton(r,c);
return;
}


}
}


}
public void handleGridButton(int r, int c)
{

if(count%2==0)
{
if (board[r][c] == UNCLICKED)
{
++count;
board[r][c] = MISS;
gridButton[r][c].setBackground(COLOR_MISS);

}
}
else
{
    ++count;
    if (board1[r][c] == UNCLICKED)
    {

    board1[r][c] = MISS;
    gridButton1[r][c].setBackground(COLOR_MISS);


}
}
gridenable();
System.out.println(count);
}
public void gridenable()
{
if(count%2==0)
{
    for (int r = 0; r < gridButton.length; r++)
    {
    for(int c = 0; c < gridButton[r].length; c++)
    gridButton1[r][c].setEnabled(false);
    }
    for (int r = 0; r < gridButton.length; r++)
    {
    for(int c = 0; c < gridButton[r].length; c++)
        if(board[r][c]!=MISS)
    gridButton[r][c].setEnabled(true);
    }
}
else
{
    for (int r = 0; r < gridButton.length; r++)
    {
    for(int c = 0; c < gridButton[r].length; c++)
    gridButton[r][c].setEnabled(false);
    }
    for (int r = 0; r < gridButton.length; r++)
    {
    for(int c = 0; c < gridButton[r].length; c++)
        if(board1[r][c]!=MISS)
    gridButton1[r][c].setEnabled(true);
    }
}
 }
 @Override
 public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub

 }
 @Override
 public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub

 }
 @Override
 public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
 public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

 public static void main(String args[])
 {
mPanel n= new mPanel();
n.pack();

n.setVisible(true);
n.setResizable(false);
 }
 }

Solution

  • You have to initialize layeredPane before starting calling methods on it. something like below

    layeredPane= new JLayeredPane()
    layeredPane.addMouseListener( this );
    layeredPane.addMouseMotionListener( this );
    layeredPane.setPreferredSize(boardSize);
    

    It won't also hurt to inherently understand what NPE mean and it's root cause by googling on the web. For instance you can look at sof