How do I change it to use one of the existing CartItem constructors?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
How do I change it to use one of the existing CartItem constructors?
Look at the code for the CartItem class and find its constructors. Then write the new statement to use one of them.
If you don't understand my answer, don't ignore it, ask a question.
Here is the code for the CartItem class. In the image, you can see the constructors. I have tried using them but it is not working. I think i am writing the statement wrong. How would I write a new statement to use one of them?
5.PNG
Please copy and post the code where you tried and the error messages you get.I have tried using them but it is not working.
If you don't understand my answer, don't ignore it, ask a question.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
product cannot be resolved to a variable
The constructor CartItem(String) is undefined
at Shopping.main(Shopping.java:59)
5.PNG
Copy the constructor definition
and the new statement you are trying to use and paste them one above the other. That will allow you to compare the required arguments with the ones you are using and see what is wrong. For example:public SomeClass(int anInt) { // constructor definition statement = new SomeClass("aString"); // invalid call - arg should be int not String = new SomeClass(123); // valid - arg is int
If you don't understand my answer, don't ignore it, ask a question.
So all three lines of that code should go in the highlighted section:
5.PNG
No. Those lines of code where an example, not part of your code.all three lines of that code should go in
Copy all the constructor definition statements
and the new statement you are trying to use
and paste them here in the thread, one above the other.
Please copy and paste the lines here, no images.
If you don't understand my answer, don't ignore it, ask a question.
Here is what I did for case 1 and this seemed to work:
item = new CartItem(product, quantity, price);
However for case 2, it gives me an error saying:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor CartItem(String) is undefined
at Shopping.main(Shopping.java:71)
Change case 2 so its new statement uses the same type of arguments as for the new statement in case 1.
If you don't understand my answer, don't ignore it, ask a question.
Ok I did that but here is what happens when I run the program and choose option 1:
Menu - Managing a List
1 Add an item to your cart
2 Remove an item from your cart
3 View the items in your cart
4 Exit and add up the total
5 Empty your cart
6 Exit
Select a menu option
1
Enter an item:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at CartItem.<init>(CartItem.java:16)
at Shopping.main(Shopping.java:64)
There is a variable with a null value on line 16. Look at line 16 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at CartItem.<init>(CartItem.java:16)
If you can not tell which variable it is, add a println just before line 16 and print out the values of all the variables on that line.
If you don't understand my answer, don't ignore it, ask a question.