Two programs have been created to display the contents of the cart.
However, if you run it, it becomes NullpointerException.I would like you to tell me how to improve it.
I'm not confident about the array substitution.
(What has already been pointed out in the comment is that it was a mistake when posting.)
NullpointerException occurs because you have not assigned an array.
The normal array is
Name of data type [ ] array = new data type [array size];
Use as shown in .
For the posted sample, there is no right side, so null initializes and tries to use it, so an exception occurs.
in the constructor
I'll do it like this.
Because arrays cannot be scaled, you must be aware of their range when using them.
For example, if you set the size at 100, as in the previous example,
num becomes 100, you cannot add any more.
is incorrect (because
num is the next substitution position).
© 2022 OneMinuteCode. All rights reserved.