变量“context”可能尚未初始化,java JNDI util

时间:2012-12-08 04:20:25

标签: java maven maven-2 pmd

我目前正在为一家公司开发一个项目,我们从Microsoft获得了一个开源家族树项目​​(源代码函数可以在这里找到:Family History和他们{{3的源代码但是我一直遇到问题。我在大多数情况下解决了大多数编译错误,因为它们是简单的异常错误,但这个是我还没能解决的问题。我用Google搜索了问题并找到了许多类似的解决方案,但似乎没有一个正常工作。如果您需要更多信息,请告诉我,我会尽力为您提供。虽然,我还是java的初学者。

编译器(Apache Maven 2.2.1)给出了以下错误:

C:\mfhp-2.4.0\services\src\main\java\gov\hhs\fhh\service\locator\JndiUtil.java:
[68,4] variable context might not have been initialized

以下是文件的代码:

public final class JndiUtil {

private static final Logger LOG = Logger.getLogger(JndiUtil.class);
private static final String RESOURCE_NAME = "jndi.properties";

private static JndiUtil theInstance = new JndiUtil();

private final InitialContext context;

private JndiUtil() {
    try {
        Properties props = getProperties();
        context = new InitialContext(props);
    } catch (NamingException e) { //This was a fix I did
        LOG.error("Unable to initialize the JNDI Util.", e);
        throw new IllegalStateException(e);
    } catch (IOException ioe) { //This was a fix I did
    LOG.error("IOException", ioe);
    }
}

/**
 * @return jndi (& jms) properties
 * @throws IOException on class load error
 */
public static Properties getProperties() throws IOException {
    Properties props = new Properties();
    props.load(JndiUtil.class.getClassLoader().getResourceAsStream(RESOURCE_NAME));
    return props;
}

/**
 * @param name name to lookup
 * @return object in default context with given name
 */
public static Object lookup(String name) {
    return lookup(theInstance.context, name);
}

/**
 * @param ctx context
 * @param name name to get
 * @return object in contect with given name
 */
public static Object lookup(InitialContext ctx, String name) {
    try {
        return ctx.lookup(name);
    } catch (NamingException ex) {
        //LOG.error("------------------Here is what's in the context--(looking for " + name + ")----------");
        LOG.error("------------------Error looking up ctx context for: " + name + ")----------");
        //dump(ctx, 0);
        //LOG.error("-----------------------------------------------------------");
        throw new IllegalStateException(ex);
    }
}
/*
 * Method taken out to avoid looping messages into log file
private static void dump(javax.naming.Context ctx, int indent) {
    try {
        NamingEnumeration<NameClassPair> en = ctx.list("");
        while (en.hasMore()) {
            NameClassPair ncp = en.next();
            String cn = ncp.getClassName();
            String n = ncp.getName();
            LOG.info("\t\t\t\t\t\t".substring(0, indent) + n + " : " + cn);
            try {
                Object o = ctx.lookup(n);
                if (o instanceof Context) {
                    dump((Context) o, indent + 1);
                }
            } catch (Exception e) {
                LOG.info(e);
            }
        }
    } catch (NamingException ex) {
        LOG.info(ex);
    }
}
*/
}

1 个答案:

答案 0 :(得分:1)

必须在构造函数完成时初始化

context,因为它是final字段。

如果Properties props = getProperties();碰巧抛出异常,那么context将不会在构造函数结束时初始化。将捕获异常(通过您实施的“修复”),处理和处理将继续。从本质上讲,即使context未初始化,您的“修复”也会导致您的类构造函数成功结束。