如何修复 "java.lang.NullPointerException: null"?

How to fix "java.lang.NullPointerException: null"?

它首先取客户的姓名,然后取该客户的购买金额,最多十位客户。然后它打印出购买最多的客户的姓名。我有以下代码:

Store cashier=new Store();
                double[] purchase=new double[10]; 
                String[] customer=new String[10]; 
               
                for(int i=0;i<10;i++){
                customer[i]=JOptionPane.showInputDialog("Please enter the name of the customer");
                String purchaseString=JOptionPane.showInputDialog("Please enter the buying amount of the customer");
                purchase[i]=Double.parseDouble(purchaseString);
                cashier.addSale(customer[i],purchase[i]);
                }
                JOptionPane.showMessageDialog(null,"The best customer is"+cashier.nameOfBestCustomer());
                             
               
                break;

而这个是 Class:

public class 商店 {

private double[] sales;
private String[] customerNames;
private int counter;
private double maxsale;
private int index;

public Store()
{
    double[] sales=new double[10];
    String[] customerNames=new String[10];        
}

   public void addSale(String customerName, double saleAmount)
{
    counter=0;
    sales[counter]=saleAmount;
    customerNames[counter]=customerName;
    counter=counter+1;     
}

public String nameOfBestCustomer(){
    maxsale=sales[0];
    for(int i=0;i<10;i++){
    if(sales[i]>maxsale){
    maxsale=sales[i];
    index=i;
    }
    }
    return customerNames[index];
}

}

但是,我收到“java.lang.NullPointerException: null”错误。你能帮我么?谢谢。

编辑:这是我的调试器的屏幕截图

public Store()
{
   double[] sales=new double[10];
   String[] customerNames=new String[10];        
}

这声明了一个名为sales的全新变量,为其分配了一个新的双精度数组,然后立即将局部变量扔进垃圾箱,原样all 局部变量的命运一旦其作用域结束(局部变量的范围限定为最近的一组大括号,因此,在这两行之后,出现一个右括号:这就是所有局部变量的位置在里面声明,比如你在这段代码中的 salescustomerNames,噗噗不存在)。这些数组最终会被垃圾回收;没有人再提及它们。

这对您名为 sales 的字段完全没有任何作用。

您可能想要的是:

public Store()
{
    sales=new double[10];
    customer=new String[10];        
}