Reputation: 499
I implemented a button and user clicks this button and the file chooser is popped up and user can choose the file however when he changes his preferences the program always understand the first file.
I found this bug by writing down size() method. First the user chose the device list which size is 20, after changing this document by another list which size is 100, I expected to see 100 as size but I still see 20.
chooser = new JButton("Choose Device List");
chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fileopen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("csv files", "csv");
fileopen.addChoosableFileFilter(filter);
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
try {
DeviceHelper.loadDevices(fileopen.getSelectedFile());
int size = DeviceHelper.loadDevices(fileopen.getSelectedFile()).size();
System.out.println("Size of the program " + size);
} catch (IOException e) {
}
}
}
});
How can I solve this? Could someone please help me?
EDIT:
public static List<String> loadDevices(File file2) throws IOException {
if (deviceAdresses == null) {
deviceAdresses = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(file2))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (line.trim().length() > 0) {
deviceAdresses.add(line);
}
}
}
}
return new ArrayList<String>(deviceAdresses);
}
Upvotes: 0
Views: 144
Reputation: 13872
Remove this check:
if (deviceAdresses == null) {
What is happening that once deviceAdresses
is initialized, this check will always be false and lines from new file will never be read.
Upvotes: 2