# [java] WTH

## Recommended Posts

I'm stumped on this one. Changing the z-order of my button from
canvas.setComponentZOrder(button,0);
to
canvas.setComponentZOrder(button,1);
nullifies the button. The program works fine with a 0 for z-order, but throws a null pointer exception upon access when the z-order > 0 This is the original context.
		for(int r = 0; r < tiles.length; r++){
for(int c = 0; c < tiles[0].length; c++){
button = new xCellContainer();
tiles[r][c] = button;

canvas.setComponentZOrder(button,0);

button.setVisible(true);
button.setSize(cell_w_scaled, cell_h_scaled);
button.setLocation(cell_w_scaled*(c), cell_h_scaled*(r));

}
}		
Accessed with
tiles[current_r][current_c].setCell(c);

The program works fine until I change that one piece! Ideas?

##### Share on other sites
That should be impossible. Can you post a full program that demonstrates the issue? Keep stripping out code until you get a minimal program, or the bug dissappears.

##### Share on other sites
Quote:
 Original post by rip-offThat should be impossible. Can you post a full program that demonstrates the issue? Keep stripping out code until you get a minimal program, or the bug dissappears.

The program is huge; it's my game engine. I'll try to cut down this section. I wish it was impossible

##### Share on other sites
Increasing the z-order is expected to make your button disappear, as it is drawn beneath other widgets. Why did you begin fiddling with it?

It's also likely that the "button" stops getting mouse, keyboard, focus events: maybe the respective event handlers, of which you post nothing, did something that averted the mysterious NPE.

Or maybe the widgets are retrieved (how and why?) from the "canvas" (what is it?) by z-order, and the wrong value misplaces them.

Did you debug your code? Discovering what is null should be straightforward.

EDIT: finding all references to the z-order of Container children in your own code should be equally straightforward, and some of them are almost certainly the source of your NPE bug.

[Edited by - LorenzoGatti on February 15, 2010 2:36:01 AM]

##### Share on other sites
Quote:
 Original post by LorenzoGattiIncreasing the z-order is expected to make your button disappear, as it is drawn beneath other widgets. Why did you begin fiddling with it?It's also likely that the "button" stops getting mouse, keyboard, focus events: maybe the respective event handlers, of which you post nothing, did something that averted the mysterious NPE.Or maybe the widgets are retrieved (how and why?) from the "canvas" (what is it?) by z-order, and the wrong value misplaces them.Without knowing what your "access" is nor anything about the xCellContainer class, there is no way to help you.Did you debug your code? Discovering what is null should be straightforward.EDIT: finding all references to the z-order of Container children in your own code should be equally straightforward, and some of them are almost certainly the source of your NPE bug.

I now have a JLayeredPane (used to be JPanel) holding a grid of 'containers' (not in the java sense) which display cells from the map. On top of this layer, I have entity 'containers' which display entities from the map. The cell 'containers' add fine but the entity containers cause me problems. I am now receiving an error "java.lang.IllegalArgumentException: illegal component position"

add(e.getContainer(), new Integer(1));

What could be wrong with my custom components that they resist being added to layers, both in the case of JPanel and JLayeredPane? My other custom components work fine... speculation please!

##### Share on other sites
Quote:
 Original post by LorenzoGattiIncreasing the z-order is expected to make your button disappear, as it is drawn beneath other widgets. Why did you begin fiddling with it?It's also likely that the "button" stops getting mouse, keyboard, focus events: maybe the respective event handlers, of which you post nothing, did something that averted the mysterious NPE.Or maybe the widgets are retrieved (how and why?) from the "canvas" (what is it?) by z-order, and the wrong value misplaces them.Without knowing what your "access" is nor anything about the xCellContainer class, there is no way to help you.Did you debug your code? Discovering what is null should be straightforward.EDIT: finding all references to the z-order of Container children in your own code should be equally straightforward, and some of them are almost certainly the source of your NPE bug.

I now have a JLayeredPane (used to be JPanel) holding a grid of 'containers' (not in the java sense) which display cells from the map. On top of this layer, I have entity 'containers' which display entities from the map. The cell 'containers' add fine but the entity containers cause me problems. I am now receiving an error "java.lang.IllegalArgumentException: illegal component position"

add(e.getContainer(), new Integer(1));

What could be wrong with my custom components that they resist being added to layers, both in the case of JPanel and JLayeredPane? My other custom components work fine... speculation please!

##### Share on other sites
Quote:
 Original post by LorenzoGattiIncreasing the z-order is expected to make your button disappear, as it is drawn beneath other widgets. Why did you begin fiddling with it?It's also likely that the "button" stops getting mouse, keyboard, focus events: maybe the respective event handlers, of which you post nothing, did something that averted the mysterious NPE.Or maybe the widgets are retrieved (how and why?) from the "canvas" (what is it?) by z-order, and the wrong value misplaces them.Without knowing what your "access" is nor anything about the xCellContainer class, there is no way to help you.Did you debug your code? Discovering what is null should be straightforward.EDIT: finding all references to the z-order of Container children in your own code should be equally straightforward, and some of them are almost certainly the source of your NPE bug.

I now have a JLayeredPane (used to be JPanel) holding a grid of 'containers' (not in the java sense) which display cells from the map. On top of this layer, I have entity 'containers' which display entities from the map. The cell 'containers' add fine but the entity containers cause me problems. I am now receiving an error "java.lang.IllegalArgumentException: illegal component position"

add(e.getContainer(), new Integer(1));

What could be wrong with my custom components that they resist being added to layers, both in the case of JPanel and JLayeredPane? My other custom components work fine... speculation please!

##### Share on other sites
Managed to get it to add correctly, except it never is drawn. paintComponent(args) is never called.
					JPanel j = new JPanel();					j.setSize(100, 100);					j.setBackground(Color.red);					j.setLocation(200,200);					j.setVisible(false);					add(j, new Integer(1));										xEntityContainer x = e.getContainer();															x.setVisible(true);					x.setLocation(100,100);//e_draw_x, e_draw_y);					System.out.println("HERE!==================================");					add(x, new Integer(1));					x.repaint();

The JPanel shows up fine. The first line of code in the container is never reached...

	@Override	public void paintComponent(Graphics g){		System.out.println("HERE---------------------------------------------------------------");

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628278
• Total Posts
2981786

• 10
• 11
• 17
• 14
• 9