Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

assignment

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got this problem in my study guide but don't follow it well:


the output is hi hi followed by an exception due to m5's instance variable? I don't get this dot operator very well
 
lowercase baba
Posts: 13053
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
post the exact text of the exception. Those words all MEAN something. Unless you tell us, we can only guess.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of things wrong with the code that you posted, so that it will not even compile. For example, there is no member variable m1 declared anywhere, yet you are using it in several places (lines 3, 8 and 9). There's also a semi-colon missing on line 7.

So, the code that you posted is not the same as what you are actually running.

Make sure that you post the actual code that you are compiling and running.
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again folks. Sorry about the previous mistake and I promise to be more careful with my coding. On that note let's try that question again shall we?




and so once again the output is hi hi followed by an exception. The book says the m2 object's m1 instance variable is never initialized so when m5 tries to use it a NullPointerException is thrown. Does that mean that m2 and m1 are referencing the same object? The third line under the class declaration has an initialization for m1 does it not? I'm also not sure about the dot operator. For example when it connects two references what does that mean exactly? Thanks
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have added some newlines (and a ;), so you can actually read your code.
And I thought, as I went through it, “why has he got an empty constructor? Does he really want m1 to be uninitialised?” I am going to tell you what I told somebody else yesterday:

You need to decide where you do and where you don’t want constructors.

I think you don’t want that empty constructor. You ought not to have m1 uninitialised, but that empty constructor allows that. Delete the empty constructor and force other code to use the other constructor (you might have to change calls to constructors elsewhere), then you won’t have that null any more.
 
Campbell Ritchie
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, maybe you do need the empty constructor after all. But that is a bit of bad design, where a class has a field of its own type (a self-referential class), which can become null unexpectedly. I presume your book (by the way: which book?) shows you that code as an example of what can go wrong.

You will have to get a pencil and paper and go through the code writing down what all the references point to. Remember a field starts off pointing to null by default. As you go through the code you will need to rub out the connections and replace them with new connections.
The dot operator (or more precisely the dot separator) separates an object/class/etc from one of its fields/constructors/methods/etc. The two belong together, so fooo.foo() means, “call the foo() method belonging to the object fooo.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added some lines of code to make you understand better

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic