Note that there are some explanatory texts on larger screens.

plurals
  1. POJDialog to get input, instead get error
    primarykey
    data
    text
    <p>I wrote a class that extends <code>JDialog</code>, to be shown when I press save (save is a <code>JMenuItem</code> object). However, when I press save, I get the dialog with an error of having file name as null, and I didn't get the chance to type anything.</p> <p>What am I doing wrong here? Any help would be appreciated.</p> <p>Thanks.</p> <p>Here is the code for my <code>JDialog</code> extended class:</p> <pre><code>package ui; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class SaveImageView extends JDialog { private final JPanel contentPanel = new JPanel (); private JTextField txtFilename; public int type; // 0 -&gt; png, 1 -&gt; jpg, 2 -&gt; gif public String filename; public SaveImageView () { setTitle("Save Image"); setBounds(100, 100, 450, 230); getContentPane().setLayout(null); contentPanel.setBounds(0, 0, 434, 229); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel); contentPanel.setLayout(null); { JLabel lblFilename = new JLabel("Filename: "); lblFilename.setBounds(10, 95, 58, 20); contentPanel.add(lblFilename); } { txtFilename = new JTextField(); txtFilename.setBounds(78, 95, 123, 20); contentPanel.add(txtFilename); txtFilename.setColumns(10); } { JRadioButton rdbtnGif = new JRadioButton("GIF"); rdbtnGif.setBounds(123, 11, 43, 23); contentPanel.add(rdbtnGif); rdbtnGif.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { type = 2; } }); } { JRadioButton rdbtnJpg = new JRadioButton("JPG"); rdbtnJpg.setBounds(123, 37, 43, 23); contentPanel.add(rdbtnJpg); rdbtnJpg.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { type = 1; } }); } { JRadioButton rdbtnPng = new JRadioButton("PNG"); rdbtnPng.setBounds(123, 63, 45, 23); contentPanel.add(rdbtnPng); rdbtnPng.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { type = 0; } }); } JLabel lblImageTypeTo = new JLabel("Image type to save: "); lblImageTypeTo.setBounds(10, 11, 144, 23); contentPanel.add(lblImageTypeTo); JLabel lblNote = new JLabel("NOTE: Images will be saved in \"output\" folder."); lblNote.setBounds(10, 132, 273, 22); contentPanel.add(lblNote); { JPanel buttonPane = new JPanel(); buttonPane.setBounds(0, 160, 434, 33); contentPanel.add(buttonPane); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); { JButton okButton = new JButton("OK"); okButton.setActionCommand("OK"); buttonPane.add(okButton); getRootPane().setDefaultButton(okButton); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { filename = txtFilename.getText(); } }); } { JButton cancelButton = new JButton("Cancel"); cancelButton.setActionCommand("Cancel"); buttonPane.add(cancelButton); } } } } </code></pre> <p>And this is in my main GUI class, this is when I add the <code>ActionListener</code> to the item:</p> <pre><code>////////////////////////////////////////////////////////// // Save - Menu Item // ////////////////////////////////////////////////////////// JMenuItem saveItem = new JMenuItem("Save"); saveItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { SaveImageView siv = new SaveImageView(); siv.setVisible(true); String typeStr = (siv.type == 0) ? "png" : ((siv.type == 1) ? "jpg" : "gif"); try { ImageIO.write(img, typeStr, new File("output/" + siv.filename + "." + typeStr)); } catch (IOException e1) { e1.printStackTrace(); } } }); menu.add(saveItem); </code></pre> <p>This is the error I'm getting:</p> <pre><code>java.io.FileNotFoundException: output\null.png (The system cannot find the path specified) at java.io.RandomAccessFile.open(Native Method) at java.io.RandomAccessFile.&lt;init&gt;(Unknown Source) at javax.imageio.stream.FileImageOutputStream.&lt;init&gt;(Unknown Source) at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source) at javax.imageio.ImageIO.createImageOutputStream(Unknown Source) at javax.imageio.ImageIO.write(Unknown Source) at ui.PPMViewer$1.actionPerformed(PPMViewer.java:40) 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.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$400(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(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.AccessControlContext$1.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) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.imageio.ImageIO.write(Unknown Source) at ui.PPMViewer$1.actionPerformed(PPMViewer.java:40) 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.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$400(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(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.AccessControlContext$1.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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload