Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 12:53 AM
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 05:45 AM
Senior Member
 
Join Date: Sep 2008
Posts: 493
Rep Power: 1
emceenugget is on a distinguished road
Default
NullPointerException means that you are trying to operate on a null variable. You seem to already know that choice1 is your null variable, so you just need to make sure that you properly handle the case of choice1 being null.

There might be more to it, but this is usually the problem.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2008, 04:01 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 472
Rep Power: 1
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Make sure your sending the choice1 value to calculatorChoice or declare it globally so any change will effect it throughout the whole program.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
error 530 error authentication required rgale JavaServer Pages (JSP) and JSTL 0 05-12-2008 04:28 PM


All times are GMT +2. The time now is 03:21 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org