PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How to represent an empty / invalid java.nio.Path

 October 28, 2022     is-empty, java, nio, nullpointerexception     No comments   

Issue

How can one create a java.nio.Path that represents no path no matter the environment?

I cannot call Path.of(<String>) and guarantee the resulting path to point to an invalid file or directory.

What I'm looking for is if there is a safer way to represent an empty / invalid path than using a null value.


Solution

Depending on your use case, there are a couple options:

  • If you are trying to return the path from a method, you can use Optional<Path> and return Optional.empty() to denote no path.
  • If this is a field in a class and that class implements some behavior, you can use the null object pattern to implement another class with the same interface that does "nothing" (whatever your application needs to do if there is no path).


Answered By - casablanca
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing