# Beginner at Java, Need help with my project its due tuesday.

• October 20th, 2013, 05:55 PM
Ancharius
Beginner at Java, Need help with my project its due tuesday.
Guidelines

This assignment is comprised of modifying your program which will calculate a monthly a moritzed amount (e.g. a house mortgage). For this program, you are to add the logic to prompt the user if they wish to enter the data again, then loop if they enter a "y" value; otherwise perform the calculations and the error checking as in the previous program.

Enter loan amount: 100000
Enter rate: 6
Enter number years: 30

The monthly payment is: \$599.55

Would you like to calculate again (y/n): y

Enter loan amount: -100000
Enter rate: 6
Enter number years: 30

You must enter positive numeric data!

Would you like to calculate again (y/n): n

__________________________________________________ __________________

Code Java:

```package monthypayment2; import java.text.NumberFormat; import java.util.Scanner;     public class Monthypayment2 { private static double monthlypay1; private static String choice; private static boolean n; private static String False; private static boolean y;       /** * @param args the command line arguments */ public static void main(String[] args) { Scanner in = new Scanner(System.in);   //format number and currency NumberFormat NF = NumberFormat.getCurrencyInstance(); NumberFormat NI = NumberFormat.getNumberInstance();     double months = 1; double numyears; double loanamount; double rate;   int repeat = 0; //condition for loop   do {       //prompt loan amount System.out.print("Enter Loan Amount:"); loanamount = in.nextDouble();   //prompt rate System.out.print("Enter Rate:"); rate = in.nextDouble();   //prompt years System.out.print("Enter Number of Years:"); numyears = in.nextDouble();     //calculation monthly monthlypay1 = (loanamount * rate / 12) / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);     System.out.println("The Monthly Payment is: \$" + monthlypay1);   y = true; n = false;   if (monthlypay1 < 0) { System.out.print("You need to enter positive numerical data!");} else { System.out.print("Would you like to continue calculations(y/n)?"); choice = in.nextLine(); } } while (choice.equals("y"));   while(choice.equals("n")) { break; } }     }```

Basically when I run the program the Math is still off despite me getting help and doing all suggestions that users gave me on here. I compared it with the formula and it's correct.
When i put in a negative loan amount i get this error
"Exception in thread "main" java.lang.NullPointerException"
at line 65(which would be
Code Java:

` while (choice.equals("y"));`
after asking if I would like to calculate again.
I've restarted a couple times on the project but I keep running into the same problems.
Can anyone help me?

https://scontent-b-iad.xx.fbcdn.net/...95&oe=526636AF <--- formula that I am using.
• October 20th, 2013, 10:57 PM
Junky
Re: Beginner at Java, Need help with my project its due tuesday.
Code :

`private static String choice;`
The variable choice is not initialised so it is given the default value of null. If your code never assigns a value to choice then you get the NPE. You should always initialise your variables with appropriate values.
• October 20th, 2013, 11:01 PM
Ancharius
Re: Beginner at Java, Need help with my project its due tuesday.
for the math I tried breaking it down but still doesn't calculate correctly.
Code Java:

```double calc1 = loan * rate; double calc2 = Math.pow(1 + rate, months); double calc3 = Math.pow(1 + rate, months)-1; double calc4 = calc1 / calc2; monthlypay1 = calc4 / calc3;```