Java Null Pointer Exception

Thread Solved
Reply

Join Date: Feb 2007
Posts: 27
Reputation: mr.sweetchuck is an unknown quantity at this point 
Solved Threads: 1
mr.sweetchuck mr.sweetchuck is offline Offline
Light Poster

Java Null Pointer Exception

 
0
  #1
Mar 1st, 2007
Hi I'm trying to write a multi-formed J2ME app. I keep getting a null pointer exception in the startApp() method.

  1.  
  2. public void startApp()
  3. {
  4. display.setCurrent(currentForm);
  5. }

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

  1. private Form currentForm, login; //etc

  1. currentForm = login;

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


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

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

Thanks, chuck
Nel sogni, come in amore, non ci sono cose impossible
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,280
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java Null Pointer Exception

 
0
  #2
Mar 1st, 2007
Maybe the NullPointerException refers to "display".
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 27
Reputation: mr.sweetchuck is an unknown quantity at this point 
Solved Threads: 1
mr.sweetchuck mr.sweetchuck is offline Offline
Light Poster

Re: Java Null Pointer Exception

 
0
  #3
Mar 1st, 2007
Nope, sadly not. It's set up as usual:

  1. Display display;
  2. //...
  3. display = Display.getDisplay(this);

Thanks, any other ideas?
Last edited by mr.sweetchuck; Mar 1st, 2007 at 1:56 pm.
Nel sogni, come in amore, non ci sono cose impossible
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,280
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java Null Pointer Exception

 
0
  #4
Mar 2nd, 2007
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.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 27
Reputation: mr.sweetchuck is an unknown quantity at this point 
Solved Threads: 1
mr.sweetchuck mr.sweetchuck is offline Offline
Light Poster

Re: Java Null Pointer Exception

 
0
  #5
Mar 2nd, 2007
It's a pretty big class, but I'll try to cut out the irrevelant parts.

  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3.  
  4. public class MyClient extends MIDlet implements CommandListener
  5. {
  6. /*********************Widget declarations***************************************/
  7. private Display display;
  8. private Form currentForm, main, login; //more, but not relivant
  9. private StringItem options[];
  10. private TextField loginTF[], cleanUpTF[], patientBill[], wardInfo[], patientDetails[];
  11. private TextField securityDetails[], securityTF[], medAlertTF[];
  12. private Command exit, select, clear, submit, remove, mainMenu, first;
  13. private Command previous, next, last, generateBill, findPatient, update, report;
  14. /****************************Constructor****************************************/
  15. public void myClient()
  16. {
  17. display = Display.getDisplay(this);
  18. /**************************Commands*********************************************/
  19. //comands initialised
  20.  
  21. /********************Login Screen***********************************************/
  22. loginTF = new TextField[2];
  23. loginTF[0] = new TextField("Staff Number:", "", 10, TextField.NUMERIC);
  24. loginTF[1] = new TextField("Password:", "", 50, TextField.PASSWORD);
  25. login = new Form("login", loginTF);
  26. login.addCommand(submit);
  27. login.addCommand(exit);
  28. login.addCommand(clear);
  29. login.setCommandListener(this);
  30.  
  31. currentForm = login;
  32. }
  33. /*********************Displaying The First Screen*******************************/
  34. public void startApp()
  35. {
  36. display.setCurrent(currentForm);
  37. }
  38. /*********************Pausing The Application***********************************/
  39. public void pauseApp()
  40. {}
  41. /**************************Destroying The Application***************************/
  42. public void destroyApp(boolean unconditional)
  43. {}
  44.  
  45. }
  46. /******************************End of Class*************************************/
Last edited by mr.sweetchuck; Mar 2nd, 2007 at 1:07 pm. Reason: removing irelivant code
Nel sogni, come in amore, non ci sono cose impossible
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,280
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java Null Pointer Exception

 
0
  #6
Mar 2nd, 2007
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.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 27
Reputation: mr.sweetchuck is an unknown quantity at this point 
Solved Threads: 1
mr.sweetchuck mr.sweetchuck is offline Offline
Light Poster

Re: Java Null Pointer Exception

 
0
  #7
Mar 5th, 2007
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)
Nel sogni, come in amore, non ci sono cose impossible
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,280
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java Null Pointer Exception

 
0
  #8
Mar 5th, 2007
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.
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 27
Reputation: mr.sweetchuck is an unknown quantity at this point 
Solved Threads: 1
mr.sweetchuck mr.sweetchuck is offline Offline
Light Poster

Re: Java Null Pointer Exception

 
0
  #9
Mar 5th, 2007
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 getDisplay(MIDlet m)

This is what I did:
  1. class MyClass extends MIDlet
  2. {
  3. ...
  4. Display display;
  5. Form currentForm,login;
  6. TextField loginTF[];
  7. ...
  8. public void MyClass
  9. {
  10. display = Display.getDisplay(this);
  11. ...
  12. loginTF = new TextField[2];
  13. loginTF[0] = new TextField("Staff Number:", "", 10, TextField.NUMERIC);
  14. loginTF[1] = new TextField("Password:", "", 50, TextField.PASSWORD);
  15. login = new Form("login", loginTF);
  16. ...
  17. currentForm = login;
  18. }
  19. ...
  20. public void startApp()
  21. {
  22. display.setCurrent(currentForm);
  23. }
Nel sogni, come in amore, non ci sono cose impossible
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,280
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 242
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Java Null Pointer Exception

 
0
  #10
Mar 5th, 2007
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.
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC