[java] Read input from a JOptionPane.showInputDialog box

I am almost done with this project. It's my very first one and I need to read a little better the inputs from the JOption box. For example, I want to go back to the previous page if the user clicks on "cancel" or close the program. So far, every time I click on "cancel" my program crashes. help me out! I just started learning Java about a month ago. I think I am doing pretty alright. Thanks so very much!

Here is the code,

import java.util.Scanner; import javax.swing.JOptionPane;  /**  * Write a description of class Operation here.  *  * @author Charles Kossivi  * @version 1.0  */ public class Operation {      static String firstNumber; //first number or coefficient     static String secondNumber; //second number or coefficient     static String thirdNumber; //third number or coefficient     static String operationType = ""; //Operation type     static boolean anotherOperation = false;     static boolean goodType = true; //true=run program false=stop program     static boolean numberTypeA = true; // another operation?     static boolean numberTypeB = true; // another operation?      static boolean numberTypeC = true; // another operation?      static Scanner keyInput = new Scanner(System.in);     static String inputType; // temperature type     static double a; // first number     static double b; // second number     static double c; // third number     static double s; // sum of a and b     static double d; // difference of a and b     static double p; // product of a and b     static double r; // ratio of a and b     static double D; // discriminant     static double numType;      public static void main(String[] args) {         while (goodType) {             operationType = JOptionPane.showInputDialog("Welcome! \nPlease, Select the Desired Operation Type"                     + "(\nA=Addition, \nD=Division, \nE=Quadratic Equation, "                     + "\nM=Multiplication, \nS=Subtraction, \nQ=Quit");             if (operationType.equalsIgnoreCase("A")) {                 newAddition();             }             if (operationType.equalsIgnoreCase("S")) {                 newSubtraction();             }             if (operationType.equalsIgnoreCase("M")) {                 newMultiplication();             }             if (operationType.equalsIgnoreCase("D")) {                 newDivision();             }             if (operationType.equalsIgnoreCase("E")) {                 newQuadratic();             }             if (operationType.equalsIgnoreCase("Q")) {                 // quit                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                 goodType = false;             }         }         goodType = false;     }      //Start Addition class here     private static void newAddition() {         //read in first coefficient from user as a string         firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");         if (firstNumber.equalsIgnoreCase("Q")) {             // quit                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");             goodType = false;         } else {             a = Double.parseDouble(firstNumber);             //read in second coefficient from user as a string             secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");             if (secondNumber.equalsIgnoreCase("Q")) {                 // quit                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                 goodType = false;             } else {                 b = Double.parseDouble(secondNumber);                 //Adding the numbers                 s = a + b;                 //Display results                 JOptionPane.showMessageDialog(null, "The sum is " + s);             }         }     }      //End Addition class     //Start Addition class here     private static void newSubtraction() {         //read in first coefficient from user as a string         firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");         if (firstNumber.equalsIgnoreCase("Q")) {             // quit                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");             goodType = false;         } else {             a = Double.parseDouble(firstNumber);             //read in second coefficient from user as a string             secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");             if (secondNumber.equalsIgnoreCase("Q")) {                 // quit                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                 goodType = false;             } else {                 b = Double.parseDouble(secondNumber);                 //Subtraction the numbers                 d = a - b;                 //Display results                 JOptionPane.showMessageDialog(null, "The sum is " + d);             }         }     }//End Subtraction class      // Begin Multiplication class     private static void newMultiplication() {         //read in first coefficient from user as a string         firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");         if (firstNumber.equalsIgnoreCase("Q")) {             // quit                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");             goodType = false;         } else {             a = Double.parseDouble(firstNumber);             //read in second coefficient from user as a string             secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");             if (secondNumber.equalsIgnoreCase("Q")) {                 // quit                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                 goodType = false;             } else {                 b = Double.parseDouble(secondNumber);                 //multiplying the numbers                 p = a * b;                 //Display results                 JOptionPane.showMessageDialog(null, "The product is " + p);             }         }     } // End Multiplication class      //Start Division class     private static void newDivision() {         //read in first coefficient from user as a string         firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");         if (firstNumber.equalsIgnoreCase("Q")) {             // quit                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");             goodType = false;         } else {             a = Double.parseDouble(firstNumber);             //read in second coefficient from user as a string             secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");             if (secondNumber.equalsIgnoreCase("Q")) {                 // quit                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                 goodType = false;             } else {                 b = Double.parseDouble(secondNumber);                 //Dividing the numbers                 r = a / b;                 //Display results                 JOptionPane.showMessageDialog(null, "The ratio is " + r);             }         }     } //End Division class      //Start Quadratic class     private static void newQuadratic() {         //read in first coefficient from user as a string         firstNumber = JOptionPane.showInputDialog("Please, Enter First Number");         if (firstNumber.equalsIgnoreCase("Q")) {             // quit                JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");             goodType = false;         } else {             a = Double.parseDouble(firstNumber);             //read in second coefficient from user as a string             secondNumber = JOptionPane.showInputDialog("Please, Enter Second Number");             if (secondNumber.equalsIgnoreCase("Q")) {                 // quit                    JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                 goodType = false;             } else {                 b = Double.parseDouble(secondNumber);                 //read in third coefficient from user as a string                 thirdNumber = JOptionPane.showInputDialog("Please, Enter Third coefficient");                 if (thirdNumber.equalsIgnoreCase("Q")) {                     // quit                        JOptionPane.showMessageDialog(null, "\nProgram ended. \nThank You!");                     goodType = false;                 } else {                     c = Double.parseDouble(thirdNumber);                     //Quadratic formula                     D = (Math.sqrt(b * b - 4 * a * c)) / (2 * a);                     //Display results                     if (D >= 0) {                         JOptionPane.showMessageDialog(                                 null, "The equation's first real root is " + ((-b + D) / (2 * a)) + "\n"                                 + "The equation's second real root is " + ((-b - D) / (2 * a)) + "\n");                     } else {                         JOptionPane.showMessageDialog(                                 null, "The equation has no real solution");                     }                 }             }         }     }// end class quadratics } 

This question is related to java swing modal-dialog joptionpane cancel-button

The answer is


Your problem is that, if the user clicks cancel, operationType is null and thus throws a NullPointerException. I would suggest that you move

if (operationType.equalsIgnoreCase("Q")) 

to the beginning of the group of if statements, and then change it to

if(operationType==null||operationType.equalsIgnoreCase("Q")). 

This will make the program exit just as if the user had selected the quit option when the cancel button is pushed.

Then, change all the rest of the ifs to else ifs. This way, once the program sees whether or not the input is null, it doesn't try to call anything else on operationType. This has the added benefit of making it more efficient - once the program sees that the input is one of the options, it won't bother checking it against the rest of them.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background Read input from a JOptionPane.showInputDialog box Disable click outside of angular material dialog area to close the dialog (With Angular Version 4.0+) How can I display a modal dialog in Redux that performs asynchronous actions? Angular 2.0 and Modal Dialog How to use Bootstrap modal using the anchor tag for Register? Bootstrap modal opening on page load Trying to make bootstrap modal wider How to present a modal atop the current view in Swift Send parameter to Bootstrap modal window? Set bootstrap modal body height by percentage

Examples related to joptionpane

Read input from a JOptionPane.showInputDialog box Convert timestamp to string Return string Input with parse.string How to present a simple alert message in java? JOptionPane Yes or No window Multiple input in JOptionPane.showInputDialog JOptionPane Input to int Java - How to create a custom dialog box?

Examples related to cancel-button

Read input from a JOptionPane.showInputDialog box