0
私は私の入力に接続するたびに、それは私にエラー与える:スレッドで給与計算
例外Calculate.calculatePayで "メイン" のjava.lang.NullPointerException
(Calculate.java:13)
をat Hours.main(Hours.java:30)
このエラーはなぜ発生するのですか?
これはHours.javaです:
import java.util.Scanner;
public class Hours {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Calculate hours = new Calculate();
Bills hours1 = new Bills();
System.out.println("Enter your hours for this week: ");
int Weekhours = input.nextInt();
System.out.println("Enter the number of days you work this week: ");
int Days = input.nextInt();
int new_days = (Days - 1);
System.out.println("Enter your hourly wage: ");
double Wage = input.nextDouble();
System.out.println("Did you work Sunday? <yes/no> ");
String Didworksun = input.nextLine();
System.out.println("How much money do you have saved already? ");
int Money = input.nextInt();
System.out.println("How much is your bills this month? ");
int Monthpayment = input.nextInt();
hours.calculatePay(Weekhours, Wage, Days, new_days);
hours1.bills(Monthpayment, Money);
hours.displayHours();
System.out.println();
}
}
そしてこれはCalculate.javaです:
import java.util.Scanner;
public class Calculate {
public int Weekhours, Days, new_days;
public double pay, Wage, payment, weekpay, Sundaypay, premium;
public String Didworksun, Night;
Scanner input = new Scanner(System.in);
public double calculatePay(int Weekhours, double Wage, int Days, int new_days) {
if (Didworksun.equals("y") || Didworksun.equals("Y") || Didworksun.equals("yes") || Didworksun.equals("Yes") || Didworksun.equals("YworkES") || Didworksun.equals("YeS") || Didworksun.equals("yEs") || Didworksun.equals("yeS") || Didworksun.equals("YEs") || Didworksun.equals("yES")) {
System.out.println("How many hours did you work on sunday? ");
double Sundayhours = input.nextDouble();
System.out.println("You worked " + (Sundayhours) + (" hours on Sunday."));
double Sundaywage = (Wage * 1.5);
double Sundaypay = (Sundayhours * Sundaywage);
double new_hours = (Weekhours - Sundayhours);
double weekpay = (new_hours * Wage);
int Worksunday1 = 1;
System.out.println("How much is night premium? ");
double Premcash = input.nextDouble();
double premium = (Premcash * (Days + Worksunday1));
pay = Sundaypay + weekpay + premium;
double taxes = .21;
double tax_payment = pay * taxes;
payment = (pay - tax_payment);
}else if (Didworksun.equals("n") || Didworksun.equals("N") || Didworksun.equals("no") || Didworksun.equals("No") || Didworksun.equals("NO") || Didworksun.equals("nO")) {
System.out.println("You did not work Sunday.");
int new_hours = Weekhours;
double weekpay = (new_hours * Wage);
System.out.println("How much is night premium? ");
double Premcash = input.nextDouble();
double premium = (Premcash * Days);
double taxes = .21;
double tax_payment = pay * taxes;
pay = weekpay + premium;
payment = (pay - tax_payment);
} else {
System.out.println("Error: Invalid response. Try again");
return calculatePay(Weekhours, Wage, Days, new_days);
}
return payment;
}public void displayHours() {
System.out.printf(" /nYour paycheck will be: %f", payment);
}
}
Bills.java
はimport java.util.Scanner;
public class Bills extends Calculate {
private int Money;
private int Monthpayment;
public void bills(int Monthpayment, int Money) {
Scanner input = new Scanner(System.in);
double until = (Monthpayment - (Money + payment));
if (until <= 0) {
System.out.println("You're able to make your payment");
} else {
System.out.println("You need to save " + until + (" more dollars to make your payment"));
}
}
}
@Corpse権利。それは、あなたが「Didworksun」の考えで何をしようとしているかによって異なります。私はあなたが 'Hours'の22行目で読んだ値に設定したいと思っています。 – GavinCattell
だから私はそれをしましたが、今は_Didworksun_をfalseとして読み込み、Elseステートメントに進みます。 (無限ループを作成したので、今は0に戻しています) – Corpse