Results 1 to 3 of 3
  1. #1
    stevemcc is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default 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
    			
    		}
    		
    	}
    
    }
    I cannot figure out why the array being passed to the mouselistener would be null, especially when the cell being clicked is not null.

    What am I doing wrong?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The NullPointer is from the board[0][0] element being null.
    Java Code:
    for(JLabel[] row : board) {
        for(JLabel s : row) {
            s = new JLabel();
    In this block "s" will always be null. You cannot use the foreach loop to alter the elements returned.
    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
            }
        }
    }

  3. #3
    stevemcc is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default

    Ohhh, so apparently when I make a change to s it doesnt go up the line and make those changes to the actual array.

    I guess the lesson is, do not use for each loops when you want to make changes to the array you are for eaching.

Similar Threads

  1. NULL Value Of parameters
    By riders in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-14-2008, 03:29 PM
  2. Img.getWidth(null); gives -1 why?
    By willemjav in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 03:06 PM
  3. What is NULL
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 05:55 PM
  4. I need help with my MouseMotionAdapter and MouseListener.
    By MurderfaceX4 in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 04:13 AM
  5. parameters=null
    By mary in forum Java Servlet
    Replies: 1
    Last Post: 07-13-2007, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •