×

Discussion Board

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

    Nullpointer Execp in startApp()

    Hey Everybody Im new at J2ME prog.
    My compiler, Netbeans 3.6, tells me that my startApp() func throws a null pointer execption and I can not figure out why...

    Is it because there is no instance of my MIDlet ???
    So if anyone could help it would be great....


    The code looks like this:
    /*
    * Game.java
    *
    * Created on 3. marts 2005, 19:08
    */
    package BackG;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    /**
    *
    * @author Christian V Petersen
    * @version
    *This MIDlet creates the main menus and paths to the different game-functions
    *The exact game-functionality is placed in ....
    */
    public class Game extends MIDlet implements CommandListener
    {
    private boolean firstTime;

    public String userName;
    private static String Intial_Text = "Enter user name here - max 10 characters"; // initial text for input to username

    //Commands
    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);
    // all other commands are from List.getSelectedIndex()

    private Command currentCommand = null;
    private Displayable currentDisplayable;

    //Dipslayables: menuLists and screens
    private Display display;
    private TextBox inputName;
    private List mainMenu;
    private List playMenu;
    private List highscoreMenu;
    private Form privateScore;
    //private TextField userScore;
    private Form top5;
    //private TextField top5Score;
    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()
    {
    display = Display.getDisplay(this);

    // 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);

    userScore = "No scores stored"; // this temp, socres should be stored on device
    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;
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    // should be changed to support storage
    inputName = null;
    Intial_Text=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); // no back command needed
    mainMenu.setCommandListener(this);
    display.setCurrent(mainMenu);

    firstTime= false;
    }
    }

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

    String com_type = currentCommand.getLabel();

    //All the commands must be checked
    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 (c== List.SELECT_COMMAND)
    {
    if(currentDisplayable.equals(mainMenu)) // options from the mainMenu
    {
    switch (mainMenu.getSelectedIndex())
    {
    case 0:// if the shown list is Submenu1
    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:
    //Play online
    break;
    case 1:
    // Play local
    break;
    }
    }
    }

    }//end commandAction
    } // end Game.class

  2. #2
    Regular Contributor
    Join Date
    Sep 2003
    Location
    Finland
    Posts
    99
    I don't know, but I would try to set the firstTry = true when declaring the variable.... I seems that is the only thing that could cause the exception...

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Code works correctly

    Hi, I copied your code to eclipse with nokia sdk. It compiles perfectly and runs on the emulator.
    There is no nullpointer.

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

    Thanks

    Hey

    I figure it out ...

    I belive I created a project wish in the end should create a JAR file. When I pasted the code into an empty class there was no problem.

    Thank you for the help

Posting Permissions

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