Hi,
I am developing a J2ME application but i have some trouble when I want to display a new form (contained in a class).

My Midlet class is like that

public class StoreDataMidlet extends MIDlet{

    public StoreDataConnex StoreInfosConnex ;
    private static Display mDisplay;
    private ChaineConnexion connexForm; // Form which is opened when my Midlet starts


    public void StoreDataMidlet() {


    }

    public void startApp() {

        mDisplay = Display.getDisplay(this);

        if (connexForm == null) {

                connexForm = new ChaineConnexion(this);

        } else {
                connexForm.DrawComponents(); // DrawComponents is the method which initialize my form components

        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

 /*    public static Display getDisplay () 
{
    return mDisplay;
}*/


}

My Midlet starts with the second class wich contains two commands: the first one opens an Alert and the second has to open an other Form in a third class. But it doesn't work. I don't know why the third form cant be opened (It appears: NullPointerException). My the second class looks like below

public class ChaineConnexion  implements CommandListener{

    private Display mDisplay;
    public Command mOkCommand;
    public Command mExitCommand;
    public TextField usernameTextField;
    public TextField passwordTextField;
    private ConnexionForm connexForm;
    private StoreDataMidlet midlet ;
    private Form formulaireConnexion;


    public ChaineConnexion(StoreDataMidlet midlet) {
        mDisplay = Display.getDisplay(midlet);
        DrawComponents();
        mDisplay.setCurrent(formulaireConnexion);



    }
    public void DrawComponents()
    {
           formulaireConnexion = new Form("Formulaire Connexion");

            usernameTextField = new TextField("Login", "user", 20, TextField.ANY);
            passwordTextField = new TextField("Mot de Passe", "user", 20, TextField.PASSWORD);
            formulaireConnexion.append(usernameTextField);
            formulaireConnexion.append(passwordTextField);

            mOkCommand = new Command("Valider", Command.ITEM, 0);
            mExitCommand = new Command("Quitter", Command.EXIT, 0);

            formulaireConnexion.addCommand(mExitCommand);
            formulaireConnexion.addCommand(mOkCommand);

            formulaireConnexion.setCommandListener(this);

    }

    public void commandAction(Command c, Displayable d) {
       if (c == mOkCommand){
               showAlert("Succes","Login et Mot de passe changé"); // Alert appears but 

       } else if (c == this.mExitCommand){
            connexForm = new ConnexionForm(midlet);                // This form doesnt work

       }
    }

    private void showAlert( String title, String message) 
{
    //Display display = Display.getDisplay(midlet);
    Alert alert = new Alert(title);
    alert.setString(message);
    alert.setTimeout(Alert.FOREVER);
    mDisplay.setCurrent(alert);  // this line has been changed
}
}

My third class that doesnt appear is like below

public class ConnexionForm implements CommandListener  {

    private Display mDisplay;
    public Command mConnectionCommand;
    public Command mExitCommand;
    public TextField usernameTextField;
    public TextField passwordTextField;
    private StoreDataConnex storeData ;
    private Alert  alert;
    public Form formulaireConnexion;
    private StoreDataMidlet midlet;


    public ConnexionForm(StoreDataMidlet midlet) throws RecordStoreException{
        mDisplay = Display.getDisplay(midlet);
        DrawComponents();
        mDisplay.setCurrent(formulaireConnexion);

    }

    public void DrawComponents() throws RecordStoreException{
        try {


            formulaireConnexion = new Form("Formulaire Connexion");
            usernameTextField = new TextField("Login", storeData.getLogin(), 20, TextField.ANY);
            passwordTextField = new TextField("Mot de Passe", storeData.getPassword(), 20, TextField.PASSWORD);
            formulaireConnexion.append(usernameTextField);
            formulaireConnexion.append(passwordTextField);

            mConnectionCommand = new Command("Connexion", Command.ITEM, 0);
            mExitCommand = new Command("Quitter", Command.EXIT, 0);

            formulaireConnexion.addCommand(mExitCommand);
            formulaireConnexion.addCommand(mConnectionCommand);
         //   formulaireConnexion.setCommandListener(this);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void commandAction(Command c, Displayable d) {

        if (c == mConnectionCommand) {

        } else if(c == mExitCommand) {

            midlet.notifyDestroyed();
        }

    }

}

Please help me to know what wrong with my code!!!

StoreDataMidlet variable named "midlet" is null when it comes into the third form, you never initialize it to anything in ChaineConnexion. The midlet received in the constructor of ChaineConnexion is never stored in your local private variable, hence the NPE exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.