DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023
  • IntelliJ and Java Spring Microservices: Productivity Tips With GitHub Copilot
  • Using Unblocked to Fix a Service That Nobody Owns
  • How To Approach Dependency Management in Java [Video]

Trending

  • Performance Optimization Techniques for Snowflake on AWS
  • Fixing Common Oracle Database Problems
  • Teradata Performance and Skew Prevention Tips
  • Scaling Mobile App Performance: How We Cut Screen Load Time From 8s to 2s
  1. DZone
  2. Coding
  3. Tools
  4. folder.listFiles() May Produce NullPointerException [Snippet]

folder.listFiles() May Produce NullPointerException [Snippet]

If you're calling folder.listFiles() more than once in your code, you might be getting the dreaded NullPointerException. Be sure you only run it once.

By 
Steven Schwenke user avatar
Steven Schwenke
·
Mar. 09, 17 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
18.0K Views

Join the DZone community and get the full member experience.

Join For Free

Yesterday, I noticed a strange warning during a code review. IntelliJ IDEA warned me about this code:

File folder = new File("path");
if(folder.listFiles() != null) {
    for(File file : folder.listFiles()) {
        System.out.println(file.getName());
    }
}


The warning said "Dereference of 'folder.listFiles()' may produce 'java.lang.NullPointerException'". I wondered why because there is an explicit NPE-check in line 2. Then I realized that this check is pointless. listFiles() will search the given directory for files at every call. So by having two calls in the code above, the directory could be empty when the second call executes, even when it was not at the time the first call hits.

Here's a better solution:

File[] files = folder.listFiles();
if(files != null) {
    for(File file : files) {
        System.out.println(file.getName());
    }
}


Now, the method listFiles() is only called once, so the folder cannot be emptied between two calls.

Yeah, this is an easy one — but maybe you wondered about this warning as I did.

intellij

Published at DZone with permission of Steven Schwenke. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023
  • IntelliJ and Java Spring Microservices: Productivity Tips With GitHub Copilot
  • Using Unblocked to Fix a Service That Nobody Owns
  • How To Approach Dependency Management in Java [Video]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: