The Artima Developer Community
Sponsored Link

Java Answers Forum
Past Java Exam Question - help needed! please!

6 replies on 1 page. Most recent reply: Jul 13, 2003 5:37 PM by zenykx

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Sharon

Posts: 4
Nickname: jbeginner
Registered: Jul, 2003

Past Java Exam Question - help needed! please! Posted: Jul 7, 2003 2:53 PM
Reply to this message Reply
Advertisement
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. }


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Past Java Exam Question - help needed! please! Posted: Jul 9, 2003 4:11 AM
Reply to this message Reply
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;


To prevent this exception from occurring one possible solution would be to change line 6 to

6. private String drink = new String();


Adam

Tom Roberts

Posts: 3
Nickname: tomroberts
Registered: Jul, 2003

Re: Past Java Exam Question - help needed! please! Posted: Jul 9, 2003 4:13 AM
Reply to this message Reply
Check this statement : 19. ret += "\"" + drink "\"";

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Past Java Exam Question - help needed! please! Posted: Jul 9, 2003 4:21 AM
Reply to this message Reply
Oops. Tom is right of course.

Line 19 and not 18 was the problem. But my suggestion about line 6 still stands - for the moment. ;)

Adam

Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: Past Java Exam Question - help needed! please! Posted: Jul 9, 2003 9:03 AM
Reply to this message Reply
> 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".

Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: Past Java Exam Question - help needed! please! Posted: Jul 9, 2003 9:08 AM
Reply to this message Reply
> 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."

zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: Past Java Exam Question - help needed! please! Posted: Jul 13, 2003 5:37 PM
Reply to this message Reply
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.

Flat View: This topic has 6 replies on 1 page
Topic: Tomcat Server Startup Problem.. Pls Help Previous Topic   Next Topic Topic: displaying arrays.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use