0

In my program, I have created a menu bar which allows the user to switch between three programs using file chooser. However, it does not seem to be compiling or working. I am pretty sure my code is incorrect however, I could only find a few examples to work with. I have copied the code that for when "Basic" is clicked on, it is supposed to open BasicCalculator.java. Any help would be appreciated.

JMenuBar menuBar = new JMenuBar();
JMenu calculatorMenu = new JMenu("Calculator");
menuBar.add(calculatorMenu);
JMenuItem basic = new JMenuItem("Basic");
JMenuItem intermediate = new JMenuItem("Intermediate");
JMenuItem scientific = new JMenuItem("Scientific");

basic.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        if (e.getActionCommand().equals("Basic")){
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new File(System.getProperty("BasicCalculator.java")));
            int result = fileChooser.showOpenDialog(frame);
            if (result == JFileChooser.APPROVE_OPTION){
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            }
        }
    }
});
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
assassinweed2
  • 71
  • 1
  • 3
  • 9
  • 2
    `"However, it does not seem to be compiling..."` -- if you're having problems compiling the code and need help, posting the compiler's error message and indicating which line in the posted code is causing it absolutely necessary if we're to understand your problem. – Hovercraft Full Of Eels Dec 05 '16 at 01:00
  • Without this, your question remains quite unclear. – Hovercraft Full Of Eels Dec 05 '16 at 01:01
  • You don't open a file to start a Java application. You create a new instance of the class by using the "new" keyword. You would do something like: `JDialog basic = new BasicCalculator()`. In the constructor of the class you would create all the components of the calculator and add them to the dialog and make the dialog visible. This of course assumes that your class extends JDialog. – camickr Dec 05 '16 at 01:02
  • @HovercraftFullOfEels so sorry, I just realised I forgot to post the error... Here it is : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.io.File. – assassinweed2 Dec 05 '16 at 01:07
  • In the future, search on your exception type as you'll likely find out how to solve it by this simple task. For instance [this Google search](https://www.google.com/search?q=site%3Astackoverflow.com+java&oq=s&ie=UTF-8#q=site:stackoverflow.com+java+NullPointerException) first hit answers will explain all. – Hovercraft Full Of Eels Dec 05 '16 at 01:10

0 Answers0