×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    80

    java.lang.NullPointerException, how to handle ?

    Can anybody point me how can I resolve the null pointer problem in general?

    If I run simulator I get the following call stack in the console. Does it mean I have reference to null object in line 409? I don't see any problem in line 409

    >> calculateUrgentTime::calculateUrgentTime()
    >> calculateUrgentTime::CalculateUrgentTimeL()
    java.lang.NullPointerException
    at urgentpackage.calculateUrgentTime.CalculateUrgentTimeL(calculateUrgentTime.java:409)
    at urgentpackage.calculateUrgentTime.<init>(calculateUrgentTime.java:139)
    at urgentpackage.Midlet.startApp(Midlet.java:53)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    null
    null
    null

    // line 409 contain this code
    String buftimezone = iSetting.getTimeZone();


    // getTime zone is like this and iTimeZone is not null
    public String getTimeZone()
    {
    return iTimeZone;
    }

    If I run with debugger then app crashes (at least it shows) totally wrong places. It crashes at simple debug line (System.out.println("m_UrgentTimeArray[2] = " + m_UrgentTimeArray[2].toString())

    Any tips, I have spent already some time and looks clueless

  2. #2
    Registered User
    Join Date
    May 2008
    Location
    Surat Thani
    Posts
    258

    Re: java.lang.NullPointerException, how to handle ?

    Quote Originally Posted by aktarjahan2003 View Post
    java.lang.NullPointerException
    at urgentpackage.calculateUrgentTime.CalculateUrgentTimeL(calculateUrgentTime.java:409)

    // line 409 contain this code
    String buftimezone = iSetting.getTimeZone();
    Looking at the info above, the most likely cause for the NullPointerException is that iSetting hasn't been initialized before line 409 is executed.

  3. #3
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,104

    Re: java.lang.NullPointerException, how to handle ?

    aktarjahan2003, does this help?

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,567

    Re: java.lang.NullPointerException, how to handle ?

    Traud, here I would rather assume that the IDE successfully resolved the line numbers. The question is about running in an emulator+IDE.

    Aktarjahan2003: initializers run from top to bottom. If iSettings has an inline initializer (the "protected blabla iSetting=new Setting();" thing), and it is declared later than the referred one (line 139 of calculateUrgentTime.java), it is not usable that time. Move it somewhere before line 139.
    Last edited by wizard_hu_; 2013-07-18 at 11:34.

  5. #5
    Regular Contributor
    Join Date
    Oct 2008
    Posts
    80

    Re: java.lang.NullPointerException, how to handle ?

    Thanks all, I could not make it working and start from back up copy but know more precisely where is problem. May be I have basic problem in Java String
    public class settingClassInterface {
    ...
    private String iTimeZone;
    ...


    public settingClassInterface()
    {
    ...
    iTimeZone = new String("default");
    ...

    }

    public String getTimeZone()
    {

    // Without these 3 lines it does not work , what I am doing wrong?
    if(iTimeZone == null || iTimeZone == "")
    {
    return new String ("default"); // This way work but I expect to return iTimeZone
    }
    return iTimeZone;
    }
    public void setTimeZone(String aTimeZone)
    {

    iTimeZone = aTimeZone;
    }
    }

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2003
    Posts
    4,104

    Re: java.lang.NullPointerException, how to handle ?

    In Java, you compare string-content with String.equals(…). You are checking for the identity of two object references, and Java might not take care whether they are Strings. If you want to check for an empty String, you use String.length() == 0.

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Guatemala
    Posts
    186

    Re: java.lang.NullPointerException, how to handle ?

    Yes, as traud said, you should use the equals method to compare strings, but in this case, it might not be causing the problem.

    As far as I can see, you are initializing your iTimeZone member variable with the word "default", which happens to be the same word you are returning when the string is apparently null, and I don't see a call to the setTimeZone method, so maybe it is working as you expect, but you think it is not.

    Change the texts and try again. If that doesn't work, post some more code. so we don't have to assume .

    Thanks,
    -CarlosDL

  8. #8
    Regular Contributor
    Join Date
    Dec 2012
    Location
    Indonesia,DKI Jakarta
    Posts
    87

    Re: java.lang.NullPointerException, how to handle ?

    Hi aktarjahan2003,

    Please post more your code. Based on your code you have to check on line 409. It produce Null System.out.println before line 409. It will show you if the value null or not.

Similar Threads

  1. java lang nullpointerexception
    By j2meSak in forum Mobile Java Media (Graphics & Sounds)
    Replies: 7
    Last Post: 2009-11-19, 05:46
  2. java.lang.NullPointerException
    By littleson in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2009-02-06, 16:13
  3. Getting java.lang.NullPointerException
    By nagesh.chetana in forum Mobile Java General
    Replies: 3
    Last Post: 2008-10-20, 10:41
  4. java.lang.nullpointerexception
    By maneeshv6 in forum Mobile Java General
    Replies: 2
    Last Post: 2008-03-25, 09:36
  5. java.lang.nullpointerexception
    By jdarelle in forum Mobile Java General
    Replies: 3
    Last Post: 2004-08-22, 08:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •