Avatar billede s991284 Nybegynder
07. april 2005 - 12:56 Der er 1 kommentar og
1 løsning

J2ME: NullPointerException

Hej
Jeg har et problem. Det hele drejer sig om et spil, hvori man fra en hovedmenu kan navigere igennem flere forskellige menuer.
Den væsentligste menu er dog PLAY og her opstår problemet.
Det fungerer således; jeg opretter et Canvas som jeg tegner grafik på. Dette Canvas aktiveres i menuen playMenu og herefter skal spillet starte.
På dette Canvas har jeg oprettet to kommandoer Back og Clear.
Det er Back-com der ikke virker som den skal. (Clear defineres senere)
Når brugeren trykker på Back skal man således få vist playMenu igen men her opstår en nullpointer exception

Min Kode ser sådan ud:

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


public class Game extends MIDlet implements CommandListener
{
    private static Game instance_Game;
    public static Board instance_Board= new Board(instance_Game);
   
   
    private Board the_board = new Board(this);    // the subclass of Canvas that displays the board and runs the actual game
   
    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
   
    //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;
    public 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()
    {
        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;
       
       
    }
     
    public void pauseApp()
    {
    }
   
    public void destroyApp(boolean unconditional)
    {
        // should be changed to support storage
        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);  // no back command needed
            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();
       
        //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 (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 the shown list is Submenu1
                      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:
                        //Play online function
                        display.setCurrent(the_board);
                        // online var should be true
                       
                        break;
                    case 1:
                        //  Play local function
                        display.setCurrent(the_board);
                        // offline var should be true
                        break;
                }
            }
           
         
        }
       
    }//end commandAction
 
 
} // end Game.class
-------------------------------------
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(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;
       
       
    }
   
    public void commandAction(Command c, Displayable d)
    {
        if(c==BACK)
        {
            display.setCurrent(the_game.getGame().playMenu);
           
        }
        /*else if(c==EXIT)
        {
          // quit();       
        }*/
        else if(c==CLEAR)
        {
                     
       
        }
    }
   
    // this func is used to clear the Canvas
    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);
    }
   
}
Avatar billede s991284 Nybegynder
07. april 2005 - 14:04 #1
Jeg ved det :)
Avatar billede bootie Nybegynder
07. april 2005 - 23:12 #2
Nogle gange hjælper det at spørge om hjælp! :)
Hvad løste det så?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester