我必须使用继承概念制作一个程序。我已经制作了基类和子类。但是当我输入第一个输入时,它在线程“main”java.lang.NullPointerException 中给出了异常。我该如何解决这个问题?
这些是我的代码。 我的主要
package labweek7;
import java.awt.Menu;
import java.util.Scanner;
import java.util.Vector;
public class Main {
Vector<String> menu =new Vector<>();
Scanner scan = new Scanner(System.in);
Employee emp;
EmployeeFullTime ft;
EmployeePartTime pt;
public Main() {
int choice = 0;
int pay;
int time;
int salary;
do{
System.out.println("ABC EMPLOYEE DATA");
System.out.println("=================");
System.out.println("1. Add Employee");
System.out.println("2. View Employee");
System.out.println("3. Resign");
System.out.println("4. Exit");
System.out.print("Choice: ");
choice = scan.nextInt();
scan.nextLine();
switch(choice){
case 1:
String name = "";
do{
System.out.print("Input employee name[must be more than 3 characters]: ");
name = scan.next();
}while(! (name.length()>=3));
emp.empName.add(name);
int age;
do{
System.out.print("Input employee age[>=17]: ");
age = scan.nextInt();
}while(!(age>=17));
emp.empAge.add(age);
String role = "";
do{
System.out.print("Input employee role[Assistant | Programmer](Case Sensitive): ");
role = scan.nextLine();
}while(!(role.equals("Assitant") || (role.equals("Programmer"))));
emp.empRole.add(role);
String type = "";
do{
System.out.print("Input employee type[PartTime | FullTime](Case Sensitive): ");
type = scan.nextLine();
}while(!(type.equals("PartTime")|| type.equals("FullTime")));
emp.empType.add(type);
if(type.equals("PartTime")){
emp = new EmployeePartTime(name, age, role, type);
do{
System.out.print("Input pay per hour[>=10000]: ");
pay = scan.nextInt();
}while(!(pay>=10000));
pt.pay.add(pay);
do{
System.out.print("Input working hour per week[>0]: ");
time = scan.nextInt();
}while(!(time>0));
pt.hour.add(time);
}
else{
emp = new EmployeeFullTime(name, age, role, type);
do{
System.out.println("Input base salary[>=10000]: ");
salary = scan.nextInt();
}while(!(salary>=10000));
ft.salary.add(salary);
}
String status = "active";
break;
}
}while(choice!=4);
}
void view(){
//for(int i=1, j=0; i<)
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
}
我的父类(super class)名为 Employee
package labweek7;
import java.util.Vector;
public abstract class Employee {
public String name;
public int age;
public String role;
public String type;
Vector<String> empName = new Vector<>();
Vector<Integer> empAge = new Vector<>();
Vector<String> empRole = new Vector<>();
Vector<String> empType = new Vector<>();
public Employee(String name, int age, String role, String type) {
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
this.role = role;
this.type = type;
}
}
我的 EmployeeFullTime(子类 1) 包labweek7;
import java.util.Vector;
public class EmployeeFullTime extends Employee{
Vector<Integer> salary = new Vector<>();
public EmployeeFullTime(String name, int age, String role, String type) {
super(name, age, role, type);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
和我的员工兼职(子类 2)
package labweek7;
import java.util.Vector;
public class EmployeePartTime extends Employee{
Vector<Integer> pay = new Vector<>();
Vector<Integer> hour = new Vector<>();
public EmployeePartTime(String name, int age, String role, String type) {
super(name, age, role, type);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
当我想输入员工姓名时,它在 emp.empName.add(name); 上给出了 NULLpointer 异常;线。任何帮助表示赞赏。谢谢。
最佳答案
您收到异常,因为 emp 为 null。
您需要首先在 main 方法中初始化您的 emp 对象:
Employee emp = new EmployeeFullTime();
要使上述语句生效,您的 EmployeeFullTime 和 Employee 类中应该有默认构造函数。
对其他对象 ft 和 pt 也执行相同的操作。
EmployeeFullTime ft = new EmployeeFullTime ();
EmployeePartTime pt = new EmployeePartTime();
关于java - 线程 "main"java.lang.NullPointerException 中出现异常以及如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60946217/