简体   繁体   中英

My app stops when a variable has null value. why?

@PostMapping("/order")
public Bill addOrder(@RequestBody Bill theBill) {
    
    theBill.setDate(timestamp = new Timestamp(System.currentTimeMillis()));
        
    billService.save(theBill);
    
    List<BillDetails> billDetails1 = theBill.getBillDetails();
    
    for(BillDetails billDetails2:billDetails1)
    {
        Product product = billDetails2.getProduct();
        Location location = billDetails2.getLocation();
        Quality quality = billDetails2.getQuality();
        
        double quantity1 = billDetails2.getQuantity();
        
        LocationDetails locationDetails1 = locationDetailsService.findById(new LocationDetailsId(product,quality,location));
        
        if(locationDetails1 == null){
            
            LocationDetails locationDetails2 = new LocationDetails();
            
            LocationDetailsId locationDetailsId = new LocationDetailsId();
            
            locationDetailsId.setProduct(product);
            locationDetailsId.setLocation(location);
            locationDetailsId.setQuality(quality);
            
            locationDetails2.setQuantity(quantity1);
            
            locationDetails2.setLocationDetailsId(locationDetailsId);
            
            locationDetailsService.save(locationDetails2);
            
        }
        else {
        
            double quantity2 = locationDetails1.getQuantity();
            
            double quantity3 = quantity2 + quantity1;
            
            LocationDetails locationDetails3 = new LocationDetails();
            
            locationDetails3.setLocationDetailsId(locationDetails1.getLocationDetailsId());
            locationDetails3.setQuantity(quantity3);
            
            locationDetailsService.save(locationDetails3);
        }
        
    }
    
    return theBill;
}

Hi I'm new to springboot, here my app runs fine when I have values for locationDetails1 . but it stops running when locationDetails1 returns null value . The Error I'm getting is (did not found). I want to handle that in if clause which I did .But still I receive the error. how to get rid of this?

Try to use ! operator like this...

if(!locationDetails1) ....

Because you are setting locationDetails1 in locationDetailsService. locationDetailsService.save(locationDetails1);

You should be setting locationDetails2.

您最终尝试将空对象 - locationDetails1 - 保存到数据库中。

locationDetailsService.save(locationDetails1);

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