in

Java – code will always throw a Null Pointer Exception when no Property is present?


I have inherited the following java code, that gets the value of a property from a properties file:

    String personName = this.properties.getFilePropertty("person.name");
    if (personName != null) {
       // do something else
    } else {
        // do something
    }

The intended behavior in the above flow is that personName will either be retrieved from the properties file or will be returned as null if its not there, and handled accordingly.

However when the property is not there an exception is thrown in the getFileProperty() method (shown below).

How can I fix this to have the intended behavior?

getFileProperty():

            public String getFileProperty(String name) throws SystemPropertiesException {
            
            return Optional.ofNullable( this.properties.getProperty(name, null) )
            .orElseThrow(()->new PropertiesException("Can not get property!"));
            
             }

Note – the getProperty() method being called in the code above is java utils getProperty method.



Source: https://stackoverflow.com/questions/70541305/java-code-will-always-throw-a-null-pointer-exception-when-no-property-is-prese

Realme 9 Pro Supposedly Spotted at Multiple Certification Sites Including BIS

EF Core 5 Owned type does not return saved data from database