×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Red face Null Pointer Exception!

    Hi!
    I run my game in J2ME Wireless ToolKit 2.2 successfully, but when I run my game in Nokia Mobile Internet Toolkit or a real nokia phone that had a error of Null Pointer Exception. The error was happened when some action changes in the game or exiting the game. Here is my program:

    SpeedMIDlet.java

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class SpeedMIDlet extends MIDlet implements CommandListener {
    private HCanvas canvas;
    protected Image background;

    public void startApp() {
    if (canvas == null) {
    canvas = new HCanvas(Display.getDisplay(this));
    Command exitCommand = new Command("Exit", Command.EXIT, 0);
    canvas.addCommand(exitCommand);
    canvas.setCommandListener(this);
    }

    // Start up the canvas
    canvas.start();
    }

    public void pauseApp() {}

    public void destroyApp(boolean unconditional) {
    canvas.stop();
    }

    public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT) {
    destroyApp(true);
    notifyDestroyed();
    }
    }
    }

    Some parts of HCanvas.java

    public HCanvas(Display d) {
    super(true);
    display = d;

    // Set the frame rate (30 fps)
    frameDelay = 33;

    // Clear the input delay
    inputDelay = 0;
    }

    public void start() {
    // Set the canvas as the current screen
    display.setCurrent(this);

    // Initialize the random number generator
    rand = new Random();

    // Initialize the game variables
    gameOver = false;
    numLives = 3;
    score = 0;

    // Initialize the background image and chicken and car sprites
    try {
    background = Image.createImage("/Highway.png");
    chickenHead = Image.createImage("/ChickenHead.png");
    speed = Image.createImage("/speed.png");

    blackholeSprite[0] = new Sprite(Image.createImage("/Blackhole.png"));
    blackholeSprite[0].setPosition(70, 77);
    blackholeSprite[1] = new Sprite(Image.createImage("/Blackhole.png"));
    blackholeSprite[1].setPosition(90, 107);
    blackholeSprite[2] = new Sprite(Image.createImage("/Blackhole.png"));
    blackholeSprite[2].setPosition(121, 60);
    blackholeSprite[3] = new Sprite(Image.createImage("/Blackhole.png"));
    blackholeSprite[3].setPosition(100, 20);

    chickenSprite = new Sprite(Image.createImage("/Person.png"), 20, 26);
    chickenSprite.setPosition(2, 77);

    carSprite[0] = new Sprite(Image.createImage("/Car1.png"));
    carSprite[0].setPosition(40, 0);
    carYSpeed[0] = 4;
    carSprite[1] = new Sprite(Image.createImage("/Car3.png"));
    carSprite[1].setPosition(111, 67);
    carYSpeed[1] = -4;

    }
    catch (IOException e) {
    System.err.println("Failed loading images!");
    e.printStackTrace();
    }

    // Initialize the music and wave players
    try {
    InputStream is = getClass().getResourceAsStream("Music.mid");
    musicPlayer = Manager.createPlayer(is, "audio/midi");
    musicPlayer.prefetch();
    is = getClass().getResourceAsStream("Celebrate.wav");
    celebratePlayer = Manager.createPlayer(is, "audio/X-wav");
    celebratePlayer.prefetch();
    is = getClass().getResourceAsStream("Squish.wav");
    squishPlayer = Manager.createPlayer(is, "audio/X-wav");
    squishPlayer.prefetch();
    is = getClass().getResourceAsStream("GameOver.wav");
    gameoverPlayer = Manager.createPlayer(is, "audio/X-wav");
    gameoverPlayer.prefetch();
    }
    catch (IOException ioe) {
    ioe.printStackTrace();
    }
    catch (MediaException me) {
    me.printStackTrace();
    }

    // Start playing the music indefinitely
    try {
    musicPlayer.setLoopCount(-1);
    musicPlayer.start();
    }
    catch (MediaException me) {
    me.printStackTrace();
    }

    // Start the animation thread
    sleeping = false;
    Thread t = new Thread(this);
    t.start();
    }

    public void stop() {
    // Close the music and wave players
    musicPlayer.close();
    celebratePlayer.close();
    squishPlayer.close();
    gameoverPlayer.close();

    // Stop the animation
    sleeping = true;
    }

    public void run() {
    Graphics g = getGraphics();

    // The main game loop
    while (!sleeping) {
    update();
    draw(g);
    try {
    Thread.sleep(frameDelay);
    }
    catch (InterruptedException ie) {ie.printStackTrace();}
    }
    }

    private void update() {
    // Check to see whether the game is being restarted
    if (gameOver) {
    int keyState = getKeyStates();
    if ((keyState & FIRE_PRESSED) != 0) {
    // Start a new game
    try {
    musicPlayer.setMediaTime(0);
    musicPlayer.start();
    }
    catch (MediaException me) {
    me.printStackTrace();
    }
    chickenSprite.setPosition(2, 77);
    gameOver = false;
    score = 0;
    numLives = 3;
    }

    // The game is over, so don't update anything
    return;
    }

    // Process user input to move the chicken
    if (++inputDelay > 2) {
    int keyState = getKeyStates();
    if ((keyState & LEFT_PRESSED) != 0) {
    chickenSprite.move(-6, 0);
    chickenSprite.nextFrame();
    }
    else if ((keyState & RIGHT_PRESSED) != 0) {
    chickenSprite.move(6, 0);
    chickenSprite.nextFrame();
    }
    if ((keyState & UP_PRESSED) != 0) {
    chickenSprite.move(0, -6);
    chickenSprite.nextFrame();
    }
    else if ((keyState & DOWN_PRESSED) != 0) {
    chickenSprite.move(0, 6);
    chickenSprite.nextFrame();
    }
    checkBounds(chickenSprite, false);

    // Reset the input delay
    inputDelay = 0;
    }

    // See whether the chicken made it across
    if (chickenSprite.getX() > 154) {
    // Play a sound for making it safely across
    try {
    celebratePlayer.start();
    }
    catch (MediaException me) {
    me.printStackTrace();
    }

    // Reset the chicken position and increment the score
    chickenSprite.setPosition(2, 77);
    score += 25;
    }

    // Update the car sprites
    for (int i = 0; i < 2; i++) {
    // Move the car sprites
    carSprite[i].move(0, carYSpeed[i]);
    checkBounds(carSprite[i], true);

    for (int j = 0; j < 4; j++) {
    // Move the blackhole sprites
    blackholeSprite[j].move(0, 0);
    checkBounds(blackholeSprite[j], true);

    // Check for a collision between the chicken and cars
    if ((chickenSprite.collidesWith(carSprite[i], true))||(chickenSprite.collidesWith(blackholeSprite[j], true))) {
    // Play a sound for losing a chicken
    try {
    squishPlayer.start();
    }
    catch (MediaException me) {
    me.printStackTrace();
    }

    // Check for a game over
    if (--numLives == 0) {
    // Stop the music and play a game over sound
    try {
    musicPlayer.stop();
    gameoverPlayer.start();
    }
    catch (MediaException me) {
    me.printStackTrace();
    }

    gameOver = true;
    } else {
    // Reset the chicken position
    chickenSprite.setPosition(2, 77);
    }

    // No need to continue updating the car sprites
    break;
    }
    }
    }
    }

    Could anyone help me? Thx!

  2. #2
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,429

    Re: Null Pointer Exception!

    Have you tried in a Nokia emulator ? In which device are you trying? Please try to locate where you are getting the exception. Since there is no full code, I could not even compile and test the app.

    Regards
    Gopal

  3. #3
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: Null Pointer Exception!

    Hi,
    Please tell which phone you are using......also just asking whether your images are properly displayed or some exceptions are occuring at that point.
    Thanks
    Soku

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Bonn, Germany
    Posts
    16

    Re: Null Pointer Exception!

    Most likely the first call to paint(...) happens before all the images or resources are loaded. Make sure paint returns unless all data is present.
    Moritz Voss
    MEF Mobile Entertainment Factory
    [url]http://www.m-e-f.net[/url]

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Re: Null Pointer Exception!

    I use Nokia Series 40 and Nokia 6230 phone for testing my game. But the result are the same. My game is downloaded into my 6230 phone successfully. And the images can be displayed on the screen. But the problem is Null Pointer Exception when my object is knocked down by car or obstacle and exiting the game. Thx!
    Last edited by Eric Ma; 2006-05-18 at 11:28.

  6. #6
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: Null Pointer Exception!

    can you point out where the exception occurs in the code...

    Thanks
    Soku

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Re: Null Pointer Exception!

    Sorry! I can't point out where is the problem. I just see "Application error". and click in detail that show Null Pointer Exception - java/lang/NullPointerException

  8. #8
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: Null Pointer Exception!

    Hi,
    Can u please explain the procedure for getting the exception.like playing the game and so on...If so we can figure it out.when will it come...

    Thanks
    Soku

  9. #9
    Super Contributor
    Join Date
    Mar 2006
    Posts
    516

    Re: Null Pointer Exception!

    Hi eric,

    It may be due to the players used in ur game. Can u please give it a try by commenting all the audio players used in the application.


    Wang

  10. #10
    Super Contributor
    Join Date
    Nov 2003
    Location
    Bangalore , India
    Posts
    4,429

    Re: Null Pointer Exception!

    If you still have trouble, and if you dont have security issues with your code, please post your complete code/upload files in some server, so that some one could do some
    round of debugging.

    Regards
    Gopal

  11. #11
    Regular Contributor
    Join Date
    Aug 2005
    Posts
    151

    Re: Null Pointer Exception!

    Back to Basics.

    Just Check have you specified correct names of images while craeting the images like in statement

    new Sprite(Image.createImage("/Blackhole.png"));

    As image names are case sensitive while running in the phone where as they run on the emulator.
    Cheers,
    Manan

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    Re: Null Pointer Exception!

    I agree with Wang,
    I believe the problem is with the wave players. As far as my knowledge goes S40 phones (as 6230) are not able to create a player with content type wav.
    If anyone was able to create a player for wav on a 6230 I would love to hear about it,
    The null exception you get is thus on the player.

    --tzurs

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    36

    Cool Re: Null Pointer Exception!

    Hi,
    I want to play a song through midlet with .mp3 extension.
    I have placed the song at "D:\hindi\classic\AA CHAL KE.MP3".
    I want to access it through
    p1=Manager.createPlayer(getClass().getResourceAsStream(
    "\04 MUNGDA HO MUNGDA.mp3"), "audio/mpeg").
    I mean iwant to know wheter is it right to given path is right or i have to write the full path or what else.
    I am getting javanullpointer exception error.
    So please guide me.
    Thanks in advance...

  14. #14
    Super Contributor
    Join Date
    Jan 2008
    Location
    Amravati, India
    Posts
    546

    Re: Null Pointer Exception!

    getResourceAsStream is used for reading files inside the jar file
    If you want to access files outside jar file then you will have to use JSR 75 FileConnection API with path as "file:///D:/hindi/classic/AA CHAL KE.MP3"
    Now don't waste your time in trying
    getResourceAsStream("file:///D:/hindi/classic/AA CHAL KE.MP3")
    It won't work...
    Download and read JSR 75 - PIM and FC

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Re: Null Pointer Exception!

    Could be that the format of the PNG for some reason cause the exception, on createImage(...) API.

    Try use different format of the images.

Similar Threads

  1. RecordEnumeration throwing null pointer exception
    By kralvarado in forum Mobile Java General
    Replies: 2
    Last Post: 2006-04-13, 04:07
  2. getNumRecords/ null pointer exception
    By cjmarsha in forum Mobile Java General
    Replies: 3
    Last Post: 2005-07-30, 13:09
  3. Series 60 null pointer exception
    By JohnnyC1969 in forum Mobile Java General
    Replies: 5
    Last Post: 2005-03-30, 13:26
  4. another Null pointer exception
    By kvchen1985 in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2004-06-28, 15:33
  5. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×