简体   繁体   中英

How do I check object null checking inside setter method in java?

this is the usual way

productMenu = productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId")));
            if (productMenu != null) {
                productPost.setProductMenu(productMenu);
            }

can i do this not null object checking inside setter method

productPost.setProductMenu(productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId"))));

Trying to reduce the code. Would appreciate any sort of help.

Version >= Java 8 You could use Optional to ensure only non null value will be set.

 Optional.ofNullable(request.getString("productMenuId"))
 .map(value-> Integer.parseInt(value))
 .map(productMenuService::getProductMenuById);
 .ifPresent(productPost::setProductName);

Version < Java 8

void setProductMenu(*ProductMenuType* productMenu) {
  // do all your checking before setting here
  if (productMenu != null) {
     productPost.setProductMenu(productMenu);
  }
}

But your case doesn't make sense(no else part) because if we are not setting a value for a variable, then by default it will be NULL

if(productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId")) == null){
   return new ResponseEntity<String>("Not found", HttpStatus.BAD_REQUEST);
}else{
productMenu = productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId")));

}
//in your repository first check that data is present or not like this
repository.findById(id).isPresent(){
return repository.getOne(id);
}else return null

Throw an exception.

If someone passes an invalid product menu ID to your application, they made a mistake. You should not act like they did nothing wrong. Your application certainly should not leave the old product menu in place and ignore the user's request entirely.

if (productMenu == null) {
    throw new RuntimeException("Invalid product menu ID.");
}

productPost.setProductMenu(productMenu);

While this won't make your code shorter, it will make your code better.

A robust application does not pretend that it's working when it is not in fact working. To do otherwise would frustrate users and would make debugging a lot more difficult, as it could be weeks or months or years before the problem is discovered.

Ideally, you will want to create your own exception class, and throw that instead:

if (productMenu == null) {
    throw new InvalidProductMenuIDException("Invalid product menu ID.");
}

productPost.setProductMenu(productMenu);

where the exception class is simply:

public class InvalidProductMenuIDException
extends Exception {
    public InvalidProductMenuIDException(String message) {
        super(message);
    }

    public InvalidProductMenuIDException(Throwable cause) {
        super(cause);
    }

    public InvalidProductMenuIDException(String message,
                                         Throwable cause) {
        super(message, cause);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM