Java - 非空参数的声纳空传递

Sonar 在第 17 行标记此后映射,并显示以下错误(Date parsedStartDate = df.parse(startDate);):

//Magicservice.controller.MagicStoreController.getTradeMagicsByDateRange(HttpServletRequest, String, String, String) 中java.text.DateFormat.parse(String)的非空参数传递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.


紫衣仙女
浏览 89回答 1
1回答

慕哥9229398

startDate您只处理当和endDate均为空时的情况相反,您应该处理其中任何一个为空时的情况:if&nbsp;(startDate&nbsp;==&nbsp;null&nbsp;||&nbsp;endDate==null)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;ESException(MSG_ERROR_NULL_INPUTS); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java