NullPointer - cant find where its coming from!!

Well, I guess the phrase “cant find where its coming from” isn’t exactly right, but I just dont know why im getting this error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at net.sauce.block.BlockStone.renderBlock(BlockStone.java:23)
	at net.sauce.game.Chunk.renderChunk(Chunk.java:57)
	at net.sauce.game.World.render(World.java:21)
	at net.sauce.game.GameCanvas.paint(GameCanvas.java:33)
	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
	at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
	at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
	at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
	at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:785)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
	at java.awt.EventQueue.access$000(EventQueue.java:84)
	at java.awt.EventQueue$1.run(EventQueue.java:602)
	at java.awt.EventQueue$1.run(EventQueue.java:600)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

My render block method:
(Line 23 is the if(e.getType…) )

public void renderBlock(Graphics2D g) {
		if(willRender()){
			if(!getAdjacentBlocks().isEmpty()){
				for(Block e: getAdjacentBlocks()){
					if(e.getType().equals(Material.AIR)){
						g.setColor(Color.gray);
						g.fillRect(getRenderX(), getRenderY(), 32, 32);
					}
				}
			}
		}
	}

getAdjacentBlocks() method:

public List<Block> getAdjacentBlocks(){
		List<Block> ret = new ArrayList<Block>();
		Block[][] blocks = getChunk().getBlocks();
		//UP-LEFT
		if(getGridX() != 0 && getGridY() != 0)
			ret.add(blocks[getGridX()-1][getGridY()-1]);
		//UP
		if(getGridY() != 0)
			ret.add(blocks[getGridX()][getGridY()-1]);
		//UP-RIGHT
		if(getGridY() != 0 && getGridX() != getChunk().width)
			ret.add(blocks[getGridX()+1][getGridY()-1]);
		
		//DOWN-LEFT
		if(getGridY() != getChunk().height && getGridX() != 0)
			ret.add(blocks[getGridX()-1][getGridY()+1]);
		//DOWN
		if(getGridY() != getChunk().height)
			ret.add(blocks[getGridX()][getGridY()+1]);
		//DOWN-RIGHT
		if(getGridX() != getChunk().width && getGridY() != getChunk().height)
			ret.add(blocks[getGridX()+1][getGridY()+1]);
		
		//LEFT
		if(getGridX() != 0)
			ret.add(blocks[getGridX()-1][getGridY()]);
		//RIGHT
		if(getGridX() != getChunk().width)
			ret.add(blocks[getGridX()+1][getGridY()]);
		
		return ret;
	}

From this code, I dont see how I can get this error.

I’ve never tried that type of for loop so that might be it.

however, on the line its throwing it either has to be getType() throwing the exception, or the Material.AIR. the AIR var though seems to be a static final? so I doubt its that.

so actually I would say you should make sure that the block e is getting initialized properly, because that would be my best guess as to what is wrong.

Either “e” or “e.getType()” is returns null :slight_smile:

I would start by making sure all the Block variables in the “blocks” array are not null. Then make sure “e.getType()” doesn’t return null.

e can’t be null, assuming the list does not allow null elements (I assume it’s an ArrayList). I think the problem is that e.getType() sometimes is null, and calling equals(…) on a null object throws a Nuppo.

You can check this out easily by replacing the line

if(e.getType().equals(Material.AIR)){

with a test line:

if(e.getType() == null){

which will draw all tiles that have null data.

It could be a concurrency problem. If your frame is set to visible before you have initialized your data, a repaint may be triggered before you’re done setting everything up. Looking over your code with a debugger is a very good idea in this case.

Run in IDE, in debug mode. Inspect object. Done.
Seriously NPE’s with stacktraces take 30 secs to fix.

ArrayLists do allow nulls so yes, there is a possibility either “e” or “e.getType()” are null.

also a little little trick is the yoda notation

if you have

a.equals(B);

where “a” can be null and B is guaranteed to be not null(constants for example),
you can use

B.equals(a);
//i.e
"Hallo world".equals(teststring);

You should be aware that with this u can’t handle the special case a==null

Also, when I write code I place alot of asserts at the start of my methods, everywhere were I depend on that specific criterias are met. Of course asserts should be placed only when this criterias aren’t fullfilled by using the method false in code not through exceptional behavoir(user input …)