| | |
Java Null Pointer Exception
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 27
Reputation:
Solved Threads: 1
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:
Java Syntax (Toggle Plain Text)
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*************************************/ /*******************************************************************************/
Last edited by mr.sweetchuck; Mar 5th, 2007 at 10:49 am.
Nel sogni, come in amore, non ci sono cose impossible
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.
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
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
masijade can be right, NPE may be thrown somewhere else...
good luck
i found your problem...
this is not a constructor....
i think you put void mistakenly
•
•
•
•
Java Syntax (Toggle Plain Text)
/****************************Constructor****************************************/ public void NurseClient() { display = Display.getDisplay(this);
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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- null pointer exception problem (Java)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
- Previous Thread: PLEASE HELP, Duplication in Arrays
- Next Thread: Java run time enviorment
| Thread Tools | Search this Thread |
android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database db defaultmethod development dice dragging draw ebook eclipse error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx oracle parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows






