Results 1 to 3 of 3
- 04-02-2008, 02:50 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 7
- Rep Power
- 0
Null array when passed to MouseListener
I have a GUI program that creates an array of JLabels. A MouseListener is added for each. When the JLabel is pressed it should pass the cell clicked and the large array of JLabels. Inside of the method triggered by my MouseListener I can use the cell, but The array of JLabels that was passed is always null.
Java Code:import java.awt.Color; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; public class ChessPanel extends JPanel { private static final int ROWS = 8; private static final int COLS = 8; // the array of JLabels private JLabel board[][] = new JLabel[ROWS][COLS]; public ChessPanel() { setLayout(new GridLayout(ROWS, COLS)); // add squares to board boolean makeBlack = false; for(JLabel[] row : board) { for(JLabel s : row) { s = new JLabel(); s.addMouseListener(new LabelClick(s, this.board)); add(s); } } } public class LabelClick implements MouseListener { private JLabel clicked; private JLabel[][] newBoard; private int x; private int y; public LabelClick(JLabel s, JLabel[][] origBoard) { clicked = s; newBoard = origBoard; } public void mouseClicked(MouseEvent e) { // it fails here, the array is null System.out.println(newBoard[0][0].getIcon().toString()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } } }
What am I doing wrong?
- 04-02-2008, 05:48 AM #2
The NullPointer is from the board[0][0] element being null.
Java Code:for(JLabel[] row : board) { for(JLabel s : row) { s = new JLabel();
Use regular for loops to initialize the elements of "board". Then you'll get null from the getIcon method in the mouse code since no icon was set.
Java Code:import java.awt.Color; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.*; public class ChessPanelRx extends JPanel { private static final int ROWS = 8; private static final int COLS = 8; // the array of JLabels private JLabel board[][] = new JLabel[ROWS][COLS]; public ChessPanelRx() { setLayout(new GridLayout(ROWS, COLS)); // add squares to board boolean makeBlack = false; for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { board[i][j] = new JLabel(); board[i][j].addMouseListener( new LabelClick(board[i][j], this.board)); add(board[i][j]); } } } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new ChessPanelRx()); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } public class LabelClick implements MouseListener { private JLabel clicked; private JLabel[][] newBoard; private int x; private int y; public LabelClick(JLabel s, JLabel[][] origBoard) { clicked = s; newBoard = origBoard; } public void mouseClicked(MouseEvent e) { // it fails here, the array is null System.out.println(newBoard[0][0].getIcon()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } } }
- 04-02-2008, 10:42 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
NULL Value Of parameters
By riders in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-14-2008, 02:29 PM -
Img.getWidth(null); gives -1 why?
By willemjav in forum New To JavaReplies: 0Last Post: 02-15-2008, 02:06 PM -
What is NULL
By bugger in forum New To JavaReplies: 1Last Post: 01-09-2008, 04:55 PM -
I need help with my MouseMotionAdapter and MouseListener.
By MurderfaceX4 in forum New To JavaReplies: 1Last Post: 12-07-2007, 03:13 AM -
parameters=null
By mary in forum Java ServletReplies: 1Last Post: 07-13-2007, 04:29 PM
Bookmarks