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!");
}
}
}
Exception in thread "main" java.lang.NullPointerExceptionI cannot figure out why my program is giving me this exception. Can y
Page 1 of 1
8 Replies - 5286 Views - Last Post: 15 March 2009 - 01:48 AM
#1
Exception in thread "main" java.lang.NullPointerException
Posted 12 March 2009 - 06:01 AM
Replies To: Exception in thread "main" java.lang.NullPointerException
#2
Re: Exception in thread "main" java.lang.NullPointerException
Posted 12 March 2009 - 06:35 AM
Please post your exception stack trace
#3
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);
#4
Re: Exception in thread "main" java.lang.NullPointerException
Posted 13 March 2009 - 05:58 AM
pbl, 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!
#5
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
response = userEntry.nextLine()
returned a String of length 0
#6
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.
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!");
}
}
}
#7
Re: Exception in thread "main" java.lang.NullPointerException
Posted 14 March 2009 - 09:07 PM
TrainerGodd, 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.
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.
#8
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.
Once you fix this, the program should work fine for you.
// Line 22 should be... response = userEntry.nextLine().charAt(0);
Once you fix this, the program should work fine for you.
#9
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);
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|