• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pag 213 Chapter 3 - question 4 - wrong answer - K&B OCA/OCP Java SE 7

 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the response:


In the book response is : hi hi followed by runtime exception.
Wrong!!!
 
Julian West
Ranch Hand
Posts: 89
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong how?  Have you run it?

I have and this is what I get:


The question is, do you know why?
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote:The question is, do you know why?

The response in the book is wrong. I completed the problem to errata of this book.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
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
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:
Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
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.


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.
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:
Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
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.


How can I modified my code to run withot exception?

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:How can I modified my code to run withot exception?
Did you understand why that program gives runtime exception?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:
Ganesh Patekar wrote:
Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
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.


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.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Perfect!!!
Correct explanation!!!
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Anda Cristea
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Anda Cristea wrote:
Ganesh Patekar wrote:
Anda Cristea wrote:The response in the book is wrong. I completed the problem to errata of this book.
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.


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.


In this situation how is it correct to use System.Err?

 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:In this situation how is it correct to use System.Err?
This output stream is used to display error messages. Read more here ---> PrintStream err
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

No, that's not wrong! The study guide is spot-on. And therefore this is not an errata item.

In this topic you'll find an excellent explanation (with additional code snippets) about exactly the same code snippet. Definitely worth reading!

Hope it helps!
Kind regards,
Roel

PS. As mentioned in another of your topics as well, you should not change the class name(s) when you post such a code snippet. And the reason why is pretty simple: using the class name of a code snippet ranchers can easily search (using the excellent search function) for topics about the same code snippet they have doubts/questions about. If you change the class name to something different, they might miss out on valuable information.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:I run my code in Net Beans and I didn't see strings hi hi.

That's another reason to quit using NetBeans (or any other IDE) during your preparation. In this topic you'll find a few other reasons about why you should not use an IDE during preparation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic