In this post, I am going to show you what are the ways to check null in a String or check blank in a String in java.

When a String is null or empty creates a data lost problem and even if a string is null it created an abnormal flow of you're Java program like NullPointerException.

How we can handle this NullPointerException in java. So simply we need to check if our String is null or not, If Sting is null then don't try to execute the program further.

checking empty string in java
String empty java

Handling null in the string can be fixed a NullPointerException in your code. So how to check null in a String in java, their many approaches that you can use to handle null in String.


It depends on you which code is good for you, mostly all code can help you to check null in string in java.

1) Check String is null or empty in Java code using the String length method.

If you only want to check a String is null or blank then used this code. Here we used a simple length method to count the length of a String.
public class Check_String_Null_Blank {

	public static void main(String[] args) {

		String test1 = null;
		String test2 = "";
		
		if (test1==null && test2.length()==0) {
			System.out.println("TEST  Completed");
		}else {
			System.out.println("TEST  InCompleted");
		}
	}

}
OUTPUT: TEST Completed

This code will check a String contains a null or blank in java only.

What happens if we add one more variable of String test3=" "; which contains some space in String then it will return What, How we can handle this string space contains see below code first.

2)This function is not going to check if a string which contains a space.


 public class Check_String_Null_Blank {

	public static void main(String[] args) {

		String test1 = null;
		String test2 = "";
		String test3 = "  ";
		
		if (test1==null && test2.length()==0 && test3.length()==0) {
			System.out.println("TEST  Completed");
		}else {
			System.out.println("TEST  InCompleted");
		}
	}

}
OUTPUT: TEST InCompleted

If a String contains some space it will say the string contains some data. How to handle this type of case, We need to check if the string is empty or null or contains space see below example

3) In CoreJava Programming we can use the trim method to handle the above example.

Only we need to add a trim method to handle a String contains space in java.

public class Check_String_Null_Blank {

	public static void main(String[] args) {

		String test1 = null;
		String test2 = "  ";
		
		if (test1==null && test2.trim().length()==0) {
			System.out.println("TEST  Completed");
		}else {
			System.out.println("TEST  InCompleted");
		}
	}

}


OUTPUT: TEST Completed

4) Using an equals method to check string is null or blank or contains space.

public class Check_String_Null_Blank {

	public static void main(String[] args) {

		String test1 = null;
		String test2 = "    ";
		
		if (test1==null && "".equals(test2.trim())) {
			System.out.println("TEST  Completed");
		}else {
			System.out.println("TEST  InCompleted");
		}
	}

}

OUTPUT: TEST Completed

5)String null or blank in java using isEmpty() method

 public class Check_String_Null_Blank {

	public static void main(String[] args) {

		String test1 = null;
		String test2 = "    ";
		
		if (test1==null && test2.trim().isEmpty()) {
			System.out.println("TEST  Completed");
		}else {
			System.out.println("TEST  InCompleted");
		}
	}

}

OUTPUT: TEST Completed

6) Using Apache Commons StringUtils to check a string is null or empty or contains space.

For using this you need to download apache-commons jar.
public class Check_String_Null_Blank {

	public static void main(String[] args) {

		String test1 = null;
		
		if (StringUtils.isBlank(test1)) {
			System.out.println("TEST  Completed");
		}else {
			System.out.println("TEST  InCompleted");
		}
	}

}
If your test1 contain
  1. StringUtils.isBlank(null) = true
  2. StringUtils.isBlank("") = true
  3. StringUtils.isBlank(" ") = true
  4. StringUtils.isBlank("string null") = false

This way to handle a string null in java. If you want to know how many ways we can convert int into String in core java

Hope This Help You, Comment Below.

Post a Comment

Previous Post Next Post