I would suggest, inside the for loop with the call to showData()
, that you add the following statement:
if(cust[i]!=null)
This will make sure that it never tries to read data from a space in the array where there is no customer. (Null pointer exceptions are caused by the program trying to access objects that have no data.)
You could get around most of the problem by using an ArrayList<Customer>
instead of a Customer[]
. This way, you can have any arbitrary number of customers, and if you get to the end of the customers that you have added, it doesn’t try to look in places where there are no customers.
0
solved Why am I getting an error!?(Null pointer Exception) [closed]