No, absolutely not - because if acct is null, it won't even get to isEmpty... it will immediately throw a NullPointerException.

Your test should be:

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

Note the use of && here, rather than your || in the previous code; also note how in your previous code, your conditions were wrong anyway - even with && you would only have entered the if body if acct was an empty string.

Alternatively, using Guava:

if (!Strings.isNullOrEmpty(acct))
Answer from Jon Skeet on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @ecetasci.iu โ€บ checking-for-null-or-empty-strings-in-java-19518fa1e553
Checking for Null or Empty Strings in Java | by Ece Tasci | Medium
February 25, 2025 - Handling Strings properly in Java ... check like if(name != null && !name.isEmpty()) helps prevent unexpected crashes and ensures that only valid data is processed....
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ checking for empty or blank strings in java
Checking for Empty or Blank Strings in Java | Baeldung
January 8, 2024 - If weโ€™re at least on Java 6, then the simplest way to check for an empty string is String#isEmpty: boolean isEmptyString(String string) { return string.isEmpty(); } To make it also null-safe, we need to add an extra check:
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ check-if-a-string-is-empty-or-null
Java Program to Check if a String is Empty or Null | Vultr Docs
December 17, 2024 - java Copy ยท public static boolean ... code checks if the variable str is null or empty (""). If either condition is true, it returns true; otherwise, it returns false....
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - One of the key differences between StingUtils and String methods is that all methods from the StringUtils class are null-safe. It additionally provides a few methods that we can leverage for this, including StringUtils.isEmpty() and StringUtils.isBlank():
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 09 โ€บ right-way-to-check-if-string-is-empty.html
Right way to check if String is empty in Java with Example | Java67
One of the most popular ways of ... call this method without checking whether String is null or not. In another word, this is not null safe and it will throw NullPointerException if String is null....
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ isempty() in java
Java String isEmpty - Scaler Topics
January 4, 2024 - The isEmpty() method cannot be used for a string that is containing a null value.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ why does the order of string == null || string.isempty() matter? or does it?
r/learnjava on Reddit: Why does the order of String == null || String.isEmpty() matter? Or does it?
May 3, 2019 -

I just completed MOOC, Java part II, exercise 19 for week 9 and found that the order I placed name.isEmpty() or name == null was the key to get it's tests to pass.

The test which failed was one that tested a null but I don't understand why it matters since it's an or statement.

Thank You

So this fails

    public Person(String name, int age) {
        if ( name.isEmpty() || name == null ||  name.length() > 40) {
            throw new IllegalArgumentException("Name is an invalid length.");
        } else {
            this.name = name;
        }

        if ((age < 0) || (age > 120)) {
            throw new IllegalArgumentException("Age is not a valid range");
        } else {
            this.age = age;
        }
    }

But this passes

    public Person(String name, int age) {
        if (  name == null || name.isEmpty() || name.length() > 40) {
            throw new IllegalArgumentException("Name is an invalid length.");
        } else {
            this.name = name;
        }

        if ((age < 0) || (age > 120)) {
            throw new IllegalArgumentException("Age is not a valid range");
        } else {
            this.age = age;
        }
    }
๐ŸŒ
Medium
medium.com โ€บ @lp.lok.payu โ€บ isempty-vs-empty-vs-isblank-vs-isnull-12aea580fe4b
isEmpty() vs empty() vs isBlank() vs isNull() | by leela prasad | Medium
February 16, 2025 - Returns true if the string is null or empty. Both empty() and isEmpty() work similarly but have some differences in their synchronization behavior. The empty() method is synchronized, while isEmpty() is not.
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ guava-discuss โ€บ c โ€บ Ar7HSlvlS4A
null-safe isEmpty for Iterables
To further support the argument of NEVER using null as meaning "empty": It is virtually cost-less to return an immutable empty collection using the java.util.Collections.emptyXxx() methods, which returns empty immutable singletons and even does a good job of type inference.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ commons_collections โ€บ safe checks in apache commons collections
Safe Checks in Apache Commons Collections
August 14, 2018 - True if empty or null. The following example shows the usage of org.apache.commons.collections4.CollectionUtils.isEmpty() method. We'll check a list is empty or not. package com.tutorialspoint; import java.util.List; import org.apache.commo...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ stringutils-isempty
StringUtils.isEmpty() in Java: Your Method Guide
March 27, 2024 - StringUtils.isEmpty, on the other hand, is null-safe and can handle both empty and null strings gracefully. Therefore, itโ€™s generally recommended to use StringUtils.isEmpty when checking if a string is empty or null in Java.
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ can-we-rely-on-string-isempty-for-checking-null-condition-on-a-string-in-java
Can We Rely on String.isEmpty() for Checking Null Conditions on Strings in Java? | Java Programming Guide โ€” javathinking.com
String str = ...; // Could be null, empty, or non-empty // Safe: Check null first, then empty if (str == null || str.isEmpty()) { System.out.println("String is null or empty."); } Why this works: The || operator short-circuits: if str == null ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ java/lang โ€บ java string isempty method
Java String isEmpty Method
September 1, 2008 - Because Null is a placeholder that generally means, "no data is available about this", and it is not the same as an empty string, the compiler cannot handle it and throws a NullPointerException.