This is a question taken from a past Java exam paper, it would be very helpful if I knew the answer to this. Please if you could help me, I would be very grateful. Thank you in advance for you time and help!
The code excerpt below from a VendingMachine class includes a toString method which is intended to be used for debug purposes to show the current state of a VendingMachine object. It compiles correctly but the program contains a bug that resulted in a null pointer exception being thrown at runtime. What lines from the toString method below could possibly have raised an exception given only the information you can deduce from the code given? Suggest appropriate modifications to the code that fixes the problem. 1. public class VendingMachine 2. ( 3. private int total; 4. private Carton milk = new Carton(10); 5. private Carton sugar = new Carton(10); 6. private String drink = null; 7. private int price = 0; 8. 9. //further code from this class not shown here... 10. 11. public String toString() 12. { 13. String ret = "VendingMachine["; 14. 15. ret += "total: " + total; 16. ret += ", milk: " + milk.getUnits(); 17. ret += ", sugar: " + sugar.getUnits(); 18. ret += ", drink: "; 19. ret += "\"" + drink "\""; 20. ret += ", price: " + price; 21. ret += "]"; 22. return ret; 23. } 24. }
> The Null Pointer is probably being thrown by the line > >
> 18. ret += ", drink: ";
>
> > since the variable drink was initialised to null > >
> 6. private String drink = null;
>
I'm pretty sure that won't matter -- if a null value is appended to a StringBuffer*, then the String "null" appears instead. You would not get a NullPointerException unless you tried to call a method on the String whose value is null.
Nor would a NullPointerException be thrown by the absence of a concatenation (+) operator on line 19 (sorry, tomroberts). The code would not compile in the first place. NullPointerException can only happen at runtime.
What are the possible answers to this question? Perhaps the NullPointerException is thrown somewhere in the Carton constructor.
* concatenation uses StringBuffers behind the scenes, and the StringBuffer will see the "null" reference and call the append(Object) method, which will then call the static method String.valueOf(Object), which will then translate the null value into the string "null".
> What are the possible answers to this question? Perhaps > the NullPointerException is thrown somewhere in the Carton > constructor.
Err, I meant somewhere in the Carton class. It really looks like the NullPointerException is getting thrown by the call to getUnits() on the Carton instances. So we need to see the Carton class to really understand what's going on.
From what I remember of the certification exam, I suspect that the answer to this question is something like "E. The source of the NullPointerException cannot be determined from the code shown."
What's wrong with you guys... this code is not even compilable.... the statement in line 19 is a mess: "\"" + drink "\""; you need an extra +! If we consider a writing error, then the only exceptions can come from the lines 16, 17 in case getUnit returns a null Object. So the correct answer is something we don't know from this class.