• Post Reply Bookmark Topic Watch Topic
  • New Topic

can't call my object methods inside my object method  RSS feed

 
Rex David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[/code]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "it doesn't work"?  Does it throw an error? if so, post that exact error message here.  Does it do <something>, just not what you want? then tell us what it DOES do and what you EXPECT it to do.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It also may help if you showed us some of the code from the PersonList class.

Henry
 
Rex David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for these quick replies.  Please look at the comments in the code where I have mentioned the issue occurring. Here's the error: its null pointer in one area of the code but not in the other section, when i'm passing in the same object. attached is the PersonsList Code  and error:
before5

Code thats Breaking:






**************************************************
before5 >>> This works in the refill method but Lobby.pop() fails in the refill method but works in the run method.

Exception in thread "main" java.lang.NullPointerException
at PersonsList.pop(PersonsList.java:52)
at Elevator.refillElevator(Elevator.java:140)
at Elevator.run(Elevator.java:54)
at Elevator.<init>(Elevator.java:25)
at Elevator.main(Elevator.java:188)

******************************************************

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: What happens to the pop() method, if you don't call the printList() method?  ... you have a bit of data structure corruption happening
 
Rex David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha.. I dont see the error. What's happening , please explain. I'm new to this.
 
grace christopher
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it doesn't make sense to execute/retrieve something outside of a method/block/assignment. What do you expect the stray b.behavior() and b1.instanceVariable to do?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rex David wrote:Haha.. I dont see the error. What's happening , please explain. I'm new to this.


Take a look at your variables (size, top) prior to the printList() call. And then, take a look after the printList() call.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rex,

You're the second person to post a question related to an elevator simulation problem this weekend. From what the other poster said, I guess this is from some kind of online course you're taking. Would you mind posting the requirements as they were given to you so we can have a clearer understanding of what you're being asked to do? This business with elevator lists and lobby lists isn't exactly intuitive when you're thinking about elevators in general, although it makes a little more sense once you know that using a Stack data structure is a hard requirement for this exercise.

There are naming issues in the code that create a lot of cognitive dissonance for the reader, like when you name a stack as PersonList.  This is because there is also a well-known standard interface, java.util.List. It's like saying, "Ok, in this conversation, when I say 'green', I really mean 'blue'. So, why is the sky so green? The American flag is red, white, and green!"
 
Rex David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu

Thanks for replying to my thread. Yes, I am taking an online data structure course. However I won't be able to post the question and requirements here on a public forum just yet since that would be unfair for other students and evaluators. We have to give our best shot at this . I will be able to post the entire requirements later once we submit our work for evaluation next week.

@Grace- thanks for replying, can you please elaborate a bit if possible?

@Henry - I'll try that. Thanks.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rex David wrote:I will be able to post the entire requirements later once we submit our work for evaluation next week.

Ok fair enough.

I'd like to point some inconsistencies in your code:

Line 20 defines a generic method, push (E ...).  The parameter, however, is named Person.  This goes against a couple of Java conventions. First, parameters follow the naming convention for variables, which is that they should start with a lowercase letter, and the rest would be camel case if the name is made up of multiple words.  Names for classes and interfaces are capitalized. So, this name can be confused with the name of a class or interface. This results in the confusion on Line 22, which will make even seasoned Java programmers do a double take.

Next, between the peek and pop operations of a Stack, the pop() operation is usually the one that generates an exception when you attempt to invoke it when the stack is empty.  A peek() operation, being more tentative in nature, is the one you'd expect to be more forgiving.  Also, it's kind of silly to throw an exception, then turn around and catch it and then display a message on the console. That's a bit of a contortion.  Besides, since you caught the exception, you're still trying to return an invalid top on line 48, regardless.
 
Rex David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks Junilu for the review. I agree with you on all points -on the naming conventions and the try and catch method used..I did only to troubleshoot and I don't intend to add them in my finished code.

Going back to my question, why do you think it's working in one method and not in the other ?

I don't have access to my computer right now and haven't got a chance to check my variables like Henry suggested. However, I'd love to have bit more explanation on it .. appreciate your help!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most probably it's because your PersonList.printList() method violates the Principle of Least Astonishment / Surprise in that it has the unintended side-effect of changing the value of top.  One would naturally expect that a method called printList does NOT do anything that would change the state of the stack but if you study it carefully, it does.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There really is no point to these kinds of throw/catch acrobatics:


This goes beyond the bad practices that fall under the "don't use exceptions to control flow" guideline.

What you're doing here with the try-throw-catch is actually pointless and wasteful. This code is functionally equivalent:

One good practice to make your code cleaner is to refactor explicit formulae like "size == 0" by extracting them to a method that has a name that clearly reveals the intent, like so:

This allows you to make the code you wrote previously more expressive:

It may seem like a trivial change but the cognitive weight of having to constantly unconsciously parse the meaning of "size == 0" adds up and makes your code less enjoyable to read.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing about code like "(size == 0)" is that it violates the Law of Leaky Abstractions.  The abstract idea involved is that of the "emptiness" of the stack.  The leaked information is the fact that you're using a size variable to determine whether or not the stack is empty.  By hiding the formula inside the isEmpty() method, you make the abstraction more important than its implementation in the rest of the program.  Abstraction is usually a good thing when done prudently and in moderation.
 
Rex David
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu , I fixed my code and funny why I didn't realize its the printMethod that was messing everything up.. thanks everyone again for your help. Hope someday I can be of help to someone
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. One more thing: your implementation of pop() can produce memory leaks.  When you pop an item from the stack, you should also set its next field to null.  If you don't, then it's possible that People objects that you pop off the stack will never get garbage collected.

In this implementation, line 50 is unnecessary. You can delete this line and move the type declaration to the next line where you set temp = top. On line 52, you change top to reference the Person that the current top Person's next field references. However, you never really remove the Person you popped because it's still pointing to its next Person; it's basically still connected to the chain of Person objects.  You need to to set temp.next to null before returning it on line 54 to completely remove that Person from the linked chain.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:There really is no point to these kinds of throw/catch acrobatics . . .
Agree. There is another thing: what are you going to return if somebody tries peek() on an empty Stack? Are you going to return null? But what if null is a permissible value on the stack? The user cannot distinguish an empty stack (error) from a null on top of the stack (normal occurrence). I shall have to formulate another rule about Exceptions. They are like dogs; they can smell fear. If you let them think they are scared of you, they will bite you, so you must show Exceptions who is boss. If you have an empty stack, then simply throw the Exception.Note the important tag in the documentation comments. Users must know about that behaviour. Note no throws clause because the exception involved is unchecked.
You have now done the right thing. You have told the calling method that you cannot fulfil the request and it is for the calling method to sort out the problem. The calling method might be the best place for a try‑catch.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. and welcome to the Ranch, Grace Christopher
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!