Exception in thread "main" java.lang.NullPointerException

I cannot figure out why my program is giving me this exception. Can y

Page 1 of 1

8 Replies - 5895 Views - Last Post: 15 March 2009 - 01:48 AM Rate Topic: -----

#1 TrainerGodd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-February 09

Exception in thread "main" java.lang.NullPointerException

Post icon  Posted 12 March 2009 - 06:01 AM

import java.util.Scanner;
import java.text.NumberFormat;
public class AnnuityV4 {

	static Scanner userEntry = new Scanner(System.in);
	static char response;
	static String newResponse;
	static double beginningMonthlyPayment;
	static double endingMonthlyPayment;
	static double interestRate;
	static double term;
	static double monthlyInterestRate;
	static double interestEarned;
	static double beginningBalance = 0;
	static double endingBalance = 0;
	static double initialBalance = 0;

	public static void main(String[] args) {

  System.out.print("Do you have an annuity (Y/N):  ");
		response = userEntry.findInLine(".").charAt(0);

		if (response == 'y' || response == 'Y') {

			GetFinalValue();

}
		NumberFormat cf = NumberFormat.getCurrencyInstance();
		System.out.println("Your ending balance is " + cf.format(endingBalance));

		AnotherAnnuity();

  }

	public static void GetFinalValue(){

			GetBeginningMonthlyPayment();
			GetEndingMonthlyPayment();

			GetInterestRate();
			GetTerm();

			monthlyInterestRate = interestRate / 12;


			for (int i = 1; i <= term; i++){

			interestEarned = (beginningMonthlyPayment + beginningBalance) * monthlyInterestRate;
			endingBalance = beginningMonthlyPayment + interestEarned + endingMonthlyPayment + beginningBalance;


			NumberFormat cf = NumberFormat.getCurrencyInstance();

			if (i == 1) {
			System.out.println(i + "\t	" + cf.format(initialBalance) + "\t	" + cf.format(beginningMonthlyPayment) + "\t	" + cf.format(interestEarned) + "\t	" + cf.format(endingMonthlyPayment) + "\t	" + cf.format(endingBalance));
			}
			else {System.out.println(i + "\t	" + cf.format(beginningBalance) + "\t	" + cf.format(beginningMonthlyPayment) + "\t	" + cf.format(interestEarned) + "\t	" + cf.format(endingMonthlyPayment) + "\t	" + cf.format(endingBalance));
			}

			beginningBalance = endingBalance;
			}

	}
	public static void GetBeginningMonthlyPayment() {
	System.out.print("Please enter the monthly payment at beginning of the month:  ");
	beginningMonthlyPayment = userEntry.nextDouble();

	}

	public static void GetEndingMonthlyPayment(){
	System.out.print("Please enter the monthly payment at the end of the month:  ");
	endingMonthlyPayment = userEntry.nextDouble();

	}

	public static void GetInterestRate() {
		System.out.print("Please enter the annual interest rate (enter 8% as .08): ");
		interestRate = userEntry.nextDouble();

	}

	public static void GetTerm() {
		System.out.print("Please enter the term in months: ");
		term = userEntry.nextInt();

	}

	public static void AnotherAnnuity() {
		System.out.print("Do you have another Annuity? ");
		response = userEntry.findInLine(".").charAt(0);
		if (response == 'y' || response == 'Y') {
				GetFinalValue();
		}
		else {
				System.out.println("Thanks for using the Annuity Calculator!");
		}
	}

}








Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 12 March 2009 - 06:35 AM

Please post your exception stack trace
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 12 March 2009 - 04:07 PM

	   response = userEntry.findInLine(".").charAt(0);


If there is "." in the scanner line findInLine() will return null
and charAt(0) on null will generate your null pointer exception

you propably mean

response = userEntry.nextLine().charAt(0);
Was This Post Helpful? 0
  • +
  • -

#4 TrainerGodd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-February 09

Re: Exception in thread "main" java.lang.NullPointerException

Posted 13 March 2009 - 05:58 AM

View Postpbl, on 12 Mar, 2009 - 03:07 PM, said:

	   response = userEntry.findInLine(".").charAt(0);


If there is "." in the scanner line findInLine() will return null
and charAt(0) on null will generate your null pointer exception

you propably mean

response = userEntry.nextLine().charAt(0);

I tried updating the code to your suggestion, but I am still getting an exception. Here is what the exception says:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at AnnuityV4.AnotherAnnuity(AnnuityV4.java:98)
Do you have another Annuity? at AnnuityV4.main(AnnuityV4.java:37)
Java Result: 1


Please help. Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 13 March 2009 - 06:57 PM

which means that

response = userEntry.nextLine()

returned a String of length 0
Was This Post Helpful? 0
  • +
  • -

#6 TrainerGodd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-February 09

Re: Exception in thread "main" java.lang.NullPointerException

Posted 14 March 2009 - 07:26 PM

I did exactly as suggested in your reply but I am still getting error messages that look like this:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Your ending balance is $1,236.64
at java.lang.String.charAt(String.java:687)
at AnnuityV5.AnotherAnnuity(AnnuityV5.java:95)
Do you have another Annuity? at AnnuityV5.main(AnnuityV5.java:36)
Java Result: 1

My code is below so you can see it. Please let me know what I am doing wrong.



import java.util.Scanner;
import java.text.NumberFormat;

public class AnnuityV5 {

	static Scanner userEntry = new Scanner(System.in);
	static char response;
	static char newResponse;
	static double beginningMonthlyPayment;
	static double endingMonthlyPayment;
	static double interestRate;
	static double term;
	static double monthlyInterestRate;
	static double interestEarned;
	static double beginningBalance = 0;
	static double endingBalance = 0;
	static double initialBalance = 0;

	public static void main(String[] args) {

  System.out.print("Do you have an annuity (Y/N):  ");
		response = userEntry.findInLine(".").charAt(0);

		if (response == 'y' || response == 'Y') {

			GetFinalValue();

}
		NumberFormat cf = NumberFormat.getCurrencyInstance();
		System.out.println("Your ending balance is " + cf.format(endingBalance));

		AnotherAnnuity();

  }

	public static void GetFinalValue(){

			GetBeginningMonthlyPayment();
			GetEndingMonthlyPayment();

			GetInterestRate();
			GetTerm();

			monthlyInterestRate = interestRate / 12;


			for (int i = 1; i <= term; i++){

			interestEarned = (beginningMonthlyPayment + beginningBalance) * monthlyInterestRate;
			endingBalance = beginningMonthlyPayment + interestEarned + endingMonthlyPayment + beginningBalance;


			NumberFormat cf = NumberFormat.getCurrencyInstance();

			if (i == 1) {
			System.out.println(i + "\t	" + cf.format(initialBalance) + "\t	" + cf.format(beginningMonthlyPayment) + "\t	" + cf.format(interestEarned) + "\t	" + cf.format(endingMonthlyPayment) + "\t	" + cf.format(endingBalance));
			}
			else {System.out.println(i + "\t	" + cf.format(beginningBalance) + "\t	" + cf.format(beginningMonthlyPayment) + "\t	" + cf.format(interestEarned) + "\t	" + cf.format(endingMonthlyPayment) + "\t	" + cf.format(endingBalance));
			}

			beginningBalance = endingBalance;
			}

	}
	public static void GetBeginningMonthlyPayment() {
	System.out.print("Please enter the monthly payment at beginning of the month:  ");
	beginningMonthlyPayment = userEntry.nextDouble();

	}

	public static void GetEndingMonthlyPayment(){
	System.out.print("Please enter the monthly payment at the end of the month:  ");
	endingMonthlyPayment = userEntry.nextDouble();

	}

	public static void GetInterestRate() {
		System.out.print("Please enter the annual interest rate (enter 8% as .08): ");
		interestRate = userEntry.nextDouble();

	}

	public static void GetTerm() {
		System.out.print("Please enter the term in months: ");
		term = userEntry.nextInt();

	}

	public static void AnotherAnnuity() {
		System.out.print("Do you have another Annuity? ");
		newResponse = userEntry.nextLine().charAt(0);
		if (newResponse == 'y' || newResponse == 'Y') {
				GetFinalValue();
		}
		else {
				System.out.println("Thanks for using the Annuity Calculator!");
		}
	}

}


Was This Post Helpful? 0
  • +
  • -

#7 casingh  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 29
  • Joined: 19-January 09

Re: Exception in thread "main" java.lang.NullPointerException

Posted 14 March 2009 - 09:07 PM

View PostTrainerGodd, on 14 Mar, 2009 - 06:26 PM, said:

I did exactly as suggested in your reply but I am still getting error messages that look like this:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
Your ending balance is $1,236.64
at java.lang.String.charAt(String.java:687)
at AnnuityV5.AnotherAnnuity(AnnuityV5.java:95)
Do you have another Annuity? at AnnuityV5.main(AnnuityV5.java:36)
Java Result: 1

My code is below so you can see it. Please let me know what I am doing wrong.



import java.util.Scanner;
import java.text.NumberFormat;

public class AnnuityV5 {

	static Scanner userEntry = new Scanner(System.in);
	static char response;
	static char newResponse;
	static double beginningMonthlyPayment;
	static double endingMonthlyPayment;
	static double interestRate;
	static double term;
	static double monthlyInterestRate;
	static double interestEarned;
	static double beginningBalance = 0;
	static double endingBalance = 0;
	static double initialBalance = 0;

	public static void main(String[] args) {

  System.out.print("Do you have an annuity (Y/N):  ");
		response = userEntry.findInLine(".").charAt(0);

		if (response == 'y' || response == 'Y') {

			GetFinalValue();

}
		NumberFormat cf = NumberFormat.getCurrencyInstance();
		System.out.println("Your ending balance is " + cf.format(endingBalance));

		AnotherAnnuity();

  }

	public static void GetFinalValue(){

			GetBeginningMonthlyPayment();
			GetEndingMonthlyPayment();

			GetInterestRate();
			GetTerm();

			monthlyInterestRate = interestRate / 12;


			for (int i = 1; i <= term; i++){

			interestEarned = (beginningMonthlyPayment + beginningBalance) * monthlyInterestRate;
			endingBalance = beginningMonthlyPayment + interestEarned + endingMonthlyPayment + beginningBalance;


			NumberFormat cf = NumberFormat.getCurrencyInstance();

			if (i == 1) {
			System.out.println(i + "\t	" + cf.format(initialBalance) + "\t	" + cf.format(beginningMonthlyPayment) + "\t	" + cf.format(interestEarned) + "\t	" + cf.format(endingMonthlyPayment) + "\t	" + cf.format(endingBalance));
			}
			else {System.out.println(i + "\t	" + cf.format(beginningBalance) + "\t	" + cf.format(beginningMonthlyPayment) + "\t	" + cf.format(interestEarned) + "\t	" + cf.format(endingMonthlyPayment) + "\t	" + cf.format(endingBalance));
			}

			beginningBalance = endingBalance;
			}

	}
	public static void GetBeginningMonthlyPayment() {
	System.out.print("Please enter the monthly payment at beginning of the month:  ");
	beginningMonthlyPayment = userEntry.nextDouble();

	}

	public static void GetEndingMonthlyPayment(){
	System.out.print("Please enter the monthly payment at the end of the month:  ");
	endingMonthlyPayment = userEntry.nextDouble();

	}

	public static void GetInterestRate() {
		System.out.print("Please enter the annual interest rate (enter 8% as .08): ");
		interestRate = userEntry.nextDouble();

	}

	public static void GetTerm() {
		System.out.print("Please enter the term in months: ");
		term = userEntry.nextInt();

	}

	public static void AnotherAnnuity() {
		System.out.print("Do you have another Annuity? ");
		newResponse = userEntry.nextLine().charAt(0);
		if (newResponse == 'y' || newResponse == 'Y') {
				GetFinalValue();
		}
		else {
				System.out.println("Thanks for using the Annuity Calculator!");
		}
	}

}



Hi

One quick fix would be to replace the following code,

[code]
public static void AnotherAnnuity() {
System.out.print("Do you have another Annuity? ");
//newResponse = userEntry.nextLine().charAt(0);
Scanner sc = new Scanner(System.in);
newResponse = sc.next().charAt(0);
[code]

The most likely reason for the error, is that the program doesn't wait for user input when it reaches that point. This code might help.

Another thing is you didn't mentioned if you are familiar with exception handling. If you are, then you can try catching the exception and probable print it to see exactly what went wrong.
Was This Post Helpful? 1
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4189
  • View blog
  • Posts: 11,863
  • Joined: 18-April 07

Re: Exception in thread "main" java.lang.NullPointerException

Posted 14 March 2009 - 09:59 PM

You still haven't fixed the findInLine issue on line 22, you should change that to nextLine() like the others have said.

// Line 22 should be...
response = userEntry.nextLine().charAt(0);



Once you fix this, the program should work fine for you. :)
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 15 March 2009 - 01:48 AM

Change your code to the following:

response = (userEntry.nextLine() + "N").charAt(0);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1