タイトルで述べたように、私は.properties
ファイルをjavaで読み込み、Properties
オブジェクトに格納する必要があります。私はjFileChooser
をJavaのスイングから実際に動作するファイルを取得し、引数を呼び出してファイルを新しいウィンドウに渡してからProperties
オブジェクトに格納するのにload()
メソッドを使用しますが、java.lang.NullPointerException
エラーが発生します。私はそれを説明しようとしているように私は明確だったと思う。java読み込み.propertiesファイル
これはコードです:
public void actionPerformed(ActionEvent e3) { //when button is pressed
JFileChooser fc2 = new JFileChooser (new File("C:\\Users\\Ciurga\\workspace\\PropertiesManager"));
fc2.setDialogTitle("Load Default Values File");
fc2.setFileFilter(new FileTypeFilter(".properties", "Properties File"));
int result = fc2.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
df = fc2.getSelectedFile(); //getting selected file and storing it in "df" which will be passed as calling argument
defaultNameTextField.setText(df.getName());
}
}
これは私が他のウィンドウにファイルを渡す方法です:
public void actionPerformed(ActionEvent e1) {
FormWindow w1 = new FormWindow (df, lf); //when button is pressed, create new window passing the files i got with jFileChooser
}
そして、これは私がそれを保存しようとした方法ですa Properties
オブジェクト:
private static Properties propDef;
private static Properties propLim;
private void run(File def, File lim) {
try {
propDef.load(new FileInputStream(def));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
propLim.load(new FileInputStream(lim));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(propDef.getProperty("name"));
}
ありがとう、私はそれを初期化しなければならなかったと言ったように、今は正しく動作するようだ、それは単純なエラーでしたが、私は実際にはJavaの初心者です。 これは私が変更するものである:、あなたがpropDefを初期化しません
private static Properties propDef = new Properties();
private static Properties propLim = new Properties();
'propDef'と' propLim'を初期化しましたか?そうでない場合、それらは 'null'で、' load'を呼び出すと 'NullPointerException'が返されます。 – Berger