Null Pointer Exception String null

if( currentTimeslot.isEmpty()){
         System.out.println("Do stuff");
            }

      

Why did I receive NullPointerException

? How do I check if a string is NULL

and do something if it is? Whenever currentTimeslot

is equal NULL

, I get an error. Here is the console message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PM.ui.MainFrame.getJPanelTopMenu(MainFrame.java:382)

      

+3


source to share


6 answers


Try to run



    if( currentTimeslot == null){
             System.out.println("Do stuff");
   }

      

+4


source


You may be new to Java, in Java an object can be null, which means no methods are available for that object, so a NullPointerException will be thrown every time you try to access a method of the null object.

To solve this problem, you have to check that the object is not null by



        if ( currentTimeslot != null ){
            ....
        }

      

Since the entire Java object extends from java.lang.Object , this check is relevant for any type, not just String.

+1


source


It's important to ask yourself: null

and empty are logically equivalent for what you are doing?

If so, you can use this:

if (currentTimeslot == null || currentTimeslot.isEmpty()) {
    // Do stuff
}

      

If the first half of this statement evaluates to true

, Java won't bother with the second half, thus protecting you from a null pointer exception.

Another approach would be to normalize your data; if you want the null

empty string to be treated as the same thing, just do something like this at the beginning of the code:

if (currentTimeslot == null) currentTimeslot = "";

      

Now you don't have to defend the null check every time you use it.

As for why you are getting an exception: in Java, all objects (any variable that is not a primitive type int

, boolean

etc.) are constituted null

until you initialize it. If you try to access any methods or fields of this object, you will get a null pointer exception because you are asking the code to access something that doesn't actually exist yet. In Java, you either want to make sure that your objects are initialized by something earlier, or do a lot of protective null checks (either with blocks if (variable != null) { ... }

or try { ... } catch (NullPointerException npe) { ... }

) to exactly prevent the problem you are dealing with.

* - Initialize it with something other than of null

course.

+1


source


if(currentTimeslot==null){
    System.out.println("whatever");
}

      

The command is the currentTimeslot.isEmpty()

same as currentTimeslot.equals("")

, which is NOT considered empty, it is just empty. If you want to check if it is null or not, you have to put one case in another, for example:

if(currentTimeslot==null){
    System.out.println("null string");
}
else{
    if(currentTimeslot.isEmpty()){
       System.out.println("empty string");}
     }

      

If the commands you want to put are many, to copy them twice, you can put them in a function and call the function, or alternatively use a boolean variable that becomes true in both cases, and then check if the boolean variable is true for executing other commands, for example:

 boolean empty;
if(currentTimeslot==null){
    System.out.println("null string");
    empty=true;
}
else{
    if(currentTimeslot.isEmpty()){
       empty=true;
       System.out.println("empty string");}
     }
 if(empty){
 ....
 .... }

      

Hope it helps :)

+1


source


If you get a null pointer exception, it might be because currentTimeslot is null. Therefore, you must first check if it is not null and then call one of its methods:

if(currentTimeslot!=null && currentTimeslot.isEmpty())...

      

0


source


I suggest you let go of most of the string blank and null tests. Apache Commons Lang StringUtils decided that most string manipulations (empty, empty, not empty, everything is empty, etc.) a long time ago.

Download apache commons lang and use it.

Your code will become something like this:

if (StringUtils.isEmpty(currentTimeslot))
{
   ... react to an empty currentTimeslot .
}
else // currentTimeslot  is not emty (that includes not null).
{
   .. react to a not empt currentTimeslot 
}

      

0


source







All Articles