Sign in to follow this  

stupid Null Pointer Exception

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been working on a tool for my 2D game and right now I have a really stupid mistake which I can't seem to be able to solve. I've made 3 games in C++ and now that I finally got to a 2D sidescroller I wanted to switch OSes to Linux, so I've switched the language to Java. I've studied Java before C++, but I have more experience with C++ since I didn't really need Java until this point XD

Either way, since in Java there are no pointers (available to the user), I had to make some object arrays to hold references to the objects that I want but don't have at the moment. I made sure all the holders are either null or set to 0. When I check for the objects being null, I get a null pointer exception with a really long stack dump (I think that's what it is).

Here's the code that's causing problems (only relevant pieces were kept):

public class DestinationBitmapPanel extends JScrollPane {
private class Tile {
BufferedImage tileImage;
Point sourcePosition;

public Tile() {
tileImage = null;
sourcePosition = new Point();
}
}

int width, height;
Tile[][] mTilePositions;
int tileWidth, tileHeight;

boolean linesEnabled;

DestinationBitmapPanel(int x, int y ) {
width = x;
height = y;
tileWidth = 16;
tileHeight = 16;

mTilePositions = new Tile[width/tileWidth][height/tileHeight];
}

public void placeTile(BufferedImage tile, Point position, Point sourcePosition) {
mTilePositions[position.x][position.y].tileImage = tile;
mTilePositions[position.x][position.y].sourcePosition = sourcePosition;

repaint();
}

public void paint(Graphics g) {
for(int i = 0; i < mTilePositions.length; i++) {
for(int j = 0; j < mTilePositions[i].length; j++) {
------> if(mTilePositions[i][j].tileImage != null) {
g.drawImage(mTilePositions[i][j].tileImage, i*tileWidth, j*tileHeight, tileWidth, tileHeight, Color.BLACK, null);


}
}
}
}
}




The troubling line has been point out with an arrow (------>).

Here is the stack dump:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gameproductool.DestinationBitmapPanel.paint(DestinationBitmapPanel.java:56)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:581)
at javax.swing.JComponent.paintChildren(JComponent.java:866)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5145)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:302)
at javax.swing.RepaintManager.paint(RepaintManager.java:1145)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
at java.awt.Container.paint(Container.java:1844)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:751)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:696)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:676)
at javax.swing.RepaintManager.access$700(RepaintManager.java:57)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1550)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:602)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

Share this post


Link to post
Share on other sites
The following creates your array and sets all the elements to null (which is the default for an array of objects).
mTilePositions = new Tile[width/tileWidth][height/tileHeight];

Before you can use your tiles you need to create them.
for (int i = 0; i < mTilePositions.length; i++)
{
for (int j = 0; j < mTilePositions[i].length; j++)
{
mTilePositions[i][j] = new Tile();
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by boogyman19946
Well that's the thing. I need the tiles to be null, and have to check later if they are in fact null in order to proceed, but I can't seem to be able to check them because the reference itself seems to be null.

Read what kloffy said again. He pointed out the exact issue.

Share this post


Link to post
Share on other sites
Quote:
Original post by boogyman19946
oooooooooohhhhhhhhhhhhhhhhhhhhhhhhhhh XD

So Java doesn't create default objects when arrays are created? Sorry I missed the fact that it was the private class that wasn't created, I thought it was the buffered image.


No, the array you created is an array of references.

As such, all of the references default to NULL because they have not been initialized to an instance of the class you wish them to store.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Quote:
Original post by boogyman19946
oooooooooohhhhhhhhhhhhhhhhhhhhhhhhhhh XD

So Java doesn't create default objects when arrays are created? Sorry I missed the fact that it was the private class that wasn't created, I thought it was the buffered image.


No, the array you created is an array of references.

As such, all of the references default to NULL because they have not been initialized to an instance of the class you wish them to store.


Hmm I could've realized that. From what I understand, anything aside from the primitive int, float, etc. types is considered to be an object, and when it comes to objects, Java operates only in references. Well, just shows how little I know still -.-

Share this post


Link to post
Share on other sites

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this