Results 1 to 6 of 6
  1. #1
    MadJack is offline Member
    Join Date
    Sep 2010
    Location
    Qc
    Posts
    31
    Rep Power
    0

    Default non-static variable cannot be referenced from static context...

    I know what it means and how to fix it, but I don't know how to properly do it I think...

    You see, there's a bunch of stuff I don't have direct access to in jMonkeyEngine and because of that I can't use them the way I want. It's something I have to live with.

    But, it's annoying me. There has to be a better way to access those variables than making gazillion methods and twisted ways to reference a "dummy" variable that I'll be able to use as static (because I'm controlling it).

    As an example.

    My main class is Game. It extends SimpleApplication. That class, SimpleApplication, has all the stuff I need to load textures, files, models, etc. That thing is called the assetManager. It's declared that way in the source file:

    Java Code:
    protected AssetManager assetManager;
    Since I can't access that as a static, I have devised a way to "get it" by doing something like the following in my Game class:

    Java Code:
    AssetManager gameAssetManager;
    Then later, to be sure it has been properly initialized, I call:
    Java Code:
    gameAssetManager = assetManager;
    It works, but that worries me a lot. I'm not sure things are done right since I've been having weird problems lately and I think it might be because of that. I'm unsure as of now if it is the problem, but I'd rather have better code now than wait longer and have even more spaghetti-looking code. (My Game class is now at close to 500 lines and I'm very far from being at the middle of things). With like 30 or so classes, I'm worried that I'm doing a really bad job.

    So to clean things up a bit I need to fix things up.

    I tried referencing my Game class in other classes, but I get the same message (static method/var). Or use the following:

    Java Code:
    protected Game game = new Game();
    but I'm getting nulls all over the place understandingly. At first I thought it would work nicely since all my variables are static, but since those vars in SimpleApplication are not, those get null'ed...

    I guess I could pass the Game class when initializing those other classes, but ... ugh.

    So... what are my options? :(

    And again, thanks a lot for your help.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    static variables should not be used lightly. Even if you only create a single instance of a class, you should still not use static variables/methods. In fact, the most common use of static is for constants, defaults, and factory/library methods - I've never yet managed to find a use of it that's not in some way connected to those three things. (Library methods being things like the methods in the Math class)

    I'd suggest changing to use instance variables. Your main method could, in theory, contain one line if command line arguments aren't used, that line being "new Game();".

    If you only want one instance of the Game class, make it a singleton class. Then have your other classes access it through the get method (for lack of a better term) described in the link.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    MadJack is offline Member
    Join Date
    Sep 2010
    Location
    Qc
    Posts
    31
    Rep Power
    0

    Default

    Ah thanks a lot for that singleton link!

    *runs to implement it*

    Honestly I have no idea if that will help, but I've been wondering about that for some time. Glad to have the answer. :D

  4. #4
    MadJack is offline Member
    Join Date
    Sep 2010
    Location
    Qc
    Posts
    31
    Rep Power
    0

    Default

    That didn't work at all. The singleton.

    I'm getting a StackOverflowError at startup.

    Java Code:
    Exception in thread "main" java.lang.StackOverflowError
            at java.util.concurrent.ConcurrentLinkedQueue$Node.<init>(ConcurrentLinkedQueue.java:138)
            at java.util.concurrent.ConcurrentLinkedQueue.<init>(ConcurrentLinkedQueue.java:193)
            at com.jme3.app.Application.<init>(Application.java:96)
            at com.jme3.app.SimpleApplication.<init>(SimpleApplication.java:106)
            at stellarconquest.Game.<init>(Game.java:105)
            at stellarconquest.Game.getGame(Game.java:952)
            at gui.MenuLoader.<init>(MenuLoader.java:18)
            at stellarconquest.Game.<init>(Game.java:89)
            at stellarconquest.Game.getGame(Game.java:952)
            at gui.MenuLoader.<init>(MenuLoader.java:18)
            at stellarconquest.Game.<init>(Game.java:89)
    The last 3 lines repeat for ~1000 lines.

    So much for that. *sigh*

    Even with the removal of synchronized I didn't get any change. Back to square one I guess.


    EDIT: Nevermind. That was a dumb mistake. Still in business.
    Last edited by MadJack; 12-01-2010 at 07:22 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The StackOverflowError comes about because getGame() at Game.java.952 calls Menuloader.java line 18 which in turn calls Game.java line 89. At this point the wheels start to spin because Game.java line 89 calls getGame() and the cycle just continues.

    Without seeing any code - and with line numbers like 952 floating about there's too much to post - it's hard to suggest anything but repeat the advice that was given before: remove everything static except the static main() method, and try and make that one line long.

    Beyond that, you have to stare at those three lines and see whether it makes sense for them to call one another in an endless loop like that.

  6. #6
    MadJack is offline Member
    Join Date
    Sep 2010
    Location
    Qc
    Posts
    31
    Rep Power
    0

    Default

    Static stuff has been removed, don't worry about that.

    As for the error, it's fixed already. It was a dumb mistake on my part like the EDIT line says. ;)

    Unfortunately, the original problem (why I was doing the whole clean up in the first place) is still present. But that's ok. I didn't expect that to fix it. But still, it would've been nice.

    Anyway, plowing on.

    Thanks anyway! :)

Similar Threads

  1. Replies: 5
    Last Post: 10-15-2010, 05:21 AM
  2. Replies: 3
    Last Post: 02-11-2010, 10:59 AM
  3. Replies: 3
    Last Post: 03-20-2009, 01:35 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •