为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

紫衣仙女 2023-08-04 19:09:21
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.
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

startDate您只处理当和endDate均为空时的情况
相反,您应该处理其中任何一个为空时的情况:

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


查看完整回答
反对 回复 2023-08-04
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信