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
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.
Source: Stackoverflow.com