public Lab7(File file) {
List<Item> items = null;
try {
items = InventoryReader.read(file);
} catch (ApplicationException e) {
LOG.error(e.getMessage());
return;
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, use the default.
}
try {
MainFrame frame = new MainFrame(items); // THIS IS WHERE I HAVE TROUBLE
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Background: I set the constructor of MainFrame to accept a list. How do I do this in the main() of my application?
I get the error:
Cannot refer to a non-final variable “items” inside an inner class defined in a different method
The error is in the MainFrame frame = new MainFrame(items) I can’t seem to pass MainFrame class the items variable… Why is that?
How do I pass this variable into into the MainFrame frame?
You have two choices….
Choice one…
Make the
itemslist final so it is accessiable from within theRunnables context…Choice two…
Move the
itemslist into theRunnables context