×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Nullpointer excp

    Hey everybody

    I have a small problem
    The Game class is the main MIDlet and Board is a Canvas.
    I can access the and draw on the Canvas from the Game class
    But I can not go from the Canvas back to the Main MIDlet.

    The Game MIDlet creates menus and options and the player can start the game from here. But I've created a softbutton to allow the user to go back to the main menu - but it gives me a null pointer excp.
    That is I dont understand that the reference in Board dosnt link back to Game??


    The Game.class


    package BackG;
    import javax.microedition.midlet.*;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.lcdui.*;

    public class Game extends MIDlet implements CommandListener
    {
    public static Board instance_Board;
    private static Game instance_Game;

    private Board the_board; /

    private boolean firstTime = true;
    private boolean firstTimeUser = true;
    public String userName;
    private static String Initial_Text = "Enter user name here - max 10 characters"; // initial text for input to username


    private Command BACK = new Command("Back",Command.BACK,0);
    private Command EXIT = new Command("Exit",Command.EXIT,1);
    private Command DONE = new Command("Done",Command.OK,1);


    private Command currentCommand = null;
    private Displayable currentDisplayable;


    private Display display;
    private TextBox inputName;
    private List mainMenu;
    public List playMenu;
    private List highscoreMenu;
    private Form privateScore;

    private Form top5;

    String userScore;
    String top5Score;

    // string arrays for menus
    String main[] =
    {
    "Username","Highscore","Play"
    };

    String highscore[]=
    {
    "Private Score", "Top 5"
    };

    String play[]=
    {
    "Play online","Local"
    };

    public Game()
    {
    super();
    display = Display.getDisplay(this);
    the_board = new Board();



    highscoreMenu = new List("Highscore",Choice.IMPLICIT,highscore,null);
    highscoreMenu.addCommand(BACK);
    highscoreMenu.setCommandListener(this);

    playMenu = new List("Chooce game",Choice.IMPLICIT,play, null);
    playMenu.addCommand(BACK);
    playMenu.setCommandListener(this);

    inputName = new TextBox("Profile",Initial_Text,100,0);
    inputName.addCommand(DONE);
    inputName.addCommand(BACK);
    inputName.setCommandListener(this);

    userScore = "No scores stored";
    top5Score = "No scores to see";

    privateScore = new Form("Score");
    privateScore.append(userScore);
    privateScore.addCommand(DONE);
    privateScore.setCommandListener(this);

    top5 = new Form("Top 5 in the Wold!");
    top5.append(top5Score);
    top5.addCommand(DONE);
    top5.setCommandListener(this);

    firstTime = true;

    instance_Board = the_board;
    instance_Game = this;


    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {

    inputName = null;
    Initial_Text=null;
    instance_Board = null;
    }

    public void quit()
    {
    destroyApp(true);
    notifyDestroyed();
    }

    public void startApp()
    {

    if(firstTime==true)
    {
    mainMenu = new List("Main Menu",Choice.IMPLICIT,main,null);
    mainMenu.addCommand(EXIT);
    mainMenu.setCommandListener(this);
    display.setCurrent(mainMenu);

    firstTime= false;
    }
    }

    public static Board getMIDlet()
    {
    return instance_Board;
    }

    public static Game getGame()
    {
    return instance_Game;

    }

    public void commandAction(Command c, Displayable d)
    {
    currentCommand = c;
    currentDisplayable = d;

    String com_type = currentCommand.getLabel();


    if(com_type=="Back")
    {
    if(
    currentDisplayable.equals(inputName) ||
    currentDisplayable.equals(highscoreMenu) ||
    currentDisplayable.equals(playMenu)
    )
    {
    display.setCurrent(mainMenu); // show the mainMenu
    }
    }

    else if(com_type=="Exit")
    {
    quit();
    }

    else if (com_type=="Done")
    {
    if(currentDisplayable.equals(inputName))
    {
    userName = inputName.getString();
    firstTimeUser = false;
    display.setCurrent(mainMenu);
    }

    else if (currentDisplayable.equals(privateScore) || currentDisplayable.equals(top5))
    {
    display.setCurrent(highscoreMenu);
    }
    }

    else if (c== List.SELECT_COMMAND)
    {
    if(currentDisplayable.equals(mainMenu)) // options from the mainMenu
    {
    switch (mainMenu.getSelectedIndex())
    {
    case 0:
    if(firstTimeUser == false)
    {
    inputName= new TextBox("Profile",userName,10,0);
    inputName.addCommand(DONE);
    inputName.addCommand(BACK);
    inputName.setCommandListener(this);

    }
    display.setCurrent(inputName);
    break;

    case 1:// if the shown list is Submenu2
    display.setCurrent(highscoreMenu);
    break;

    case 2:
    display.setCurrent(playMenu);
    break;

    }
    }

    else if(currentDisplayable.equals(highscoreMenu)) //options from the highscoreMenu
    {
    switch (highscoreMenu.getSelectedIndex())
    {
    case 0:
    display.setCurrent(privateScore);
    break;

    case 1:
    display.setCurrent(top5);
    break;

    }
    }

    else if (currentDisplayable.equals(playMenu))
    {
    switch(playMenu.getSelectedIndex())
    {
    case 0:

    display.setCurrent(the_board);


    break;
    case 1:

    display.setCurrent(the_board);

    break;
    }
    }


    }

    }//end commandAction

    /*public void playMenu()
    {
    display.setCurrent(playMenu);
    }*/
    } // end Game.class

    ---------------------------------------------------------------------------------------

    The Board.class


    package BackG;

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

    public class Board extends Canvas implements CommandListener
    {
    private Game the_game;


    private Command BACK = new Command("Back",Command.BACK,0);
    //private Command EXIT = new Command("Exit",Command.EXIT,0);
    private Command CLEAR = new Command("Clear",Command.OK,1);
    private Display display;

    private static final int WHITE = 0xFF << 16 | 0xFF << 8 | 0xFF;

    public Board()
    {
    // There must be a call to super because this is the Canvas.class no arg constructor
    super();

    addCommand(BACK);
    //addCommand(EXIT);
    addCommand(CLEAR);
    setCommandListener(this);


    }

    public void commandAction(Command c, Displayable d)
    {
    if(c==BACK)
    {
    display.setCurrent(the_game.playMenu);

    }
    /*else if(c==EXIT)
    {
    // quit();
    }*/
    else if(c==CLEAR)
    {


    }
    }

    public void clear (Graphics g)
    {
    int clipX = g.getClipX();
    int clipY = g.getClipY();
    int clipH = g.getClipHeight();
    int clipW = g.getClipWidth();

    int color= g.getColor();
    g.setColor(WHITE);
    g.fillRect(clipX,clipY,clipW,clipH);

    g.setColor(color);
    }


    public void paint(Graphics g)
    {
    //graphics
    clear(g);

    int height = getHeight();
    int width = getWidth();
    g.drawLine(20,13,width-20,height-31);
    }

    }
    Any help is appreciated

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    34
    In your code , whilst you declare the Game "the_game", you never actually assign it.

    Your constructor should do that, so that it looks more like this:
    private Board(Game the_game){

    this.the_game = the_game;

    ...other constructor stuff...

    }

    This will give you access to the Game class from within your Board class. Also remember to change your code where you create an instnace of Board, so that a Game is passed in.

    Cheers,

    Åge Kruger

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    RE:Nullpointer

    Ive tried what you sugested but it still gives me a nullpointer exception:
    The code now looks like this: (The relevant parts)

    private static Game instance_Game;
    public static Board instance_Board=
    new Board(instance_Game);


    private Board the_board = new Board(instance_Game);


    Game Constructor

    public Game()
    {
    super();
    display = Display.getDisplay(this);

    the_board = new Board(instance_Game);

    // Instantiate the menus; mainMenu is done in startApp()

    highscoreMenu = new List("Highscore",Choice.IMPLICIT,highscore,null);
    highscoreMenu.addCommand(BACK);
    highscoreMenu.setCommandListener(this);

    playMenu = new List("Chooce game",Choice.IMPLICIT,play, null);
    playMenu.addCommand(BACK);
    playMenu.setCommandListener(this);

    inputName = new TextBox("Profile",Initial_Text,100,0);
    inputName.addCommand(DONE);
    inputName.addCommand(BACK);
    inputName.setCommandListener(this);

    userScore = "No scores stored"; // this temp, socres should be stored on device
    top5Score = "No scores to see"; // score should be downloaded from server

    privateScore = new Form("Score");
    privateScore.append(userScore);
    privateScore.addCommand(DONE);
    privateScore.setCommandListener(this);

    top5 = new Form("Top 5 in the Wold!");
    top5.append(top5Score);
    top5.addCommand(DONE);
    top5.setCommandListener(this);

    firstTime = true;

    instance_Board = the_board;
    instance_Game = this;


    }


    The Board const

    public Board(Game x)
    {
    // There must be a call to super because this is the Canvas.class no arg constructor
    super();

    addCommand(BACK);
    //addCommand(EXIT);
    addCommand(CLEAR);
    setCommandListener(this);
    this.the_game= x;


    }

    Maybe if you expound a little on your answer

    Cheers CVP

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    34

    Re: RE:Nullpointer

    Originally posted by Christian V Petersen
    private static Game instance_Game;
    public static Board instance_Board = new Board(instance_Game);

    private Board the_board = new Board(instance_Game);
    And there's your new problem.

    You haven't instantiated the Game instance_Game before you send it to the Board constructor, so effectively, you've passed in null.

    I would also say that where you're instantiating the Board the_board is flawed. It's probably best to do that in StartApp(), since that is where the MIDlet is started from. (You don't actually need a cosntructor for your Game class, BTW, StartApp() works like one, and you really should only ever have one instance of Game class ever).

    So what does that mean for your code?

    private Board the_board;

    StartApp(){

    if(firstTime==true){

    mainMenu = new List("Main Menu",Choice.IMPLICIT,main,null);
    mainMenu.addCommand(EXIT);
    mainMenu.setCommandListener(this);
    display.setCurrent(mainMenu);

    firstTime= false;

    the_board = new Board(this);

    }

    }

    The other option, which is perhaps better from a style point of view, is to create a method called "showBoard()" which will change the display. In this method you can instantiate the Board the_board if it's null, or set the display to it otherwise. I'd also stop accessing the game via that getGame() method. It's not really good practice, and probably will cause some problems.

    I hope that helps explain things a bit further.

    Have fun,

    Åge Kruger

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    16

    Re:

    Great now it works
    Thanks alot - it was really simple when I understod it

    Cheers CVP

Posting Permissions

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