Search code examples
javaswingloopsnullpointerexceptionjoptionpane

Condition with Swing Options


I get NullPointerException when I press the CANCEL Option in my Swing.

Code:

while(!input.equals(CANCEL_OPTION));

And I get a message that it says incompatible types....

And here's my full code..

do
    {
    input=JOptionPane.showInputDialog("Enter a String.");


    for(int i =input.length()-1;i>=0;i--)
    {           
        result = result + (input.charAt(i));
        disp.append("\n").append(result);
    }       

    JOptionPane.showMessageDialog(null,input+"\n"+disp+"\n"+"\n"+result);
    input="";
    disp.setLength(0);
    result="";
    }
    while(!input.equals(CANCEL_OPTION));

And the error code or something..

Exception in thread "main" java.lang.NullPointerException
at Reverse.main(Reverse.java:15)
Java Result: 1

Solution

  • just use try catch statement..

    try
        {
        do
        {
            //your codesss......
        }
        while(!input.equals(null));
        }
        catch(Exception ex)
        {
         ////code..
        }
    

    hope it help.