steps to solve java.nullpointerexception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hon Man
    New Member
    • Jan 2011
    • 1

    #1

    steps to solve java.nullpointerexception

    how can isolve this problem in my balckberry
    steps to solve java.nullpointe rexception
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    You are trying to use some variable which is not having any value.

    Debug and figure out which is that value.

    Regards
    Dheeraj Joshi

    Comment

    • nathj
      Recognized Expert Contributor
      • May 2007
      • 937

      #3
      when you know the variable you can put a simple if check around it or use the try catch block catching a NullPointerExce ption.

      So, for example if the variable in question is a String called var1
      Code:
      if(var1 != null && var1.length() > 0)
      {
      // use the variable
      }
      else
      {
      // handle the exception
      }
      Alternatively using the try catch method:
      Code:
      try{
       // use the variable var1
      }catch(NullPointerException npe){
      System.out.println(npe.getMessage()); // not production ready
      }
      This is the way I would go, the finl suggestion is that if it is appropriate set up the variable with a default value.

      Hope this helps
      nathj

      Comment

      Working...