java.lang.NullPointerException: Cannot get property 'length' on null object

Hello, when I try to execute this customer keyword:
@Keyword

public boolean isFileDownloaded(String downloadPath, String fileName) {

File dir = new File(downloadPath);

File[] dirContents = dir.listFiles();

String lastAttempt = "";

if (dirContents.length > 0) {

	for (int i = 0; i < dirContents.length; i++) {

		if (dirContents[i].getName().equals(fileName)) {

			// File has been found, it can now be deleted:

			dirContents[i].delete();

			KeywordUtil.markPassed(fileName + ' exist in ' + downloadPath + ' and was deleted')

			return true;

		}

		lastAttempt = dirContents[i].getName().equals(fileName);
	}

	if (lastAttempt != fileName) {

		KeywordUtil.markFailed(fileName + 'does not exist in' + downloadPath)

		return false;
	}
}

return false;

}
And when I execute I go the title error: java.lang.NullPointerException: Cannot get property ‘length’ on null object and I don’t know how to handle this. Thank you in advanced!

hi,

your filelist is empty that’s why you getting npe for length
can you debug it and see are there any files in dirContents list?

I cannot debug because I need Katalon Enterprise.

hi,

anyway the list is empty, that’s npe

Can you explain me, I am newbi. Thank you in advanced!

hi,

File[] dirContents = dir.listFiles();

dir.listFiles will return empty list

1 Like

Ty, I found another explanation written by you in another topic. Thank you very much!

Hey, what was the solution? I am also getting the same issue.

The issue was that the OP was trying to get the length of a property, but the property did not have any content. Can we see what you are trying to do and then we may be able to assist you?