Jump to content

calling methods between classes extending method provider


Theorems

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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