4

Using without if else statement how to avoid nullpointerexception in java

 2 years ago
source link: https://www.codeproject.com/Questions/5318849/Using-without-if-else-statement-how-to-avoid-nullp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This is my method, which I am using Optional to avoid NullPointerException, but I want to do that using without if else statement to avoid nullpointer exception. is it possible or is there any way, that I say using without if else statement control any structure?

Expand ▼   Copy Code
@Override
    public void save2(Optional<CheckResponse> response) {
        if (response.isPresent()) {
            CheckResponse checkResponse = response.get();
            if (checkResponse.getValue().isPresent()) {
                Optional<List<CheckDetails>> value = checkResponse.getValue();
                if (value.isPresent()) {
                    List<CheckDetails> checkDetails = value.get();
                    Stream<Checks> checksStream = checkDetails.stream().map(this::mapDtoCheckDetailsToEntiyChecks);
                    Optional<Checks> first = checksStream.findFirst();
                    if (first.isPresent()) {
                        Checks checks = first.get();
                        this.checkRepository.save(checks);
                    }
                }
            }
            Checked checked = new Checked();
            checked.setResponse(checkResponse.getResponse());
            checked.setServiceResponse(checkResponse.getServiceResponse());
            checked.setErrorMessage(checkResponse.getErrorMessage());
            if (checkResponse.getBrokenRules().isPresent()) {
                List<BrokenRulesResponse> brokenRulesResponses = checkResponse.getBrokenRules().get();
                brokenRulesResponses.forEach(brokenRulesResponse -> {
                    checked.setMessage(brokenRulesResponse.getMessage());
                    checked.setMember(brokenRulesResponse.getMember());
                    this.checkedRepository.save(checked);
                });
            }
        }
    }


And the piece of code I show here is the event described in an article. And here it is said finally, we have avoided all the potential NullPointerExceptions and the code is now safe. However, that made the method ugly. Moreover, think about the case where there are more nested objects like this. However, as far as I understand, I have done the same operation by using unlimited if control conditions. There is only one thing I want here. Can I shorten the code by using Optional without using too many if control conditions?
Copy Code
if (users != null) {
    
    User user = users.get(0);
    if (user != null) {
        
        Address address = user.getAddress();
        if (address != null) {
        
            String state = address.getState();
            if (state != null) {
                System.out.println("State is " + state);
            }
        }
    }
}


What I have tried:

I still finding any solution but I couldn't find.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK