Results 1 to 5 of 5

Thread: How to check java string not null or blank

  1. #1
    Join Date
    Apr 2008
    Posts
    38

    How to check java string not null or blank

    Hi,

    I need help with java string to check the string if its not null or blank. I want to check from may java code that the string is not null or blank.
    I hope its a simple question & someone will definitely come up with the correct solution.
    Thanks in advance.

  2. #2
    Join Date
    May 2008
    Posts
    23

    Re: How to check java string not null or blank

    Null
    Null is the reserved constant used in Java to represent a void reference i.e a pointer to nothing. Internally it is just a binary 0, but in the high level Java language, it is a magic constant, quite distinct from zero, that internally could have any representation.

    If you execute some code like this:

    Code:
    x.doSomething();
    and x is null, you will raise a java.lang.NullPointerException. Note Null PointerException, not Null ReferenceException.

    The difference between a null reference and a reference to an empty object, (e.g. a freshly minted default object, empty collection or empty Iterator) is a major headache in Java. When dealing with other people’s code I want to strangle them. Why won’t they tell me when a method might return null or empty? Why won’t they tell me if a method can’t accept a null or empty object?

    Code:
    if ( x != null )
       {
       x.doSomething();
       }

  3. #3
    Join Date
    May 2008
    Posts
    35

    Re: How to check java string not null or blank

    Checking a sting object containing empty or null value

    Apache has provided two methods isBlank() and isNotBlank() for checking the strings.

    The class org.apache.commons.lang.StringUtils extends Object class and provides methods that operate on null safe string.

    The methods used:
    isBlank(String str): This method is used to check whether a string is empty ("") , null or whitespace.

    isNotBlank(String str): This method is used to check whether a string is not empty (""), not null or not a whitespace .

    There are two more methods which can be used:

    isNotEmpty(String str): Use this method for checking a string whether it is not empty("") or not null.

    isEmpty(String str): Use this method to check a string for its empty (" ) or null value.

    Code:
     import org.apache.commons.lang.StringUtils;
     
    public class CheckEmptyStringExample 
    {    
        public static void main(String[] args)
        {
        String string1 = "";
        String string2 = "\t\r\n";
        String string3 = "     ";
        String string4 = null;
        String string5 = "Hi"; 
        System.out.println("\nString one is empty? " + 
    StringUtils.isBlank(string1));
        System.out.println("String one is not empty? " +
     StringUtils.isNotBlank(string1));
        System.out.println("\nString two is empty? " + 
    StringUtils.isBlank(string2));
        System.out.println("String two is not empty?" + 
    StringUtils.isNotBlank(string2));
        System.out.println("\nString three is empty?" + 
    StringUtils.isBlank(string3));
        System.out.println("String three is not empty?" +
    StringUtils.isNotBlank(string3));
        System.out.println("\nString four is empty?" + 
    StringUtils.isBlank(string4));
        System.out.println("String four is not empty?" + 
    StringUtils.isNotBlank(string4));
        System.out.println("\nString five is empty?" + 
    StringUtils.isBlank(string5));
        System.out.println("String five is not empty?" + 
    StringUtils.isNotBlank(string5)); 
        }
    }

  4. #4
    greenblue Guest

    Re: How to check java string not null or blank

    I don't know what you want to do. If you're making report. Maybe you can try RAQ Report. It's a free java reporting tool. In RAQ Report, you can check java string not null or blank.

  5. #5
    Join Date
    Jul 2009
    Posts
    11

    Re: How to check java string not null or blank

    Every application needs to have a util class

    Just set up

    public static bool IsNullOrEmpty(string stringVar)
    bool b;
    if(stringVar == null || stringVar.Length == 0)
    {return b;}

    and be done with it and stop whining about whidbey?

    call it from anywhere:

    if (!Util.IsNullOrEmpty(myString))
    {myString="a good one"}

    Hope it works!!!!
    SweetIM

Similar Threads

  1. Replies: 2
    Last Post: 15-02-2012, 05:52 PM
  2. Problem with a null string in java
    By Rily in forum Software Development
    Replies: 4
    Last Post: 12-08-2010, 10:23 AM
  3. How to Check Null Value in C#?
    By sivaranjan in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 04:49 AM
  4. In Java Script how to Check Null Value?
    By taher in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 03:18 AM
  5. SQL need to check null or empty string
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 18-06-2009, 11:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,565,138.51671 seconds with 17 queries