When doing the product management system, encountered a problem:
When you get an input Box dialog box, String textprice = Joptionpane.showinputdialog ("Please enter the price increase (minus) to be adjusted");
If you click Cancel at this point, an exception will be thrown: java.lang.NullPointerException.
Cause: Clicking Cancel does not produce a string object.
Workaround:
1 DoublePrice = 0;2String Textprice =Joptionpane3. Showinputdialog ("Please enter the price increase (minus) amount to be adjusted"));4 5 if(Textprice = =NULL) {6 return;7 }8 Else if(Textprice.equals ("")){9Joptionpane.showmessagedialog (NULL, enter the price increase (minus) amount to adjust, prompt message, joptionpane.error_message);Ten return ; One } A Else{ - Try { -Price =double.parsedouble (textprice); the if(Pid.changeprice (Price)) { -Joptionpane.showmessagedialog (NULL, "Price adjustment is complete! "); - } -}Catch(Exception E1) { +Joptionpane.showmessagedialog (NULL, "wrong input! "); - } + A } at}
View Code
Javaswing the Input dialog box, click Cancel to throw exception resolution