• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Object of another class  RSS feed

 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a somewhat simple game for a project, and I am trying to make one class for the actual game with all of the rules and gameplay code, and another class for the GUI.
The GUI is still going to need to access some of the methods of the Game class though, so I was trying to figure out a way that I could still call some of the methods, and I found something
online which I tried to implement.
Ok in the Game class, I construct a Game object called myGame and then do this

Which is supposed to create an instance of the GUI class. That part does work because when I set gameGui to visible, it appears properly.
In GUI class I have


This will compile, but if I try to run a method like getGame().getPoles() I will get a null pointer exception. Also, if I just try to print out
a String containing getGame(), it will print null. I am unsure of whether the getGame() method is not working, or if the whole thing is not working
and the Game is not being set properly. Any help is appreciated.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like the game member variable is null at the moment you call getGame(), maybe because you're calling it before you've called GUI.setGame, but without seeing the rest of the code it's really hard to tell why game is null. It's a matter of carefully checking where and when it gets set.
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that was it. Thanks for pointing that out for me!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it static?
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's static because you are setting the game for the GUI class and not a GUI object.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you setting it for the class and not for an object? What happens if you have two objects? Will they use the same value?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!