Sonar在第17行(Date parsedStartDate = df.parse(startDate);)上将以下错误标记为该帖子映射:

//在Magicservice.controller.MagicStoreController.getTradeMagicsByDateRange(HttpServletRequest,String,String,String)中为java.text.DateFormat.parse(String)的非null参数传递的null //此方法调用为非空方法参数。该参数被注释为应始终为非空的参数,或者分析表明它将始终被取消引用。

该代码已经在阻止传递空值,不确定在这里是否缺少某些内容

    @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;
    }

最佳答案

您只能处理startDateendDate均为空的情况
相反,您应该处理其中任何一个为null的情况:

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

07-28 01:51
查看更多