Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending a Serializable object between two JFrames  RSS feed

 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I encountered a problem that gave me a headache because i just could not solve it .
I have two JFrames the first one is a log in screen where i log in to the log in server that i made using RMI .
If the log in succeeds i send the user object witch is Serializable to the second JFrame.
The user object is received from the server. This object contains all the information needed in the second JFrame.
This second JFrame is made inside the first one and i defined a method with witch i send this user object to the second frame.
Now the problem arises , while inside this method i am able to read all the data i need from the user object .
At the end of the method i save the reference of this object in a new User object defined in the second JFrame ,because i need it later on.
But soon as this method finishes , for some reason my reference to the object becomes null , and i cant use it later on.
Am i missing something ? Am i leaving something out ? Am i not aware of something i should be ?
I tried everything in my knowledge to solve it even read forums , but i could not find
the answer .
Thank you in advance , and i am sorry if my question is too long.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leon, welcome to the Ranch!

Your question isn't really too long; on the contrary, it is too short. For example you don't say whether these two JFrames are part of the same application, or even whether they are running on the same computer. And there isn't any code for us to look at. However your statements about references becoming null after a method finishes sounds, to me anyway, as if you have two variables with different scopes but the same name. My theory is that inside the method you assign a reference to a local variable, and then outside the method you expect an instance variable with the same name to hold that reference. Does that match anything in your code? If not, then don't hesitate to post back with some code which describes the problem.
 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helo Paul ,
It matches at some points , and i think you might be right , but still i tried a lot of things to fix it , but the end result was the same .
So i will post my code , because the problem lies there .
And to answer your question , yes the two frames are part of the same application , they run on the same computer .
When the log in is a success the first frame is disposed , because it is not needed further.
But before disposing i send the the User objects reference to the second one. To be used .
As i already said , the User object is received from the server.
Now for the code.

This is in the log in , when the user succeeds in logging in the reference to the object is sent to second frame.


Now in the second frame i have a local variable , just as you said , its called loggedUser, at the start its only a variable with null as its value.
This is the code , method i used to set the user in the second frame.



I even tried creating a new user in this method , reading all the information from the given user and creating a new one in the second frame ,
but the end result was the same . Maybe i was already too tiered , non the less i got really frustrated.
So i am really lost at this point . This is a project i have to do at my university and something that should have taken
10 minutes , gave me a headache. Everything depends on the user in the second frame.
The new RMI services that are needed in the second frame for communication between the users , the look of the frame , just everything.

Again thank you in advance , and i hope i gave a sufficient amount of information this time around.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the point of this code:



If you have a User object, then you create a new Lobby object, set its "user" attribute, and then (because the variable is local to that block) throw away your only reference to the Lobby object.
 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it dose seem pointless.
But the point is that i do not need the reference to the lobby because i have no need for it in the log in screen.
I could have done this instead , just that it was late already , so i apologies .



Again i am sorry , and again thank you in advance !!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the reference to the Lobby object? Then just don't create it. Your code should look like this:


 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul !

But then how should i get the user object in the second frame ?
As a said i need the object , then again as i continued to work on my project the same problem came up yet again, at other locations.
I cant seem to grasp the problem , or should i say the cause . I am sorry.



 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should get it out of that Lobby object. Or if you don't need the Lobby object, assign the User object to a variable which you do have access to instead.

In your original question it appeared that you had some variable which became null? I didn't see it in any of the code you posted, but if there is such a variable then why not just assign it a reference to your User object?
 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe i was not clear enough , so i apologies jet again , the Lobby object is the second JFrame .
And i did the exact same thing you wrote down . I used a variable namely loggedUser to save a reference to the object.
The problem is that it for some odd reason it dose not stay saved .
Should i post the code for both of the JFrames from the beginning to the end ?
That would probably make things easier.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leon Csergity wrote:Maybe i was not clear enough , so i apologies jet again , the Lobby object is the second JFrame .


Well, yes, you certainly didn't mention that. It would have been useful information.

So you're right, you don't need a new Lobby object. You need the one which has already been created. So don't create a new Lobby object in that code where we both agreed you didn't need one. Use the existing Lobby object.

I suppose your problem is now going to be that the code you posted doesn't have a reference to that Lobby object. If so, then you should fix that. Your controller (the code which creates both of the JFrames) should pass a reference to the Lobby object into the code you're asking about.
 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for the help !

I will try doing that , and i hope i succeed this time!
That problem is the only thing that is left to fix in my project .
 
Leon Csergity
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved , again thank you for your help Paul !!!

The problem was in front of my eyes . It was so close i could not see it .
There was no problem at the part witch i posted.
The problem was that i created two Lobby frames.
Now the first time the Lobby frame was generated using the WindowBuilder plugin. This plugin generated a code that it used to show the frame.
I used that code in initLobby. Not paying attention to that that i was duplicating code .
As a result i sent the user in to the first frame while showing the second .
When i found this out i could not stop my self from starting to laugh , and laugh and laugh at my self.

And now for the absolutely pointless code.


My question was out of place and order.
But i would like to thank you again for the help even if i asked the wrong question , questions . Thanks Paul !!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asking the wrong question is an absolutely normal thing for a programmer to do. Of course it doesn't help you to fix your problem, but we do it all the time. Typically it's because there are several possible causes of the problem and we choose the wrong one to focus on.

So in your case there was an RMI server, and an object which was Serializable. In your mind both of these could be contributing to the problem. One way to deal with that is to eliminate the possibilities. For example you could remove the code which connects to the server and replace it by a simple stub which just hard-codes what you would get from a successful login. And you could try making the User object not be Serializable while doing that, just temporarily. You would have found that neither of those things made any difference; so your problem still wouldn't be solved, but it would be simplified and you would have fewer things to look at.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!