2014-06-27 26 views
-1

我面對這個錯誤:java.lang.NumberFormatException:空

`Unable to load properties file for MultiWordNet 
Exception in thread "main" java.lang.NumberFormatException: null 
at java.lang.Integer.parseInt(Integer.java:417) 
at java.lang.Integer.<init>(Integer.java:660) 
at org.itc.mwn.MysqlDictionary.<init>(MysqlDictionary.java:85)` 

這是MysqlDictionary.java試圖讀取屬性文件:

#------------------------------------------------------------ 
#Properties file properties MultiWordNet API 

#Hostname of the MySQL server 
MWN_HOSTNAME=localhost 

#User 
MWN_USER=root 

#Password 
MWN_PASSWD= 

#Database name 
MWN_DB=wordnet 

#Cache of entity 
CACHE_CAPACITY=1000 

最後,這是代碼失敗的部分:

public MysqlDictionary() { 
    try { 
     connectionParameters = new Properties(); 
     connectionParameters.load(new FileInputStream(new File("./conf/multiwordnet.properties"))); 
    } catch (java.io.IOException ioee) { 
     System.err.println("Unable to load properties file for MultiWordNet"); 
    } 

    /// connection drivers instance 
    try { 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     //Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
    } catch(ClassNotFoundException E){ 
     System.err.println("Unable to load driver"); 
    } catch(IllegalAccessException E){ 
     System.err.println("Unable to load driver"); 
    } catch(InstantiationException E){ 
     System.err.println("Unable to load driver"); 
    } 

    // MultiWordnet db connection 
    String host = connectionParameters.getProperty("MWN_HOSTNAME"); 
    String user = connectionParameters.getProperty("MWN_USER"); 
    String passwd = connectionParameters.getProperty("MWN_PASSWD"); 
    String dbname = connectionParameters.getProperty("MWN_DB"); 
    Integer cache = new Integer(connectionParameters.getProperty("CACHE_CAPACITY")); 
    //here is where the parsing fails, but the file is properly written! 
try { 
     DEFAULT_CACHE_CAPACITY = cache.intValue(); 

     String conn = "jdbc:mysql://" + host + "/" + dbname; 
     this.db = DriverManager.getConnection(conn,user,passwd); 
     this.stmt = db.createStatement(); 


     System.err.println("Welcome to the MultiWordNet API\nConnection database ...OK\n"); 
    } catch (SQLException E) { 
     System.out.println("Unable to establish multiwordnet Mysql DB connection on " + host + "(" + user + " - " + passwd + ")"); 
     E.printStackTrace(System.out); 
    } 

奇怪的是,程序啓動後突然失敗,運行correctl後y

+0

您確定'connectionParameters'已正確加載幷包含所有值嗎? – MadProgrammer

+1

檢查屬性文件,查找關鍵字:CACHE_CAPACITY,確保文件中的拼寫與您的代碼匹配 – Arvind

+0

您在哪裏放置了multiwordnet.properties您可以請求層次結構 – SparkOn

回答

0

在java中處理File時,不會與相對路徑發生衝突。它們是非常不可預測的,因爲它們依賴於你沒有完全控制的當前工作目錄。使用類路徑代替,使用類加載器加載資源:

Properties properties = new Properties(); 
properties.load(getClass().getResourceAsStream("multiwordnet.properties")); 

你的問題不是不能夠解析一個整數,但不能夠加載文件。

+0

好的,我同意你的看法,但是,假設文件路徑是正確的,爲什麼不能加載文件? – DaniloP

+1

我認爲問題恰恰在於文件路徑不正確......嘗試加載文件時應檢查工作目錄並查看路徑是否正確。嘗試'System.out.println(「Working Directory =」+ System.getProperty(「user.dir」));' – Narmer

+0

你是男人!非常感謝! – DaniloP

0

檢查不規則的不可見字符,如<SHIFT>+<SPACE><CTRL>+<SPACE>或其他,CACHE_CAPACITY=1000旁邊。

聽起來有點褻瀆,但我經常足夠偶然發現這樣的事情。

+0

這是我檢查的第一件事。不幸的是,配置文件一切正常,謝謝。 – DaniloP

0

檢查在你的屬性空格的文件或嘗試如下

String cacheProp = connectionParameters.getProperty("CACHE_CAPACITY") 
//For debug 
System.out.println("cacheProp="+cacheProp); 
Integer cache = new Integer(cacheProp.trim()); 
0

此代碼爲我工作得很好我不停的性能在資源文件夾中的文件中(src /主/資源)

public static void MysqlDictionary() { 
      try { 
       Properties p = new Properties(); 
       p.load(MyTest.class.getResourceAsStream("/test.properties")); 
       String host = p.getProperty("MWN_HOSTNAME"); 
       String user = p.getProperty("MWN_USER"); 
       String passwd = p.getProperty("MWN_PASSWD"); 
       String dbname = p.getProperty("MWN_DB"); 
       String cache = p.getProperty("CACHE_CAPACITY"); 
       int i = Integer.parseInt(cache); 
       System.out.println(host+"\t"+user+"\t"+passwd+"\t"+dbname+"\t"+i); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
相關問題