-3

It first takes the name of the customer and after it takes the purchase amount of that customer up to ten customer. Then it prints out the name of the customer who purchases most. I have the following code:

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;

And this one is the Class:

public class Store {

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];
}

}

However, I get "java.lang.NullPointerException: null" error. Can you please help me? Thank you.

Edit: Here is screenshot from my debugger enter image description here

ufuk Yavuz
  • 21
  • 6
  • 2
    Do you know what a NPE is and how to handle it? Which line throws the exception? – Thomas May 11 '21 at 13:46
  • @Thomas If I'm not wrong "sales[counter]=saleAmount;" line throws the exception. First I enter the name for example "Jack" and then I enter the amount say "10" immediately after that it gives error. – ufuk Yavuz May 11 '21 at 13:48
  • Does the stacktrace mention that exact line? – Thomas May 11 '21 at 13:48
  • 2
    Hint: On every call to `addSale()` the `counter` is set to `0`. What consequences will this have? Sidenote: You should learn how to use a debugger to debug your programs. This is a skill that is best learned early on. – maloomeister May 11 '21 at 13:49
  • @maloomeister Oh, OK, I fixed that but I still get the error – ufuk Yavuz May 11 '21 at 13:50
  • 1
    And just another design issue: `index` is only used in `nameOfBestCustomer()` so it should be a local variable instead of an instance field. Additionally, why do you have those arrays in your first snippet? You're adding each sale to the `Store` instance so those could just be local variables within the loop. – Thomas May 11 '21 at 13:50
  • @ufukYavuz You should not simply remove that line, you still have to initialize the `counter`. E.g. in the `Store()` constructor. So remove the `counter = 0` in the `addSale()` and add it in the constructor. – maloomeister May 11 '21 at 13:51
  • @Thomas I didn't understand the last comment of yours. Its either because I'm new in java or I'm not native speaker. Edit: Not the last comment anymore. "Does the stacktrace mention that exact line? " This comment – ufuk Yavuz May 11 '21 at 13:52
  • What I mean is: define `index` in `nameOfBestCustomer()` - it shouldn't even be mentioned outside that method, and if you have something else that's called "index" it would be something different. – Thomas May 11 '21 at 13:53
  • Ok, on the stacktrace: please post the entire stacktrace/error message you get (and format it nicely). – Thomas May 11 '21 at 13:54
  • I think I do all the things you said but still get the error – ufuk Yavuz May 11 '21 at 14:01
  • @Thomas I have added a screenshot from my debugger. It sets "sales" and "customerNames" arrays to null I guess, but I don't know what to do with that – ufuk Yavuz May 11 '21 at 14:17

1 Answers1

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

This declares an entirely new variable named sales, assigns a new double array to it, and then tosses the local variable it into the bin immediately, as is the fate of all local variables once its scope ends (local vars are scoped to the nearest set of braces, so, after these 2 lines, a closing brace appears: That is where all local vars declared inside, such as your sales and customerNames in this code, poof out of existence). The arrays will be garbage collected eventually; nobody has any reference to them anymore.

This does absolutely nothing whatsoever to your field named sales.

What you presumably want is:

public Store()
{
    sales=new double[10];
    customer=new String[10];        
}
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72