• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing an object from different classes

 
Daniel Gallant
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem. I'm writing a character creation program and I want to use a single object to represent the character. So far, so good. The problem I'm having is passing that object to and from different JDialogs. I have different JDialogs to print, save, load, select options, etc. Here is part of the CharacterRecord object:



I use the synchronized keyword with all the methods, and the volatile keyword with all the variables.

My problem is that when I call CharacterRecord ch = CharacterRecord.getCharacter(); in my JDialogs, they do not return the curent character, they always return a blank character (i.e. an un populated character record).

It's driving me crazy!!! I know it's something simple and stupid that I missed. Please help!

If you need any additional information, please let me know.

Thanks for your help!
 
pete reisinger
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
it returns empty character because you are calling static variable ch which is new CharecterRecord()
could you post more of your code? because I can see only this. I cannot see where you set the character record.
Then I don't understand why your method is synchronized - it doesn't access instance variables but static variable, but maybe I'm missing something.
 
Daniel Gallant
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me expand on my original question. Here is part of the class that displays the UI, handles user input, etc. I have methods that use the ch object, populate it based on user input, etc. On the main UI, the summary panel populates from the ch object correctly. Any use of the ch object within in the Character_FolioView class works, but any use outside of that class, like in any JDialog, seems to use a new or blank ch object.





At the beginning of the classes, I start with code like this:


And here is the populatePage method:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic