OCAJP 7
Julian West wrote:The question is, do you know why?
OCAJP 7
I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
Ganesh Patekar wrote:
I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
OCAJP 7
Ganesh Patekar wrote:
I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
OCAJP 7
Did you understand why that program gives runtime exception?Anda Cristea wrote:How can I modified my code to run withot exception?
Anda Cristea wrote:
Ganesh Patekar wrote:
I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
I run my code in Net Beans and I didn't see strings hi hi.
Text of exception was on the first line.
If I modified System.out.print to System.out.println I saw the string hi, hi.
Thank you.
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ganesh Patekar wrote:Each object of Chapter3_4 you create will have an instance field named m1 of type Chapter3_4 whose default value is null.
1. m2 refers to an object of Chapter3_4 who has m1 = null Or we can show m2.m1 = null ( Because while creating this object we passed nothing so no-argument constructor is invoked and m1 is not assigned any reference so It has default initial value null)
2. m3 refers to an object of Chapter3_4 who has m1 = m2 Or we can show m3.m1 = m2 ( Because while creating this object we passed m2 as parameter to parameterized constructor)
3. m3.go invokes go method on object of Chapter3_4 referred by m3 so you know this which prints hi
4. m4 = m3.m1 as we know m3.m1 ( Means m1 field of object referred by m3 ) refers to the object referred by m2 ( See step no 2 ) so now m4 and m2 refer to the same object of Chapter3_4
5. m4.go(); invokes go method on object of Chapter3_4 referred by m4 so you know this also, which prints hi
6. m5 = m2.m1; as we know m2.m1 = null (See step 1) so m5 is also assigned to null.
7. m5.go(); here we know m5 is null ( See step 6 ) and we are trying to invoke this instance method go(); on null which generates runtime exception NullPointerException
Now you know what you can do so NullPointerException will not occur.
OCAJP 7
Jeanne Boyarsky wrote:
Anda Cristea wrote:
Ganesh Patekar wrote:
I really didn't get you, do you mean to say the output given by book is wrong? If their ouput is hi hi followed by runtime exception ( NullPointerException ) then It is correct output.Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
I run my code in Net Beans and I didn't see strings hi hi.
Text of exception was on the first line.
If I modified System.out.print to System.out.println I saw the string hi, hi.
Thank you.
That's a side effect of Netbeans that it isn't outputting the statements fast enough.
You don't need to know this for the exam, but System.out and System.err are different streams. They get written out independently and order isn't guaranteed. That's why you aren't seeing it. The book isn't wrong. The program is printing hi twice even if you don't see it.
OCAJP 7
This output stream is used to display error messages. Read more here ---> PrintStream errAnda Cristea wrote:In this situation how is it correct to use System.Err?
Anda Cristea wrote:In the book response is : hi hi followed by runtime exception.
Wrong!!!
Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
Anda Cristea wrote:I run my code in Net Beans and I didn't see strings hi hi.
Don't get me started about those stupid light bulbs. |