1

我目前正在为一家公司做一个项目,我们从微软获取了一个开源的家谱项目(源代码可以在这里找到:Family History and the source code on their GForge),但我遇到了问题一直。我已经解决了大部分编译错误,因为它们是简单的异常错误,但这是我还没有能够解决的错误。我用谷歌搜索了这个问题并找到了许多类似的解决方案,但似乎没有一个工作得很好。如果您需要更多信息,请告诉我,我会尽力提供给您。虽然,我仍然是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);
    }
}
*/
}
4

1 回答 1

1

context必须在构造函数完成时初始化,因为它是一个final字段。

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

于 2012-12-08T04:22:22.310 回答