Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • 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!!!
 
Ranch Hand
Posts: 91
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?
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • 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.
 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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.
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • 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.
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • 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: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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?
 
author & internet detective
Posts: 39396
763
Eclipse IDE VI Editor Java
  • 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: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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.
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • 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: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
 
Dana Ucaed
Ranch Hand
Posts: 451
6
Netbeans IDE Oracle Windows
  • 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: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!