• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

weird behaviour when passing objects

 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The error points that my sql object at doExit() method is null when I have apparently already had 'sql=new ...' done. I have tried to doExit(). I am curious if you pass the sql object to the Server for the starting of a server thread, did it somehow took away the sql in XXXServer class ?
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure none of the other code resets your sql field to null? What is this Server class?
 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The XXXServer is some sort like a general server and the Server inside it is a more specific server. I wish I could dump the entire codes here so you can read through it but I doubt if this thing is going to take in so much codes I wrote.

I have checked for anything resetting it and no...nothing is resetting it according to what I know.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Variables won't be randomly set to null--*something* is making it null, if that's actually what the error is. Are you *sure* that "sql" is null in the doExit() method? If it is, then *something* is setting it to null somewhere.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do a search for "sql = null;" in your code, I'm sure you'll find something. And if not, please show us the stack trace you get.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is not your real code, but a paraphrase, are you absolutely sure the constructor doesn't look like



That one word "SQLManager" on the first line makes all the difference: it makes that line declare a new, local variable also named "sql", leaving the member uninitialized. This is an extremely common mistake for beginners to make.
 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Let's say the SQLManager is already instantiated and created. I have another class that wishes to get the SQLManager from testServer so I getInstance to get the testServer instance then I do a getSQLManager, does it break anything ? Because this is apparently another case where passing objects can be weird.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm unclear regarding this line: Does that actually compile (after adding the missing semi-colon)?

That aside, there's absolutely nothing weird about returning an object as the result of a method call from another class. Without understanding more about the actual code and how it's being used it's really tough to provide any meaningful input.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tay Thotheolh wrote:Because this is apparently another case where passing objects can be weird.


A reference to the SQLManager object is returned by calling the getSQLManager() method. So, the calling code can do things to the SQLManager like calling close() without the testServer knowing about it--which can have unintended behavior.
 
Tay Thotheolh
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I managed to create an example. Let's say we have a Session here...



This session contains the session information of the program. I have another class called testSession. This is the main method class.



I would have two seperate smaller classes called testSessionA and testSessionB. I would explain their uses later.





What I want is that the main method class, testSession, to have a Session object. Then testSessionA tries to access testSession's Session object, update the MODE object in testSession to an int of 12345 and then testSessionB accessing testSession again, retrieving the Session object and attempt to print out the changed 12345 int from MODE object.

What I am getting now is the following StackOverFlowError...



How do I resolve it ? If this can be resolved, I think the other SQLManager thing (which have similar structures) would also be resolved.
 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I am getting now is the following StackOverFlowError...

How do I resolve it ? If this can be resolved, I think the other SQLManager thing (which have similar structures) would also be resolved.


Well, you do know why it is happening right?



Basically, the constructor of your TestSession class is creating a TestSessionA local variable, which in turn, the constructor of your TestSessionA class is creating a TestSession local variabla, which in turn, the constructor of your TestSession class is creating a TestSessionA local variable, which in turn, the constructor of your TestSessionA class is creating a TestSession local variabla, which in turn, the constructor of your TestSession class is creating a TestSessionA local variable, which in turn, the constructor of your TestSessionA class is creating a TestSession local variabla, which in turn, the constructor of your TestSession class is creating a TestSessionA local variable, which in turn, the constructor of your TestSessionA class is creating a TestSession local variabla, which in turn, the constructor of your TestSession class is creating a TestSessionA local variable, which in turn, the constructor of your TestSessionA class is creating a TestSession local variabla, which in turn, the constructor of your TestSession class is creating a TestSessionA local variable, which in turn, the constructor of your TestSessionA class is creating a TestSession local variabla, which in turn, etc. etc. etc. etc. etc. etc. etc. etc. etc.etc. etc. etc. etc.etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc.etc. etc. etc. etc.etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc.etc. etc. etc. etc.etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc.etc. etc. etc. etc.etc. etc. etc. etc.... until your stack overflows.

How do you resolve it, is simple -- don't do that.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic