2016-11-23 4 views
-2

이것은 내가 작성한 코드입니다. 하지만 때 키 및 값을 null 포인터를 가져 오는 값을 인쇄하려면 노력하고있어.컴퓨터에있는 속성 파일을로드 할 때 Null 포인터 예외가 발생합니다.

내 주요 관심사, 특성 파일에서 키와 값을로드하고 등록 정보 파일에 당신이 절대 경로를 가지고 있기 때문에지도

Map<String, String> prop1 = new HashMap<String, String>(); 
Map<String, String> prop2 = new HashMap<String, String>(); 

public static void main(String args[]) { 

    Sample sample = new Sample(); 
    sample.getLocaleOne(); 
} 

public void getLocaleOne() { 
    Properties prop = new Properties(); 
    InputStream input = null; 

    try { 
     File file = new File("Labels.properties"); 
     System.out.println("file.getAbsolutePath()); 
     input = this.getClass().getClassLoader().getResourceAsStream("C:/workspaceNewI18NChnages/customer/Labels.properties"); 

     prop.load(input); 

     Enumertion<?> e = prop.propertyNames(); 
     while (e.hasMoreElements()) { 
      String key = (String) e.nextElement(); 
      String value = prop.getProperty(key); 
      System.out.println("Key : " + key + ", Value : " + value); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+2

에 위치하며 사진으로 코드를 절대 게시 할 수 없습니다. 항상 질문에 넣으십시오 – XtremeBaumer

+0

사실, 이것은 실제로 루틴 널 포인터 문제가 아니며 잘못된 유형의 스트림을 사용하여 속성 파일을로드하고 있습니다. –

+0

getResourceAsStream은 클래스 경로에서 사용 가능한 자원을로드하기위한 것이지만 절대 파일 경로를 제공합니다. http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html을보십시오 – haggisandchips

답변

0

에 저장해야 할 것입니다 당신이 FileInputStream를 사용한다, 하지 리소스 스트림 :

Properties prop = new Properties(); 
String filename = "C:\\workspaceNewI18NChnages\\customer\\Labels.properties"; 
FileInputStream in = new FileInputStream(filename); 
props.load(in); 
0

당신은 그냥이 같은 java.util.ressourceBundle을 사용하는 특성 파일을 읽으려면 :

ResourceBundle bundle = ResourceBundle.getBundle("properties.login"); 
System.out.printLn(bundle.getString("login"));// read login property 
System.out.printLn(bundle.getString("pwd"));// read pwd property 

login.properties 파일은이 경우 패키지 속성

관련 문제