Search code examples
javajoptionpane

incompatible types using list.addElement(JOptionPane.showInputDialog();


I'm trying to store this part in a variable so I can use cancel button.. (using java,netbeans)

String a = list.addElement(JOptionPane.showInputDialog(null,"your name",JOptionPane.OK_CANCEL_OPTION));

this part of code takes the name that user want to insert and adds it in a Jlist. but I'm having the error "incompatible types required String found void"


Solution

  • Simply add a check for the return value from showInputDialog:

    String result = JOptionPane.showInputDialog(null,"your name",JOptionPane.OK_CANCEL_OPTION);
    if (result != null) {
       list.addElement(result);
    }