andresmonc
andresmonc

Reputation: 478

Java - Sonar Null pass for non-null parameter

Sonar is flagging this post mapping with the following error on line 17 (Date parsedStartDate = df.parse(startDate);):

//Null passed for non-null parameter of java.text.DateFormat.parse(String) in Magicservice.controller.MagicStoreController.getTradeMagicsByDateRange(HttpServletRequest, String, String, String) //This method call passes a null value for a non-null method parameter. Either the parameter is annotated as a parameter that should always be non-null, or analysis has shown that it will always be dereferenced.

The code is already preventing a null value from being passed not sure if there's something I'm missing here

    @PostMapping(value = "/Magics/MagiclistbyDateRange/{MagicStatus}")
    public @ResponseBody List<MagicLog> getTradeMagicsByDateRange(HttpServletRequest request,
            @PathVariable String MagicStatus, @RequestParam(value = "STARTDATE", required =false ) String startDate,@RequestParam(value = "ENDDATE", required =false) String endDate)
            throws ESException {
        logger.info("MagicLog received from client -  MagicStatus is :: " + MagicStatus);
        String inAppAuthorization = request.getHeader("InAppAuthorization");
        validateRequest(request, inAppAuthorization);
        List<MagicLog> MagicLogs = new ArrayList<>();
        DateFormat df = new SimpleDateFormat("yyyyMMddHH:mm:ss");
        df.setLenient(false);
        if (startDate == null && endDate==null) {
            throw new ESException(MSG_ERROR_NULL_INPUTS);
        }
        else{
        try {
            // THIS is the line causing issues (17)
            Date parsedStartDate = df.parse(startDate);
            //Null passed for non-null parameter of java.text.DateFormat.parse(String) in Magicservice.controller.MagicStoreController.getTradeMagicsByDateRange(HttpServletRequest, String, String, String)
            //This method call passes a null value for a non-null method parameter. Either the parameter is annotated as a parameter that should always be non-null, or analysis has shown that it will always be dereferenced.
            Date parsedEndDate = df.parse(endDate);     
            long diffInMillies = Math.abs(parsedEndDate.getTime() - parsedStartDate.getTime());
            long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
            if(diff >3 )
            {
                throw new ESException(MSG_ERROR_INVALID_START_AND_END_DATE);                
            }
            else
            {
            MagicLogs = ESService.getMagicLogByDateRange(MagicStatus, parsedStartDate,parsedEndDate);
            }
        } catch (ParseException e) {
            logger.error(MSG_ERROR_PARSING_STRING_TO_DATE + e.getMessage(), e);
            throw new ESException(e.getMessage());
        } catch (Exception e) {
            logger.error(MSG_ERROR_FROM_SERVICE + e.getMessage(), e);
            throw new ESException(e.getMessage());
        }
        }
        logger.info("Number of records returned for source system " + MagicStatus + "from" +startDate+"to"+endDate+" "+ MagicLogs.size());
        return MagicLogs;
    }

Upvotes: 1

Views: 3516

Answers (1)

mangusta
mangusta

Reputation: 3554

You only handle cases when startDate and endDate are BOTH nulls
Instead you should handle the case when any of them is null:

if (startDate == null || endDate==null) {
            throw new ESException(MSG_ERROR_NULL_INPUTS);
        }

Upvotes: 1

Related Questions