java项目贷款NullPointerException
有人能帮我解决这个异常吗?结果出来了,但仍然得到了错误。有四个其他类连接到它java项目贷款NullPointerException,java,arrays,exception,Java,Arrays,Exception,有人能帮我解决这个异常吗?结果出来了,但仍然得到了错误。有四个其他类连接到它 public class CreateLoans { public static void main(String[] args) { Scanner s = new Scanner(System.in); int num; int term; int choice; String name; double amo
public class CreateLoans {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int num;
int term;
int choice;
String name;
double amount;
Loan[] w = new Loan[5];
for (int i = 0; i < 1; i++) {
System.out.println("Choose Loan Type: 1 Business Loan 2 Personal Loan");
choice = s.nextInt();
if (choice == 1) {
System.out.print("Enter Loan Number: ");
num = s.nextInt();
System.out.print("Enter Last Name: ");
s.nextLine();
name = s.nextLine();
System.out.print("Enter Loan Amount: ");
amount = s.nextDouble();
System.out.print("Enter Number of Terms: ");
term = s.nextInt();
w[i] = new BusinessLoan(num, name, amount, term);
} else if (choice == 2) {
System.out.print("Enter Loan Number: ");
num = s.nextInt();
System.out.print("Enter Last Name: ");
s.nextLine();
name = s.nextLine();
System.out.print("Enter Loan Amount: ");
amount = s.nextDouble();
System.out.print("Enter Number of Terms: ");
term = s.nextInt();
w[i] = new PersonalLoan(num, name, amount, term);
} else {
System.out.println("Error, Please Enter 1 Or 2");
}
}
for (int i = 0; i < w.length; i++) {
System.out.println(w[i].toString());
}
}
}
不确定要执行什么操作:由于for循环是:
for (int i = 0; i < 1; i++) {// the code inside this loop only execute one time
...
}
当我为1时,w[i]w[1]将为null,因此当您调用w[i].toString时,您将得到一个
快速修复应更改为int i=0;i<1;i++to表示int i=0;i
for (int i = 0; i < w.length; i++) {
System.out.println(w[i].toString());
}