Question

Im new editating JOptionPane buttons so i have & JOptionPane.showInputDialog that needs to be closed when i click cancel button, the issue is that i've a Exception that shows Error creating the file if i click it, i just need the JOptionPane.showInputDialog closed when i click cancel (doing nothing), i can't assing a value like 2 that means close window.

here's the code that i've done.....

if(Integer.parseInt(JOptionPane.showInputDialog(this,"Capture File Name","Mandatory Filed",JOptionPane.ERROR_MESSAGE)) 
                    == JOptionPane.CANCEL_OPTION){
Was it helpful?

Solution

Looking at JOptionPane.showInputDialog() javadoc, this method returns a String:

public static String showInputDialog(Component parentComponent,
                     Object message,
                     String title,
                     int messageType)

If Cancel button is pressed it will return null so you get an exception trying to parse null as an Integer. You should do this check instead:

String value = JOptionPane.showInputDialog(this,"Capture File Name","Mandatory Filed",JOptionPane.ERROR_MESSAGE);
if(value == null) {
    // Cancel button was pressed, not value returned do whataver you need to do
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top