有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在线Java代码分析器

有人知道java是否有在线代码分析器吗。我想能够检查一些小的代码片段

示例:此方法具有以下警告:(空解引用)

private XMLGregorianCalendar asXMLGregorianCalendar(Date data) {
            DatatypeFactory dateformat = null;
            try {
             dateformat = DatatypeFactory.newInstance();
            } catch (MyException e) {
                 ///
            }
           if (data == null) {
           return null;
           } else {
          GregorianCalendar gregorianCal = new GregorianCalendar();
          gregorianCal.setTimeInMillis(data.getTime());
          return dateformat.newXMLGregorianCalendar(gregorianCal );
    }
}

我的新版本是:

private XMLGregorianCalendar asXMLGregorianCalendar(Date data) throws ComponentBlockingException {
        if (data == null) {
            return null;
        }
        DatatypeFactory dateformat = null;
        try {
            dateformat = DatatypeFactory.newInstance();
        } catch (MyException e) {
            ////
        } 
        GregorianCalendar gregorianCal = new GregorianCalendar();
        gregorianCal.setTimeInMillis(data.getTime());
        return dateformat.newXMLGregorianCalendar(gregorianCal );

    }

}

我想第二条路应该可以


共 (1) 个答案

  1. # 1 楼答案

    我不确定是否有任何可用的在线代码分析工具,但请允许我尝试帮助您进行代码分析

    如果由于某种原因,下面的try块遇到异常

    try {
            dateformat = DatatypeFactory.newInstance();
        }
    

    那么你的dateformat将保持为空。所以下面的声明

    return dateformat.newXMLGregorianCalendar(gregorianCal );
    

    容易出现空指针异常。因此,我相信你得到的是静态代码anlayzer错误

    在代码到达执行返回的行之前,必须确保dateformat已初始化或在所有情况下都不为null

    希望有帮助