title: Java - Slick 2D - Placing text (or any graphics) on a rectangle (or any Shape)
body: <p>I am making an RTS game using Slick 2D. I have a building class and it contains an int called resources. I want the resources variable to become a string and be rendered onto the center of my building (which is just supposed to be a rectangle).</p>
  
  <p>Here's my building class:</p>
  
  <pre><code>package test;
  
  import org.newdawn.slick.Color;
  import org.newdawn.slick.Graphics;
  import org.newdawn.slick.geom.Rectangle;
  
  public class BaseBuilding extends Rectangle{
  
      private int resources;
      private Graphics g;
  
      public BaseBuilding(float x, float y, float width, float height) {
          super(x, y, width, height);
          resources = 0;
          g = new Graphics();
      } // end constructor
  
      public void render(){
          g.drawString("bro wat", resources, resources);
          g.setColor(Color.gray);
          g.fill(this);
          g.drawString(Integer.toString(resources), this.width/2, this.height/2);
      } // end update
  
      public int getResources(){
          return resources;
      } // end getResources
  
      public void addResources(int mulla){
          resources += mulla;
          if (resources &lt; 0) 
              resources = 0;
      } // end addResources
  
      public void createBasicUnit(){
          // create a BasicUnit object in a randomly, valid position close to this object
          // TODO
      } // end createBasicUnit
  
  } // end class def
  </code></pre>
  
  <p>So far all my states are working and i got the rectangle to appear appropriately. My g.drawString(str, float, float) code with correct parameters will work on my GameState class(not shown) in its render function, but it gives me this error in my BaseBuilding class:</p>
  
  <pre><code>java.lang.NullPointerException
  at org.newdawn.slick.Graphics.drawString(Graphics.java:1366)
  at test.BaseBuilding.render(BaseBuilding.java:19)
  at test.GameState.render(GameState.java:34)
  at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
  at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
  at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
  at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
  at test.StateBaseGame.main(StateBaseGame.java:22)
  Sat Apr 18 03:18:55 EDT 2015 ERROR:Game.render() failure - check the game code.
  org.newdawn.slick.SlickException: Game.render() failure - check the game code.
  at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
  at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
  at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
  at test.StateBaseGame.main(StateBaseGame.java:22)
  </code></pre>
  
  <p>So i dug around and found out it's this line that's causing me problems:</p>
  
  <pre><code>g.drawString(Integer.toString(resources), this.width/2, this.height/2);
  </code></pre>
  
  <p>Couldn't find it on the internet; help if you can please thanks.</p>