2013-08-23 34 views
0

我想從jar文件中加載屬性文件,但不能這樣做。 以下是一流的,其中我加載文件從jar文件中嵌入屬性文件時出錯

public class PropertyClass { 
    private static Properties properties; 

    public String getProperty(String propertyName) { 
     try { 
      InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("resources/test.properties"); 

      System.out.println("Input Stream " + inputStream); 
      properties.load(inputStream); 

      inputStream.close(); 
      if (properties == null) { 
       System.out.println("Properties null"); 
      } 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
     return properties.getProperty(propertyName); 
    } 
} 

的類文件和屬性文件都被擠滿了罐子裏面的代碼。但是,當我從其他方法試圖加載文件它提供了以下錯誤: -

Input Stream - [email protected]b1 
Exception in thread "main" java.lang.NullPointerException 
at PropertyClass.getProperty(PropertyClass.java:16) 

它不顯示輸入流爲空 以下位在我的代碼行16 - properties.load(的inputStream) ;

這個

回答

3

您需要initalise您Properties對象任何幫助後,才能調用properties.load()

private static Properties properties = new Properties();