Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 1st, 2006, 5:43 PM   #1
Viredus
Newbie
 
Join Date: Mar 2006
Location: Colorado
Posts: 4
Rep Power: 0 Viredus is on a distinguished road
help

What is wrong with this code? I'm trying to make a two-dimensional JLabel array and setting it up in a GUI using GridBagConstraints. I have a array traversal loop setting image icons to each space, but I keep reaching a NullPointerException. Any input would be helpful.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Image.*;
import java.awt.Toolkit;
import javax.swing.Timer.*;

public class Grid
{
public JFrame window = new JFrame();
public JPanel setup = new JPanel();
public JLabel[][] grid = new JLabel[5][5];
public Toolkit tk = window.getToolkit();
public Image emptySpace = tk.createImage("grid.gif");
public ImageIcon image1 = new ImageIcon(emptySpace.getScaledInstance(200, 200, 0));

public Grid()
{
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(new Dimension(150, 300));
setup.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
for (int row=0; row < grid.length; row++){
for (int col=0; col < grid[row].length; col++)
{
grid[row][col].setIcon(image1);
}
}
for (int row=0; row < grid.length; row++) {
for (int col=0; col < grid[row].length; col++)
{
setup.add(grid[row][col], gbc);
}
}

window.getContentPane().add(setup);
window.setSize(100,400);
window.setVisible(true);

}

public static void createAndShowGUI()
{

Grid gridProg = new Grid();


}

public static void main(String[] args)
{



createAndShowGUI();
}

}
Viredus is offline   Reply With Quote
Old Mar 1st, 2006, 5:46 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
We would love it if you would use code tags, an appropiate thread title, and gave a little more info about your code: where does what happen, where does the error occur and what message do you get.
Polyphemus_ is offline   Reply With Quote
Old Mar 1st, 2006, 5:48 PM   #3
Viredus
Newbie
 
Join Date: Mar 2006
Location: Colorado
Posts: 4
Rep Power: 0 Viredus is on a distinguished road
Sorry.
Exception in thread "main" java.lang.NullPointerException
at Grid<init><Grid.java>
at Grid.createandshowGUI<Grid.java>
at Grid.main<Grid.java>
Viredus is offline   Reply With Quote
Old Mar 1st, 2006, 5:54 PM   #4
Viredus
Newbie
 
Join Date: Mar 2006
Location: Colorado
Posts: 4
Rep Power: 0 Viredus is on a distinguished road
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Image.*;
import java.awt.Toolkit;
import javax.swing.Timer.*;

public class Grid 
{
	public JFrame window = new JFrame();
	public JPanel setup = new JPanel();
	public JLabel[][] grid = new JLabel[5][5];
	public Toolkit tk = window.getToolkit();
	public Image emptySpace = tk.createImage("grid.gif");
	public ImageIcon image1 = new ImageIcon(emptySpace.getScaledInstance(200, 200, 0));
	
   	public Grid()
   	{
   		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(new Dimension(150, 300));
		setup.setLayout(new GridBagLayout());
		
		GridBagConstraints gbc = new GridBagConstraints();
		 for (int row=0; row < grid.length; row++){ 
       		for (int col=0; col < grid[row].length; col++) 
       			{
         		 grid[row][col].setIcon(image1);
         		 }
         		 }
		for (int row=0; row < grid.length; row++) {
       		for (int col=0; col < grid[row].length; col++) 
				{
				setup.add(grid[row][col], gbc);
				}
				}
			
		window.getContentPane().add(setup);
		window.setSize(100,400);
		window.setVisible(true);
			
	}

	public static void createAndShowGUI() 
		{

      	Grid gridProg = new Grid();
      	
      
    	} 
    
	public static void main(String[] args) 
		{
      
      			
			
                createAndShowGUI();
       	}
											
	}
Viredus is offline   Reply With Quote
Old Mar 1st, 2006, 5:58 PM   #5
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Each element in your grid array is null. Put something in it.
jaeusm is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC