• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting Object of another class

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, that was it. Thanks for pointing that out for me!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is it static?
 
Leonard Connoway
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Everybody's invited. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic