Jillbarvi
Jillbarvi

Reputation: 15

IntelliJ IDEA - Get Cannot resolve method 'isBlank' in 'String' but works fine in STS

I recently moved my Java project from Spring Tools IDE to IntelliJ, but I'm getting an error that I'm not having in STS : "Cannot resolve method 'isBlank' in 'String' " Which is more, in Spring Tools the test is running successfully. I tried rebuild the pom file but still having the error.

The portion of code I'm using is this:

if (uploadedPDFName == null || uploadedPDFName.isBlank()) {
    //do something
}

I'm capturing file name in a string variable.

Upvotes: 1

Views: 4917

Answers (4)

Mehdi Mohamadi
Mehdi Mohamadi

Reputation: 21

Go to pom.xml Then change Java version to above or equal to 16 and then Maven -> Update

Upvotes: 0

dajavax
dajavax

Reputation: 3400

String::isBlank was introduced in Java 11. You probably have IntelliJ configured to use some older Java version (most likely Java 8). I recommend using some other code alternative as Java 11 is not yet widely supported.

For example:

if (uploadedPDFName == null || uploadedPDFName.trim().length() == 0)

or

if (uploadedPDFName == null || uploadedPDFName.chars().allMatch(Character::isWhitespace))

In case you are only concerned of checking if it is empty use String::isEmpty. However, isBlank returns true if the String has whitespaces, isEmpty does not.

Upvotes: 2

bassouat
bassouat

Reputation: 326

I think the problem is about your JDK version,if you have an old version like JDK 8 or JDK 7 ...you will have an error when using isBlank() because String::isBlank() was introduced in java 11. You Can check by typing :java --version in a terminal if you have an old version,you Can just use isEmpty().

Upvotes: 3

Cory Dorfner
Cory Dorfner

Reputation: 81

First, I would check that the IntelliJ is set up to be using at least Java 11, as that is when isBlank() was introduced. You could also try isEmpty() as well but the JDK version is my guess as to what's causing the issue.

Upvotes: 1

Related Questions