这里写自定义目录标题
刚开始学java,对着视频项目一个个代码码下来,结果运行报错,
Exception in thread “main” java.lang.NullPointerException
at com.company.ui.CustomerView.listAllCustomers(CustomerView.java:98)
at com.company.ui.CustomerView.enterMainMenu(CustomerView.java:42)
at com.company.ui.CustomerView.main(CustomerView.java:107)
Process finished with exit code 1
对着原项目文本写下来还是报错,怎么都解决不了,实在不得已来寻求各位大神救命!
以下是代码
package com.company.ui;
import com.company.service.Customerlist;
import com.company.util.CMUtility;
import com.company.bean.Customer;
import java.security.PublicKey;
public class CustomerView {
public Customerlist customerlist = new Customerlist(10);
public CustomerView(){
Customer customer=new Customer("ww", '男',36,"121313321","2@qq.com");
customerlist.addCustomer(customer );}
public void enterMainMenu(){
boolean Flag=true;
while (Flag){
System.out.println("\n------------------------------客户信息管理软件------------------------------");
System.out.println("\n------------------------------1 添 加 客 户");
System.out.println("\n------------------------------2 修 改 客 户");
System.out.println("\n------------------------------3 删 除 客 户");
System.out.println("\n------------------------------4 客 户 列 表");
System.out.println("\n------------------------------5 退 出");
System.out.println("\n------------------------------请选择(1-5):_\n");
char menu =CMUtility.readMenuSelection();
switch (menu) {
case '1':
System.out.println("添加客户:");
addNewCustomer();
break;
case '2':
System.out.println("修改客户:");
modifyCustomer();
break;
case '3':
System.out.println("删除客户:");
deleteCustomer();
break;
case '4':
System.out.println("查看客户信息:");
listAllCustomers();
break;
case '5':
System.out.print("是否确认退出?输入(Y/N)");
char YN=CMUtility.readConfirmSelection();
if(YN=='Y'){
Flag=false;
}
}
}
}
private void addNewCustomer(){
System.out.println("\n------------------------------添加客户信息-----------------------------");
System.out.print("姓名:");
String name=CMUtility.readString(4);
System.out.print("性别:");
char gender=CMUtility.readChar();
System.out.print("年龄:");
int age=CMUtility.readInt();
System.out.print("电话:");
String phone=CMUtility.readString(13);
System.out.print("邮箱:");
String email=CMUtility.readString(30);
Customer customer=new Customer(name,gender,age,phone,email);
boolean success;
success = customerlist.addCustomer(customer);
if(success){
System.out.print("\n------------------------------添加客户成功-----------------------------");
}else System.out.print("客户目录已满,添加失败");
}
private void modifyCustomer(){
System.out.println("修改客户信息");
}
private void deleteCustomer(){
System.out.println("删除客户信息");
}
private void listAllCustomers() {
System.out.println("\n------------------------------客 户 列 表-----------------------------" );
int total=customerlist.getTotal();
if (total==0){
System.out.println("没有客户信息");
}else{
System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
Customer[] custs=customerlist.getAllCustomers();
for (int i=0;i<custs.length;i++){
Customer cust=custs[i];
System.out.println((i+1)+"\t"+cust.getName()+"\t"+cust.getgender()+"\t"+
cust.getage()+"\t"+cust.getphone()+"\t"+cust.getemail());}
}
System.out.println("\n------------------------------客户列表完成-----------------------------" );
}
public static void main(String[] args) {
CustomerView view =new CustomerView();
view.enterMainMenu();
}
}
现在暂时是写了两段代码,添加客户后在列表客户,但是添加后在列表就失败报错。