Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this code displaying cursor(sprite) on blue background.
I unsuccessfully trying to move cursor, but got "Null Pointer Exception",
because g = null, I don't know why & how to fix.
Please, help.

package hello;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.Graphics;
 
public class Tiles extends MIDlet {
 
  Board b;
 
  public Tiles() {
    b = new Board(this);
  }
 
  public void startApp() {
    Display.getDisplay(this).setCurrent(b);
  }
 
  public void pauseApp() {
  }
 
  public void destroyApp(boolean unconditional) {
  }
 
}
 
class Board extends Canvas implements CommandListener {
private GameDesign gameDesign;
private Sprite cursor;
private LayerManager lm;
 
public Graphics g;
public int x = 0;
  MIDlet midlet;
 
  Command exitCommand;
 
  public Board(MIDlet midlet_) {
      try{
          this.lm = new LayerManager();
 
this.gameDesign = new GameDesign();
this.cursor = this.gameDesign.getCurs();
this.cursor.defineReferencePixel(cursor.getWidth(), cursor.getHeight());
this.cursor.move(0, 0);
          this.lm.append(cursor);
          
          this.lm.setViewWindow(0, 0, this.getWidth(), this.getHeight());
      }
      catch(Exception x){}
 
    exitCommand = new Command("Exit", Command.SCREEN, 2);
    addCommand(exitCommand);
 
    setCommandListener(this);
 
    ////////////////////===============
 
 
    repaint();
 
  }
 
  public void commandAction(Command c, Displayable d) {
    if (c == exitCommand) {
      midlet.notifyDestroyed();
 
    }
  }
 
  public void paint(Graphics g) {
      g.setColor(255);
      g.fillRect(0, 0, 100, 100);
            //g.drawImage(im, x, 10, Graphics.LEFT | Graphics.TOP);
            this.cursor.paint(g);
  }
 
  public void keyPressed(int code) {
    int game = getGameAction(code);
 
    switch (game) {
    case Canvas.UP:
      System.out.println("Canvas.UP");
      break;
    case Canvas.DOWN:
      System.out.println("Canvas.DOWN");
      break;
    case Canvas.LEFT:
      System.out.println("Canvas.LEFT");
      break;
    case Canvas.RIGHT:
        x++;
        this.cursor.setPosition(x, cursor.getY());
        this.cursor.paint(g);      //<===============NULL POINTER EXCEPTION
      System.out.println("Canvas.RIGHT");
      break;
    }
 
  }
 
 
}
Posted

1 solution

The problem starts with the two variables called g.
The one in paint(Graphics g) is transient, and can only be used inside this call to paint. [See this quote from the JSR-118 help for Canvas.paint():

Operations on this graphics object after the paint() call returns are undefined. Thus, the application must not cache this Graphics object for later use or use by another thread. It must only be used within the scope of this method.
]
Your global Graphics g is never initialised, which is why you get a null pointer exception when you try to use it.

What you should be doing is calling Board.repaint() or Board.repaint(x, y, width, height) in place of the failing line. This is a method inherited from Canvas which will ultimately result in your paint() being called to do the work. See the JSR-118 description of the Canvas class and its methods. Also, look at sample programs from places like the Nokia or Motorola developers forums.

[Hint: if you want to reply to this, don't post another 'Answer', make a comment to this one. If you like this answer, vote for it and mark it 'accepted'. Thanks.
 
Share this answer
 
Comments
[no name] 13-Sep-10 7:40am    
Thanks! Can you say, how to fix background image?
I was add in Paint()
g.drawImage(im, x, 10, Graphics.LEFT | Graphics.TOP);
and when I move cursor, image moving too.
[no name] 13-Sep-10 7:56am    
Solved. Forgot that i use 'x' to draw image.
thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900