aspose file tools*
The moose likes Beginning Java and the fly likes weird behaviour when passing objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "weird behaviour when passing objects" Watch "weird behaviour when passing objects" New topic
Author

weird behaviour when passing objects

Tay Thotheolh
Ranch Hand

Joined: Aug 07, 2008
Posts: 84


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

Joined: Oct 27, 2005
Posts: 19761
    
  20

Are you sure none of the other code resets your sql field to null? What is this Server class?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tay Thotheolh
Ranch Hand

Joined: Aug 07, 2008
Posts: 84
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19761
    
  20

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Tay Thotheolh
Ranch Hand

Joined: Aug 07, 2008
Posts: 84


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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 17, 2009
Posts: 274

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.


SCJP 6 || SCWCD 5
Tay Thotheolh
Ranch Hand

Joined: Aug 07, 2008
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: weird behaviour when passing objects