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

Time:06-23

@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?

CodePudding user response:

Try to use ! operator like this...

if(!locationDetails1) ....

CodePudding user response:

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

You should be setting locationDetails2.

CodePudding user response:

You are trying to save a null object - locationDetails1 - to the database in the end.

locationDetailsService.save(locationDetails1);
  • Related