0

I have problem of raising the chooser.showDialog(contentPane,"Save As") if no file was loaded before (to let user save a new file). What should I change?

This is the faulty part of my code:

public class fileEditor extends JFrame {
    File storage;

    public String getName() {
        return storage.getName();
    }

    private JPanel contentPane;


    public fileEditor() {   
        contentPane = new JPanel()
        setContentPane(contentPane);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);


        JMenuItem mntmSaveAs = new JMenuItem("Save As...", KeyEvent.VK_A);
        KeyStroke ctrlAKeyStroke = KeyStroke.getKeyStroke("control A");
        mntmSaveAs.setAccelerator(ctrlAKeyStroke);
        mntmSaveAs.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveAs();
                setTitle(getName());
            }
        });
        mnFile.add(mntmSaveAs);
    }

    public void saveAs() {

        int ret = chooser.showDialog(contentPane, "Save As");
        if (ret == JFileChooser.APPROVE_OPTION) {

            storage = chooser.getSelectedFile();
            saveFile(chooser.getSelectedFile());
        }
    }
}

And here is the error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at zad4.fileEditor.saveAs(fileEditor.java:349)
    at zad4.fileEditor$4.actionPerformed(fileEditor.java:140)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
mallorn
  • 735
  • 1
  • 6
  • 10
  • 1
    This code doesn't compile (missing semicolons and more importantly `chooser` is not known in the `saveAs()` method). Try to correct your code so it will compile and then we can try to help you!!! – ParkerHalo Nov 19 '15 at 11:18
  • This code is only a part from my whole project. added `chooser = new JFileChooser();` in saveAs(); and it is OK – mallorn Nov 19 '15 at 11:28

0 Answers0