Screen Controller problems calling functions

I feel awkward asking this but I’m having problems getting a screen controller to call a function outside of itself. Spoke to my programming tutor, about it and he gave me a solution, which gets rid of an error but it still cannot call a method in my Main. I 'm thinking this is my knowledge of programming letting me down, but if I don’t make mistakes and don’t ask I won’t learn.



Heres my code inside the screen controller. This function is called when a button is clicked.



public void dice()

{

app.getDice().nextDice();

then

((Main)app).getDice().nextDice();

}



First line is what i originally had, from looking at different examples, but i had an error with getDice() which was ‘cannot find symbol’

The second line is what my teacher did too get rid of the error, but nothing happens in the method I’m calling. nextDice() isn’t the problem (yet), as I have tested various things happening in getDice() and nothing happens.



The rest of my controller class is from the example on this page, https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_java_interaction



app is created in my Main class which extends SimpleAplication. app is passed to the controller in intialize() the same as in the example in the page linked.



So my question is, what’s wrong with this code? I know I’m missing something, just not sure what.



Any support appreciated.

Well, seems to me that you lack the basic knowledge of object oriented programming. You should make yourself familiar with java first, before you try to use such a powerfull engine like jME. otherwise you won’t understand many things and that can be very frustrating.


First line is what i originally had, from looking at different examples, but i had an error with getDice() which was ‘cannot find symbol’

'app' is defined as SimpleApplication. So it's obvoius that you CAN NOT access other methods then the methods defined in SimpleApplication and its supertypes. That is Basic OOP knowledge. What your teacher did was casting it to 'Main' since in your case app will indeed be an instance of 'Main'. You should really familiarise yourself with inheritance and other OOP principles.

The other thing is: have you veryfied that dice() is called at all? make a simple System.out.println("Dice called") at the start of the method and see if it's called (i doubt you are familiar with the usage of an debugger, are you?).

dice() is definitely called as nifty.gotoScreen(""); works.



debugger, not so much.



OOP and inheritance, yes, mostly.

Do you pass the controller to nifty when you call loadXml()?



My guess is that app is null and that something is swallowing the error.

@suttsc said:
dice() is definitely called as nifty.gotoScreen(""); works.


I'm a little bit confused as what the one has to do with the other? Are you saing you are calling nifty.gotoScreen(""); inside dice() and that is executed?

If so, then theres definietly something wrong with your dice. But without having further knowledge of what Main.getDice() does (and returns) and what nextDice() should do and what should happen then it is almost impossible to assist you further.

What does Main.getDice() return there? Is the return value as you expect it to be? if not, check your implementation of Main.getDice(). Is the return value correct, but it is not correctly displayed? Then check your displaying code.

well i got it to work



I have been looking at this guys source code (GitHub - WASDi/BigGameProject: Made with jMonkeyEngine) that he linked to in another forum topic 2 weeks ago, in his controller i just noticed that he had a constructor and didn’t have initialize() and used the line, nifty.registerScreenController(new ControllerGameScreen(this)); when setting up Nifty.



so now i have a constructor like such…



ControllerGameScreen(SimpleApplication app)

{

this.app=app;

}

@polygnome



when the button is pressed, in the xml, its told to run dice() in the screen controller, if dice() contains gotoScreen, the screen changes. getDice, was returning my Dice class, so it could run nextDice() in it.



thanks for all your guys help any way, appreciated.