Hi I'm trying to write a multi-formed J2ME app. I keep getting a null pointer exception in the startApp() method.

public void startApp()
{
    display.setCurrent(currentForm);
}

I have currentForm created above and the first is mapped to it, (with the plan of mapping each new from to currentForm.

private Form currentForm, login; //etc
currentForm = login;

I have login set up, so I'm really confused why I getting this error. :confused:

loginTF = new TextField[2];
loginTF[0] = new TextField("Staff Number:", "", 10, TextField.NUMERIC);
loginTF[1] = new TextField("Password:", "", 50, TextField.PASSWORD);
login = new Form("login", loginTF);
login.addCommand(submit);
login.addCommand(exit);
login.addCommand(clear);
login.setCommandListener(this);

Everything above has been declared as usual. Any suggestions as to what I'm doing wrong?

Thanks, chuck

Nope, sadly not. It's set up as usual:

Display display;
//...
display = Display.getDisplay(this);

Thanks, any other ideas?

Some more code is needed. According to the method showed (in a cupsule like it was) display definately would be null, since there is no evidence otherwise. Post the entire class.

It's a pretty big class, but I'll try to cut out the irrevelant parts.

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

public class MyClient extends MIDlet implements CommandListener
{
/*********************Widget declarations***************************************/
    private Display display;
    private Form currentForm, main, login; //more, but not relivant
    private StringItem options[];
    private TextField loginTF[], cleanUpTF[], patientBill[], wardInfo[], patientDetails[];
    private TextField securityDetails[], securityTF[], medAlertTF[];
    private Command exit, select, clear, submit, remove, mainMenu, first;
    private Command previous, next, last, generateBill, findPatient, update, report;
/****************************Constructor****************************************/
    public void myClient()
    {
        display = Display.getDisplay(this);
/**************************Commands*********************************************/
        //comands initialised

/********************Login Screen***********************************************/
        loginTF = new TextField[2];
        loginTF[0] = new TextField("Staff Number:", "", 10, TextField.NUMERIC);
        loginTF[1] = new TextField("Password:", "", 50, TextField.PASSWORD);
        login = new Form("login", loginTF);
        login.addCommand(submit);
        login.addCommand(exit);
        login.addCommand(clear);
        login.setCommandListener(this);

        currentForm = login;
    }
/*********************Displaying The First Screen*******************************/
    public void startApp()
    {
        display.setCurrent(currentForm);
    }
/*********************Pausing The Application***********************************/
    public void pauseApp()
    {}
/**************************Destroying The Application***************************/
    public void destroyApp(boolean unconditional)
    {}

}
/******************************End of Class*************************************/

What about the Class Display.

Is it at all possible that display = Display.getDisplay(this); returns null?

Please provide this getDisplay method, or if it comes from some thrid party API, then please provide the JavaDoc from this method.

The Display method has worked for me half a dozen time so far, so it can't be that. (I even copied and pasted it in from a working program)

And that says nothing. Those classes are those classes, this class is this class. If you do not wish to provide the info we would need to help you, then at least don't waste our time.

Display comes from:
javax.microedition.lcdui.Display

Which is a default class that the compiler recognises and I have not over-written.

The api is available on:
http://kickjava.com/3205.htm#33462

This is what the api says:

public static Display [B]getDisplay[/B](MIDlet m)

This is what I did:

class MyClass extends MIDlet
{
...
Display display;
Form currentForm,login;
TextField loginTF[];
...
public void MyClass
{
display = Display.getDisplay(this);
...
loginTF = new TextField[2];
loginTF[0] = new TextField("Staff Number:", "", 10, TextField.NUMERIC);
loginTF[1] = new TextField("Password:", "", 50, TextField.PASSWORD);
login = new Form("login", loginTF);
...
currentForm = login;
}
...
public void startApp()
{
display.setCurrent(currentForm);
}

According to the documentation for Form and Display, and if this is actually the line throwing the null pointer exception, then display is the only thing that even can be null, since the setCurrent method will accept a null value, so currentForm is allowed to be null.

Take out all the dots and attempt to run the Class exactly as it is portrayed here. If that works, then the problem lies somewhere else in your code. Maybe there is some process somewhere in your class that sets display to null again. I don't know since I can't see the rest of the code.

As I said, however, reading the Display and Form API pages from this lcdui package, indicates that display is the only thing in that line that even could cause a NullPointerExcpetion.

It's really odd. It's definately not a case of display being set to null. Thanks for the help that you're giving. Here's the full code:

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

public class NurseClient extends MIDlet implements CommandListener
{



/*********************Widget declarations***************************************/



    private Display display;
    private Form currentForm, main, login, cleanUp, bill, ward, patient, securityReport, security, medical;
    private StringItem options[];
    private TextField loginTF[], cleanUpTF[], patientBill[], wardInfo[], patientDetails[];
    private TextField securityDetails[], securityTF[], medAlertTF[];
    private Command exit, select, clear, submit, remove, mainMenu, first;
    private Command previous, next, last, generateBill, findPatient, update, report;



/****************************Constructor****************************************/



    public void NurseClient()
    {
        display = Display.getDisplay(this);



/**************************Commands*********************************************/



        exit = new Command("Exit", Command.EXIT, 1);
        mainMenu = new Command("mainMenu", Command.ITEM, 1);
        select = new Command("Select", Command.ITEM, 1);
        clear = new Command("Clear", Command.ITEM, 1);
        submit = new Command("Submit", Command.ITEM, 1);
        remove = new Command("Remove", Command.ITEM, 1);
        report = new Command("File Report", Command.ITEM, 1);
        update = new Command("Update Record", Command.ITEM, 1);
        findPatient = new Command("Find Patient", Command.ITEM, 1);
        generateBill = new Command("Generate Bill", Command.ITEM, 1);
        first = new Command("First", Command.ITEM, 1);
        previous = new Command("Previous", Command.ITEM, 1);
        next = new Command("Next", Command.ITEM, 1);
        last = new Command("Last", Command.ITEM, 1);



/**********************Main Menu Screen*****************************************/



        options = new StringItem[7];
        options[0] = new StringItem("Options", "Options");
        options[1] = new StringItem("Clean Up Alert", "Clean Up Alert");
        options[2] = new StringItem("Security Alert", "Security Alert");
        options[3] = new StringItem("Patient Record", "Patient Record");
        options[4] = new StringItem("Bill Generation", "Bill Generation");
        options[5] = new StringItem("Ward Details", "Ward Details");
        options[6] = new StringItem("Medical Alert", "Medical Alert");
        main = new Form("options", options);
        main.addCommand(exit);
        main.addCommand(select);
        main.setCommandListener(this);



/********************Login Screen***********************************************/



        loginTF = new TextField[2];
        loginTF[0] = new TextField("Staff Number:", "", 10, TextField.NUMERIC);
        loginTF[1] = new TextField("Password:", "", 50, TextField.PASSWORD);
        login = new Form("login", loginTF);
        login.addCommand(submit);
        login.addCommand(exit);
        login.addCommand(clear);
        login.setCommandListener(this);



/*******************Clean Up Screen*********************************************/



        cleanUpTF = new TextField[2];
        cleanUpTF[0] = new TextField("Room:", "", 3, TextField.NUMERIC);
        cleanUpTF[1] = new TextField("Floor:", "", 3, TextField.NUMERIC);
        cleanUp = new Form("Clean Up Call", cleanUpTF);
        cleanUp.addCommand(submit);
        cleanUp.addCommand(exit);
        cleanUp.addCommand(clear);
        cleanUp.addCommand(remove);
        cleanUp.setCommandListener(this);



/**********************Patient Bill Screen**************************************/



        patientBill = new TextField[4];
        patientBill[0] = new TextField("First Name:", "", 30, TextField.ANY);
        patientBill[1] = new TextField("Last Name:", "", 30, TextField.ANY);
        patientBill[2] = new TextField("Total Due:", "", 4, TextField.NUMERIC);
        patientBill[3] = new TextField("Balance:", "", 4, TextField.NUMERIC);
        bill = new Form("Patient Bills", patientBill);
        bill.addCommand(generateBill);
        bill.addCommand(mainMenu);
        bill.addCommand(first);
        bill.addCommand(last);
        bill.addCommand(next);
        bill.addCommand(previous);
        bill.setCommandListener(this);



/*********************Ward Info Screen******************************************/



        wardInfo = new TextField[2];
        wardInfo[0] = new TextField("Ward No:", "", 3, TextField.NUMERIC);
        wardInfo[1] = new TextField("Free Beds:", "", 3, TextField.NUMERIC);
        ward = new Form("Ward Details", wardInfo);
        ward.addCommand(mainMenu);
        ward.addCommand(first);
        ward.addCommand(last);
        ward.addCommand(next);
        ward.addCommand(previous);
        ward.setCommandListener(this);



/*********************Patient Record Screen*************************************/



        patientDetails = new TextField[7];
        patientDetails[0] = new TextField("First Name:", "", 30, TextField.ANY);
        patientDetails[1] = new TextField("Last Name:", "", 30, TextField.ANY);
        patientDetails[2] = new TextField("Date of Birth:", "", 20, TextField.ANY);
        patientDetails[3] = new TextField("Blood Type:", "", 30, TextField.ANY);
        patientDetails[4] = new TextField("Allergies:", "", 200, TextField.ANY);
        patientDetails[5] = new TextField("Current Treatment:", "", 200, TextField.ANY);
        patientDetails[6] = new TextField("Next of Kin:", "", 100, TextField.ANY);
        patient = new Form("Patient Records", patientDetails);
        patient.addCommand(findPatient);
        patient.addCommand(update);
        patient.addCommand(mainMenu);
        patient.addCommand(first);
        patient.addCommand(last);
        patient.addCommand(next);
        patient.addCommand(previous);
        patient.setCommandListener(this);



/**********************Security Report Screen***********************************/



        securityDetails = new TextField[3];
        securityDetails[0] = new TextField("Security Alert No:", "", 5, TextField.NUMERIC);
        securityDetails[1] = new TextField("Call Date:", "", 12, TextField.ANY);
        securityDetails[2] = new TextField("Report:", "", 400, TextField.ANY);
        securityReport = new Form("Security Records", securityDetails);
        securityReport.addCommand(remove);
        securityReport.addCommand(update);
        securityReport.addCommand(mainMenu);
        securityReport.addCommand(first);
        securityReport.addCommand(last);
        securityReport.addCommand(next);
        securityReport.addCommand(previous);
        securityReport.setCommandListener(this);



/******************Security Alert Screen****************************************/



        securityTF = new TextField[2];
        securityTF[0] = new TextField("Room:", "", 3, TextField.NUMERIC);
        securityTF[1] = new TextField("Floor:", "", 3, TextField.NUMERIC);
        security = new Form("Security Call", securityTF);
        security.addCommand(submit);
        security.addCommand(exit);
        security.addCommand(clear);
        security.addCommand(report);
        security.setCommandListener(this);



/*****************Medical Alert Screen******************************************/



        medAlertTF = new TextField[2];
        medAlertTF[0] = new TextField("Room:", "", 3, TextField.NUMERIC);
        medAlertTF[1] = new TextField("Floor:", "", 3, TextField.NUMERIC);
        medical.addCommand(submit);
        medical.addCommand(exit);
        medical.addCommand(clear);
        medical.addCommand(remove);
        medical.setCommandListener(this);

        
        currentForm = new Form("");
        currentForm = login;
    }



/*********************Displaying The First Screen*******************************/



    public void startApp()
    {
        display.setCurrent(currentForm);
    }



/*********************Pausing The Application***********************************/



    public void pauseApp()
    {}



/**************************Destroying The Application***************************/



    public void destroyApp(boolean unconditional)
    {}



/*************************Commands Functionality********************************/



    public void commandAction(Command command, Displayable displayable)
    {



/*************************Exit**************************************************/



        if (command == exit)
        {
            destroyApp(true);
            notifyDestroyed();
        }



/************************Clear**************************************************/



        else if(command == clear)
        {
            if (currentForm == login)
            {
                loginTF[0].setString("");
                loginTF[1].setString("");
            }
            else if (currentForm == cleanUp)
            {
                cleanUpTF[0].setString("");
                cleanUpTF[1].setString("");
            }
            else if (currentForm == security)
            {
                securityTF[0].setString("");
                securityTF[1].setString("");
            }
            else if (currentForm == medical)
            {
                medAlertTF[0].setString("");
                medAlertTF[1].setString("");
            }
        }



/**********************Select***************************************************/



        else if (command == select && currentForm == main)
        {/*
            String s = options.getString(options.getSelectIndex());

            if (s == "Clean Up Alert")
            {
                currentForm = cleanUp; 
            }    
            else if (s == "Security Alert")
            {
                currentForm = security;
            }
            else if (s == "Patient Record")
            {
                currentForm = patient;
            }
            else if (s == "Bill Generation")
            {
                currentForm = bill;
            }
            else if (s == "Ward Details")
            {
                currentForm = ward;
            }
            else if (s == "Medical Alert")
            {
                currentForm = medical;
            }*/
        }



/**************************Main Menu********************************************/



        else if (command == mainMenu)
        {
            currentForm = main;
        }
    }
}



/*******************************************************************************/
/******************************End of Class*************************************/
/*******************************************************************************/

Strange. The only thing I can suggest now, is to try running it in a debugger and observe what happens. I can't really suggest a debugger for a MIDlet as I have not tried any.

The only other suggestion is to make sure that that line is actually the one throwing an NPE.

And, as a general suggestion, to clean up the code a bit, I would move those commented parts of the constructor into their own private methods (one per commented part), and then call those methods from the constructor.

did you try to use netbeans mobility? you can debug your midlets there...
masijade can be right, NPE may be thrown somewhere else...

good luck

i found your problem...

/****************************Constructor****************************************/



    public void NurseClient()
    {
        display = Display.getDisplay(this);

this is not a constructor.... :) i think you put void mistakenly

i found your problem...

this is not a constructor.... :) i think you put void mistakenly

Aach. Completely overlooked that. Yes, that is the problem. And also the reason why display is null, because since that is not a constructor, display never gets initialised.

I am such a fool sometimes.

This question has already been answered. Start a new discussion instead.