本文介绍了如何在java中解决NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个简单的程序在行{System.out.println(Id =+ staffs [j] .getId());}中显示NullPointerException,我该如何解决这个问题?
谢谢
This simple programs shows NullPointerException in line { System.out.println("Id = " + staffs[j].getId());}, how can i solve this issue?
Thanks
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int ID;
String Name;
String LastName;
String FatherName;
int Salary;
String Position;
String input;
boolean repeat = true;
Scanner scan = new Scanner(System.in);
Staff[] staffs = new Staff[100];
int i = 0;
while (repeat) {
String userInput;
System.out.println("Please Enter Staff Information!");
System.out.println("ID: ");
ID = scan.nextInt();
System.out.println("Name: ");
Name = scan.next();
System.out.println("Last Name: ");
LastName = scan.next();
System.out.println("Father Name: ");
FatherName = scan.next();
System.out.println("Salary : ");
Salary = scan.nextInt();
System.out.println("Position: ");
Position = scan.next();
scan.nextLine();
System.out
.println("Type 'yes' or 'no' if you want to continure or not ");
input = scan.nextLine();
if ("yes".equals(input))
repeat = true;
else
repeat = false;
staffs[i++] = new Staff(ID, Name, LastName, FatherName, Salary,Position);
}
System.out.println("Thank you!");
for (int j = 0; j < staffs.length - 1; j++) {
System.out.println("Id = " + staffs[j].getId());
System.out.println("Name = " + staffs[j].getName());
System.out.println("LastName = " + staffs[j].getLastName());
System.out.println("FatherName = " + staffs[j].getFatherName());
System.out.println("Salary = " + staffs[j].getSalary());
System.out.println("Position = " + staffs[j].getPosition());
}
}
}
public class Staff {
public int ID;
public String Name;
public String LastName;
public String FatherName;
public int Salary;
public String Position;
public Staff(int ID, String Name, String LastName, String FatherName, int Salary, String Position){
this.ID = ID;
this.Name = Name;
this.LastName = LastName;
this.FatherName = FatherName;
this.Salary = Salary;
this.Position = Position;
}
public int getId(){
return ID;
}
public String getName(){
return Name;
}
public String getLastName(){
return LastName;
}
public String getFatherName(){
return FatherName;
}
public int getSalary(){
return Salary;
}
public String getPosition(){
return Position;
}
}
推荐答案
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
int ID;
String Name;
String LastName;
String FatherName;
int Salary;
String Position;
String input;
boolean repeat = true;
Scanner scan = new Scanner(System.in);
ArrayList ArrayList<staff> staffList= new ArrayList<staff>();
int i = 0;
while (repeat) {
String userInput;
System.out.println("Please Enter Staff Information!");
System.out.println("ID: ");
ID = scan.nextInt();
System.out.println("Name: ");
Name = scan.next();
System.out.println("Last Name: ");
LastName = scan.next();
System.out.println("Father Name: ");
FatherName = scan.next();
System.out.println("Salary : ");
Salary = scan.nextInt();
System.out.println("Position: ");
Position = scan.next();
scan.nextLine();
System.out
.println("Type 'yes' or 'no' if you want to continure or not ");
input = scan.nextLine();
if ("yes".equals(input))
repeat = true;
else
repeat = false;
Staff tmpStaff = new Staff(ID, Name, LastName, FatherName, Salary,Position);
staffList.add(tmpStaff);
}
System.out.println("Thank you!");
for (Staff tmpStaff : staffList) {
System.out.println("Id = " + tmpStaff.getId());
System.out.println("Name = " + tmpStaff.getName());
System.out.println("LastName = " + tmpStaff.getLastName());
System.out.println("FatherName = " + tmpStaff.getFatherName());
System.out.println("Salary = " + tmpStaff.getSalary());
System.out.println("Position = " + tmpStaff.getPosition());
}
}
}</staff></staff>
这篇关于如何在java中解决NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!