×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Un Explainable NullPointerException

    hey guys n gals,

    Im having a problem with a new menu approach i found on the net here the link to the blog its on.

    basically i have a canvas that paints seperate 'screens'. the splash screen works fine but when i come to the menu screen i get the exception. however, the screen is still painted and i just cant see why this exception is produced

    The canvas class:
    Code:
        protected void paint(Graphics g) 
        {
            if(GameStatus.SplashScreen)
                {
                    this.splashScr.drawObject(g);
                }
                else if(GameStatus.Menu)
                {
                    try
                    {
                    this.menuScr.drawObject(g); // THIS IS THE LINE CAUSING THE PROBLEM
                    }
                    catch(NullPointerException e)
                    {
                        e.printStackTrace();
                    }
                }
        }
    The 'Screen' class is question:
    Code:
    public void drawObject(Graphics g) 
        {
            g.drawImage(bgImg, 0, 0, 0);
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
            
            String[] menuItems = 
            {
                MenuItems.NEW_GAME,
                MenuItems.OPTIONS,
                MenuItems.HELP,
                MenuItems.ABOUT,
                MenuItems.EXIT
            };
            
            for(int i = 0; i < menuItems.length; i++)
            {
                g.setColor(255, 255, 255);
                if(i == menuIndex)
                {
                    g.setColor(255, 0, 0);
                }
                g.drawString(menuItems[i], 80, 130 + (i * 20), 0);
            }
        }
    Made With Caffiene And Sleepless Nights

  2. #2
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,405

    Re: Un Explainable NullPointerException

    Hi rooossone,

    have you checked if your menuScr variable is correctly initialized (so it is not null)?

    Pit

  3. #3
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: Un Explainable NullPointerException

    yeah its deffinately initialised because i can see that it has painted everything to the screen, the exception jumps up after everything has been painted, if also put in System.out's before and after every step in that method and i get the exception before i get any of the System.outs

    Code:
    public void drawObject(Graphics g) 
        {
            g.drawImage(bgImg, 0, 0, 0);
            System.out.println("bg painted fine");
            g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
            System.out.println("font set");
            String[] menuItems = 
            {
                MenuItems.NEW_GAME,
                MenuItems.OPTIONS,
                MenuItems.HELP,
                MenuItems.ABOUT,
                MenuItems.EXIT
            };
            System.out.println("array set");
            for(int i = 0; i < menuItems.length; i++)
            {
                System.out.println("element " + i);
                g.setColor(255, 255, 255);
                System.out.println("element color set");
                if(i == menuIndex)
                {
                    System.out.println("menu index element set");
                    g.setColor(255, 0, 0);
                    System.out.println("selected color set");
                }
                System.out.println("element " + i + "prepare");
                g.drawString(menuItems[i], 80, 130 + (i * 20), 0);
                System.out.println("element " + i + "success");
            }
            System.out.println("loop finished");
        }
    ive also printed out the ref var menuScr and i can see that it's not null (MenuPackage.MenuScreen@84aee8b)
    Made With Caffiene And Sleepless Nights

  4. #4
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: Un Explainable NullPointerException

    Hey Guys Im Still Having the same problem with my code and i could really do with a fresh set of eyes to see where i've gone wrong... I've uploaded my netbeans project for anyone to download and have a look at, any possible resolutions would be great!

    My Netbeans Project

    Thanks in advance
    Made With Caffiene And Sleepless Nights

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    24

    Re: Un Explainable NullPointerException

    hi,

    i faced the same exception problem while changing displayables on S40 devices. the same code worked fine on all other platforms ... but not on S40. i had even posted a thread to this regard on this forum ... dont have the exact link at the moment but you can search for "NullPointerException while changing displayables" along with my ID. have a look at that and see if the solution helps.
    i use a different mechanism for changing displayables - to change from Canvas1 to Canvas2, Canvas1 calls a method in the main midlet. that way all the changing of displayables is "centrally controlled" so to speak.
    check out a few points:
    1) is there any string item in your menu screen that might not have been initialised, but is being drawn?
    2) ditto for image items
    3) try putting a Thread.Sleep() call at the point just before you paint the menu screen. (as i've mentioned in my other thread) ... this would allow any thread running in the splashscreen to terminate properly

    the Thread.Sleep() worked fine for me and since then i've not encountered this problem. however as i said, my technique for changing displayables is quite different from the approach you have used. incidentally, i noticed another aspect when i had faced the error - the exception is thrown only when you change between two Canvas classes ... the approach worked fine when i switched from the Canvas to a form class.

    ~ayush


    Quote Originally Posted by rooossone View Post
    Hey Guys Im Still Having the same problem with my code and i could really do with a fresh set of eyes to see where i've gone wrong... I've uploaded my netbeans project for anyone to download and have a look at, any possible resolutions would be great!

    My Netbeans Project

    Thanks in advance

  6. #6
    Regular Contributor
    Join Date
    Aug 2008
    Location
    Bournemouth, UK
    Posts
    65

    Re: Un Explainable NullPointerException

    Hey I managed to sort that problem out by putting the menuScr initialisation in the canvas constructor. not ideal but it works. now i have a problem when closing and re-opening the app within the emulator. when i close it i set the bgImg in menuScr to null, set the game loop boolean to false and call notifyDestroyed() but when i goto re-open the app i get a null pointer exception for bgImg even though i intialise it in the menuScr constructor so why is it saying its null?
    Made With Caffiene And Sleepless Nights

Similar Threads

  1. Using RecordStore.AUTHMODE_ANY throws NullPointerException
    By sgaber in forum Mobile Java General
    Replies: 10
    Last Post: 2007-12-18, 18:03
  2. recordcontrol.setStream(out) throws NullPointerException
    By bharatdevada in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2006-11-10, 01:16
  3. Weird NullPointerException
    By baegsi in forum Mobile Java General
    Replies: 8
    Last Post: 2003-12-19, 11:12
  4. NullPointerException
    By cwfvanoordt in forum Mobile Java General
    Replies: 1
    Last Post: 2003-11-14, 20:20
  5. How to display trace stack for nullPointerException?
    By kokohead in forum Mobile Java General
    Replies: 1
    Last Post: 2003-02-22, 20:34

Posting Permissions

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