1
我目前正在爲一家公司開發一個項目,並且我們從微軟公司獲得了一個開源的家族樹項目(源代碼函數可以在這裏找到:Family History和源代碼GForge),但我一直遇到問題。我已經解決了絕大多數編譯錯誤,因爲它們是簡單的異常錯誤,但是這是一個我還沒有解決的錯誤。我搜索了這個問題,發現了許多類似的解決方案,但沒有一個看起來很正確。如果您需要更多信息,請告訴我,我會盡力嘗試並提供給您。雖然,我仍然是java的初學者。變量「context」可能尚未初始化,java JNDI util
編譯器(這是一個阿帕奇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);
}
}
*/
}
噢好吧,嗯......所以,我認爲應該刪除的兩個例外應該被刪除,並回到捕捉一般的例外狀態?我不得不改變它,因爲我從Maven得到了PMD錯誤,並且它也告訴我,不是捕獲泛型異常,而是捕獲NamingException。 –
那麼,你做什麼取決於你期待'JndiUtil'做什麼。如果我是你,我會聲明'JndiUtil'的構造函數,以便在其「throws」子句中包含'NamingException'和'IOException',並避免在構造函數中處理異常。 – Isaac
因此,您的原始代碼捕獲了一個泛型異常,並用'IllegalStateException'封裝了它。這會導致PMD錯誤,因爲您的PMD配置爲建議不要捕獲通用異常。如果您想維護設計並仍然滿足您當前的PMD配置,則應該捕獲顯式異常('IOException'和'NamingException')。如果您使用JDK 7,則可以執行catch(IOException | NamingException e)'。 – Isaac