Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I need to add collision to my tile map using MarteEngine. MarteEngine is built of of slick2D. Here's my tile generation code: Code:

public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException
   {
      for (int x = 0; x < 16; x++) {
         for (int y = 0; y < 16; y++) {
            map[x][y] = AIR;
               air.draw(x * GameWorld.tilesize, y * GameWorld.tilesize);
         }
      }

   for (int x = 0; x < 16; x++) {
         for (int y = 7; y < 8; y++) {
               map[x][y] = GRASS;
               grass.draw(x * tilesize, y * tilesize);
         }
      }

   for (int x = 0; x < 16; x++) {
         for (int y = 8; y < 10; y++) {
            map[x][y] = DIRT;
               dirt.draw(x * tilesize, y * tilesize);
         }
      }

   for (int x = 0; x < 16; x++) {
         for (int y = 10; y < 16; y++) {
            map[x][y] = STONE;
               stone.draw(x * tilesize, y * tilesize);   
         }
      }
      super.render(gc, game, g);


   }

And one of my tile classes (they're all the same, the image names are just different): Code:

package MarteEngine;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import it.randomtower.engine.entity.Entity;

public class Grass extends Entity {

   public static Image grass = null;

   public Grass(float x, float y) throws SlickException {
      super(x, y);

       grass = new Image("res/grass.png");

       setHitBox(0, 0, 50, 50);

       addType(SOLID);
   }

}

I tried to do it like this: Code:

for (int x = 0; x < 16; x++) {
         for (int y = 7; y < 8; y++) {
               map[x][y] = GRASS;
               Grass.grass.draw(x * tilesize, y * tilesize);
         }
      }

But it gave me a NullPointerException. No idea why, everything looks initialized right? I would be very grateful for some help!

share|improve this question
    
This should be an easy problem for you to solve by stepping through the code with the debugger. Honestly once you learn to use the debugger you will feel silly for having asked this question :) –  Byte56 Oct 3 '12 at 23:45
    
@Byte56 Haha probably will but is it really that obvious? Can you at least give me a hint to which class its in? –  opiop65 Oct 3 '12 at 23:48
    
What line is the it saying the null pointer is on? –  GameDev-er Oct 3 '12 at 23:48
    
Oh, I don't know where the problem is. I'm just saying you could figure it out in way less time than it took you to write the question. With the debugger it really will be that obvious. –  Byte56 Oct 3 '12 at 23:49
    
@GameDev-er it says on line 58 of GameWorld which is my tile generation. Line 56 starts at: [code]Air.air.draw(x * GameWorld.tilesize, y * GameWorld.tilesize); [/code] –  opiop65 Oct 3 '12 at 23:50
show 2 more comments

closed as too localized by Byte56, Jonathan Hobbs, Josh Petrie, Laurent Couvidou, Trevor Powell Nov 23 '12 at 1:15

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

From the code you posted it looks like you never actually call the constructor for Grass, so Grass.grass is still null. It never gets a new Image(...) assigned to it. If you try to call a method on a null object, you'll get a NullPointerException.

What are the values of AIR, GRASS, DIRT, and STONE? Are they global?

EDIT:

Where in your code do you call the constructor for Grass (or Air, Stone, or Dirt)?

Grass theGrass = new Grass(0, 0); 
share|improve this answer
    
Yes, they are in my GameWorld Class, and are just public static ints representing nothing pretty much. The code that is causing the error is actually Grass.grass.draw(). grass is set to null. But then I call on it later with grass = new Image(). Is this the problem? –  opiop65 Oct 3 '12 at 23:58
    
I think the problem is that the constructor for Grass is either 1) not called (leaving Grass.grass as null) or 2) called after the code that you posted above, so it hasn't initialized anything yet. I will update my answer with more information. –  Zack The Human Oct 4 '12 at 0:03
    
I call it in the Grass class with grass = new Image(res/grass.png); I realize this is not a constructor but all it is doing is setting an image. Then I call it with Air.air.draw(x * tilesize, y * tilesize); –  opiop65 Oct 4 '12 at 0:09
    
I see that you instantiate the Grass.grass image in the Grass constructor, but that constructor never gets called, so the statement grass = new Image(res/grass.png); is never executed. –  Zack The Human Oct 4 '12 at 0:26
    
So how should I call it? I'm sorry, I'm really tired right now and its probably obvious but I just have no idea how to do it right now. –  opiop65 Oct 4 '12 at 0:29
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.