Jump to content

calling methods between classes extending method provider


Recommended Posts

Posted

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.

Posted (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 by Theorems
Posted (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 by dreameo
Posted (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 by Theorems
Posted
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. 

Posted
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.

Posted

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;

       }

}

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...