Theorems Posted December 24, 2017 Share Posted December 24, 2017 I have just spent hours trying to figure out why I cannot call methods defined in one class which extends method provider in another class that extends method provider. Is that just something which cannot be done? I can use my methods from my classes which extend method provider in my main class because there I can use .exchangeContext(getBot()); but that only seems to exist for Script. If I call a method from a class extending method provider in another class extending method provider it gives me a nullpointer error (even if it is just a setter or getter)... I could just work around this in my program design but if there is a way to call methods across classes extending method provider I would much rather do that for the sake of having a structured program. Quote Link to comment Share on other sites More sharing options...
Juggles Posted December 24, 2017 Share Posted December 24, 2017 ClassName class = new Classname class.getMethod Quote Link to comment Share on other sites More sharing options...
Theorems Posted December 24, 2017 Author Share Posted December 24, 2017 1 minute ago, Juggles said: ClassName class = new Classname class.getMethod i know that . I am doing that in my main class and it works there, but if I try to do it in a different class which also extends methodProvider it gives a nullPointerException Quote Link to comment Share on other sites More sharing options...
Theorems Posted December 24, 2017 Author Share Posted December 24, 2017 (edited) In main method (works): VariableActions varAction = new VariableActions(); //declared at top of class varAction.exchangeContext(getBot()); //called in onStart() varAction.AFK(); // called where I want to use it In other method that also extends methodContext (nullpointer): VariableActions varAction = new VariableActions(); varAction.AFK(); Edited December 24, 2017 by Theorems Quote Link to comment Share on other sites More sharing options...
Juggles Posted December 24, 2017 Share Posted December 24, 2017 3 minutes ago, Theorems said: i know that . I am doing that in my main class and it works there, but if I try to do it in a different class which also extends methodProvider it gives a nullPointerException Do you mean you can't compile it because it gives you errors or osbot is giving you NPE's Quote Link to comment Share on other sites More sharing options...
Theorems Posted December 24, 2017 Author Share Posted December 24, 2017 2 minutes ago, Juggles said: Do you mean you can't compile it because it gives you errors or osbot is giving you NPE's It compiles fine, it's a runtime nullPointer error that only happens when I call a method in a class extending method provider from a class extending method provider. Quote Link to comment Share on other sites More sharing options...
dreameo Posted December 24, 2017 Share Posted December 24, 2017 (edited) 44 minutes ago, Theorems said: It compiles fine, it's a runtime nullPointer error that only happens when I call a method in a class extending method provider from a class extending method provider. A - Main Class B: Some Class Extends MethodProvidor C: Some Class Extends MethodProvider If creating an instance of C in B, then you have to call exchangeContext and pass the bot reference. If you do this, you have to make sure that class B is instantiated in class A doing the same thing, exchangeContext. That way, there is a complete reference. A -> B -> C Think I understood what you're asking lol Zzz, Think I know now what you mean 48 minutes ago, Theorems said: VariableActions varAction = new VariableActions(); varAction.AFK(); When you do this, the original one in Main has no correlation to this one. It's a separate instance and therefore, it has not been initialized. Edited December 24, 2017 by dreameo Quote Link to comment Share on other sites More sharing options...
Theorems Posted December 24, 2017 Author Share Posted December 24, 2017 (edited) 17 minutes ago, dreameo said: A - Main Class B: Some Class Extends MethodProvidor C: Some Class Extends MethodProvider If creating an instance of C in B, then you have to call exchangeContext and pass the bot reference. If you do this, you have to make sure that class B is instantiated in class A doing the same thing, exchangeContext. That way, there is a complete reference. A -> B -> C Think I understood what you're asking lol Zzz, Think I know now what you mean When you do this, the original one in Main has no correlation to this one. It's a separate instance and therefore, it has not been initialized. Yes you know what I mean, I should have done a better job explaining . So how would I then create that instance of 'B' in 'C' because unless I'm doing something wrong .exchangeContext() is only available if you are extending script. Edit: nvm I think I see what you mean, ty for explaining. Edited December 24, 2017 by Theorems Quote Link to comment Share on other sites More sharing options...
dreameo Posted December 24, 2017 Share Posted December 24, 2017 3 minutes ago, Theorems said: Yes you know what I mean, I should have done a better job explaining :). So how would I then create that instance of 'B' in 'C' because unless I'm doing something wrong .exchangeContext() is only available if you are extending script. exchangeContext is inherited from MethodProvider, so it would be available. Quote Link to comment Share on other sites More sharing options...
Theorems Posted December 24, 2017 Author Share Posted December 24, 2017 10 minutes ago, dreameo said: exchangeContext is inherited from MethodProvider, so it would be available. That is strange, it just says "return type for the method is missing" when I go to use it in any class other than my Main.java But I see it there in the api so I must just be doing something wrong...anyway I got it working now by moving some functionality to my main class and my updating variables to a class which doesn't have anything to do with method provider. Quote Link to comment Share on other sites More sharing options...
dreameo Posted December 24, 2017 Share Posted December 24, 2017 (edited) https://imgur.com/a/bWuKv As you can see, it's kind of ugly and a hassle Probably best to avoid this way tho Edited December 24, 2017 by dreameo 1 Quote Link to comment Share on other sites More sharing options...
Team Cape Posted December 25, 2017 Share Posted December 25, 2017 similar to what dreameo said but you can just call exchangeContext() in the constructor X extends Script { //your main class private Y y; @Override public void onStart() { y = new Y(this); } } Y extends MethodProvider { Y(MethodProvider m) { exchangeContext(m.getBot()); } @Override public int onLoop() { //do something return 0; } } 1 Quote Link to comment Share on other sites More sharing options...