Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 03:50 AM
Member
 
Join Date: Feb 2008
Posts: 7
stevemcc is on a distinguished road
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.
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-02-2008, 06:48 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
The NullPointer is from the board[0][0] element being null.
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.
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 } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-02-2008, 11:42 PM
Member
 
Join Date: Feb 2008
Posts: 7
stevemcc is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 01:19 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org