View Single Post
  #1 (permalink)  
Old 11-22-2008, 01:53 AM
Smirre Smirre is offline
Member
 
Join Date: Nov 2008
Posts: 16
Rep Power: 0
Smirre is on a distinguished road
Exclamation Need help with an error that I'm getting...
Hi, I please need help with my code, the error message that I get when I run my code is:

" Exception in thread "main" java.lang.NullPointerException
at org.Java.Instructor.Project.Calculator(Project.jav a:228)
at org.Java.Instructor.Project.main(Project.java:354) "

Line 228 of my code is " int c2 = Integer.parseInt(choice1); "

and Line 354 is: " p1.Calculator(choice1, num1, num2, answer); "

Below is the code that contains 'choice1' and ONLY the code which has 'choice1'...

Code:
public String calculatorChoice() throws IOException {
	    	System.out.println("Please select an option of what you would like to do with this number from the menu below and hit <ENTER>: ");
	    
	    	System.out.println("\n*********************************************");
	    	System.out.println("---------------------------------------------");
	    	System.out.println("Please select an option from the list below: ");
	    	System.out.println("---------------------------------------------");
	    	System.out.println("1 - Add");
	    	System.out.println("2 - Subtract");
	    	System.out.println("3 - Multiply");
	    	System.out.println("4 - Divide (remainder included)");
	    	System.out.println("5 - Maximum and minimum value of two numbers");
	    	System.out.println("6 - Squareroot");
	    	System.out.println("7 - Absolute value of numbers");
	    	System.out.println("8 - Octal and Hexadecimal equivalent of numbers");
	    	System.out.println("9 - Round numbers");
	    	System.out.println("0 - Exit program");
	    	System.out.println("**********************************************");
	    	
	    	boolean inputCorrect = false;
				String choice1 = null;
				while (inputCorrect == false) {
	    			try {
	    				BufferedReader inStream = new BufferedReader (new InputStreamReader(System.in));
	    				System.out.print("Please enter your option and hit <ENTER>: ");
	    				choice1 = inStream.readLine();
	    				int c1 = Integer.parseInt(choice1);
	    				System.out.println("\nYou have entered choice number: " + c1);
	    				inputCorrect = true;
	    			} catch (NumberFormatException nfe) {
	    				System.out.println("You did not enter a valid  choice number:  " + "\""+ choice1 + "\"  is not in the list!!");
	    				inputCorrect = false;
	    			} 
	    		}
				return choice1;
	    }

Code:
public int Calculator(String choice1, String num1, String num2, double answer) {
	    	int c2 = Integer.getInteger(choice1);
			switch (c2) {
				case 1 :
					addMethod(num1, num2);
					System.out.print("The answer of " + num1 + " + "  + num2 + " is: " + decimals.format(answer));
					break;
				case 2 :
					subtractMethod(num1, num2);
					System.out.print("The answer of " + num1 + " - " + num2 + " is: " + decimals.format(answer));
					break;
				case 3 :
					multiplyMethod(num1, num2);
					System.out.print("The answer of " + num1 + " * " + num2 + " is: " + decimals.format(answer));
					break;
				case 4 :
					divideMethod(num1, num2);
					System.out.print("The answer of " + num1 + " / " + num2 + " is: " + decimals.format(answer));
					modulusMethod(num1, num2);
					System.out.print(" and the remainder is "  + decimals.format(answer));
					break;
				case 5 :
					maximumValueMethod(num1, num2);
					System.out.println("The maximum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer));
					minimumValueMethod(num1, num2);
					System.out.println("The minimum number between the numbers " + num1 + " and " + num2 + " is: " + decimals.format(answer));
					break;
				case 6 :
					squarerootMethod1(num1);
					System.out.println("The squareroot of value " + num1 + " is: " + decimals.format(answer));
					squarerootMethod2(num2);
					System.out.println("The squareroot of value " + num2 + " is: " + decimals.format(answer));
					break;
				case 7 :
					absoluteNumberMethod1(num1);
					System.out.println("The absolute number of " + num1 + " is: " + decimals.format(answer));
					absoluteNumberMethod2(num2);
					System.out.println("The absolute number of " + num2 + " is: " + decimals.format(answer));
					break;
				case 8 :
					octalEquivalentMethod1(num1);
					System.out.println("The octal equivalent of " + num1 + " is: " + octalEquivalentMethod1(num1));
					octalEquivalentMethod2(num2);
					System.out.println("The octal equivalent of " + num2 + " is: " + octalEquivalentMethod2(num2));
					hexadecimalEquivalentMethod1(num1);
					System.out.println("\nThe hexadecimal equivalent of " + num1 + " is: " + hexadecimalEquivalentMethod1(num1));
					hexadecimalEquivalentMethod2(num2);
					System.out.println("The hexadecimal equivalent of " + num2 + " is: " + hexadecimalEquivalentMethod2(num2));
					break;
				case 9 :
					roundMethod1(num1);
					System.out.println("The rounded number of " + num1 + " is: " + decimals.format(answer));
					roundMethod2(num2);
					System.out.println("The rounded number of " + num2 + " is: " + decimals.format(answer));
					break;
				case 0 :
					if (c2 == 0) {
						System.exit(1);
					}
					break;
	    	}
			return c2;
	    }

Code:
public String anotherCalculation(String moreCalculations) throws IOException {
			boolean inputCorrect = false; 
			while (inputCorrect == false) {
					try {						
						BufferedReader br3 = new BufferedReader (new InputStreamReader(System.in));
						System.out.print("\nWould you like to do another calculation? Y/N ");
						moreCalculations = br3.readLine();
						String s1 = "y";
						String s2 = "Y";
						if (moreCalculations.equals(s1) || moreCalculations.equals(s2)) {
							inputCorrect = true;
							while (inputCorrect = true){
								String num1 = null;
								num1 = inputNumber1(num1);
								System.out.println("");
								String choice1 = null;
								choice1 = calculatorChoice();
								System.out.println("");
								String num2 = null;
								num2 = inputNumber2(num1, num2);
								System.out.println("");
								double answer = 0;
								Calculator(choice1, num1, num2, answer);
								System.out.println("");
								anotherCalculation(moreCalculations);
								System.out.println("");
								inputCorrect = true;
							} 
						} else {
							programRuntime(timeIn);
							System.out.println("");
							System.exit(0);
						}
					} catch (IOException ex){
						System.out.println("You did not enter a valid answer:  " + "\""+ moreCalculations + "\"  is not in the list!!");
						inputCorrect = false;
					  }
			}
		     return moreCalculations;
		}
Can someone PLEASE help me, because I can't solve this problem.
Thank you
Reply With Quote