December 20, 20178 yr I'm new to java & scripting and I'm gonna have to ask stuff here until I get verified on discord I have a class with this method: public class ActionStates extends MethodProvider { public State getState() { if(getInventory().isFull()) { // nullPointerException return State.BANK; }else if(myPlayer().isAnimating()) { return State.IDLE; }else { return State.FISH; } } As you can see, I commented the line that throws the nullPointerException The thing is, that code works when I put it in my main class which extends from Script... Is there something else I need to be able to do in order to use osbot methods in other classes? It looks like it works in eclipse but then I get that when it runs which doesn't make sense to me because getInventory().isFull() should only be returning true or false (not null) shouldn't it?
December 20, 20178 yr That shouldn't be throwing a null pointer, maybe its something in your bank state that isn't null checked?
December 20, 20178 yr Author 6 minutes ago, Chikan said: That shouldn't be throwing a null pointer, maybe its something in your bank state that isn't null checked? well that code works in my 'Main' class so I don't think that would be causing any issue. I'm just building the skeleton of my script right now so my action methods just look like this: public void bank() { log("bank"); } public void idle() { log("idle"); } public void fish() { log("fish"); } Edited December 20, 20178 yr by Theorems
December 20, 20178 yr When you're extending MethodProvider, some inherited fields must be initialized. That is the only reason why you're getting null. What those fields are, i'm not sure (take a look in Script class and see how it handles it). There are different ways on dealing this situation. @Alek Can tell you the preferred way. I think he mentioned it before in the forums.
December 20, 20178 yr 2 minutes ago, progamerz said: exchangeContext(getbot()) iirc This. After creating an instance of your class, you then need to call exchangeContext once.
December 20, 20178 yr 1 minute ago, dreameo said: When you're extending MethodProvider, some inherited fields must be initialized. That is the only reason why you're getting null. What those fields are, i'm not sure (take a look in Script class and see how it handles it). There are different ways on dealing this situation. @Alek Can tell you the preferred way. I think he mentioned it before in the forums. Progamez answer's probably what you mean
December 20, 20178 yr Yh, make an instance of ActionStates in your main class. And exchangeContext in your onstart method. ActionStates ActionStates = new ActionStates(); public void onStart() { ActionStates.exchangeContext(getbot()); }
December 20, 20178 yr 5 minutes ago, Eagle Scripts said: Progamez answer's probably what you mean Yea that might be his preferred method which is deprecated
December 20, 20178 yr Author Thanks, that worked. (although .exchangeContext() has line through it in eclipse because it's deprecated I guess).
December 20, 20178 yr 7 minutes ago, Theorems said: Thanks, that worked. (although .exchangeContext() has line through it in eclipse because it's deprecated I guess). It's not really deprecated though, it's marked for internal use, nothing to stop you using it though.
December 20, 20178 yr 23 minutes ago, dreameo said: Yea that might be his preferred method which is deprecated There is no other way lol
December 20, 20178 yr 2 minutes ago, progamerz said: There is no other way lol You reference MethodProvider from 'Main class' that extends Script to all of your other classes.
December 20, 20178 yr Alternatively you could pass a reference to the MethodProvider instance via the class constructor, i.e: public class SomeClass extends SomeOtherClass implements SomeInterface { private final MethodProvider mp; public SomeClass (MethodProvider mp) { this.mp =mp; } //... } In your main class which extends Script: public final SomeClass sc = new SomeClass(this); -Apa
December 20, 20178 yr @Explv Btw guys. Suppose I have these three classes: Main (exchangesContext with BankTask), BankTask , Data (Data class contains methods which require api access) Let's say the BankTask needs to use some methods from the Data class. I realised that I need to call exchangeContext inside BankTask aswell but there's obviously no onStart method there. Should I let the Data class accept MethodProvider in the constructor instead or is it possible or even preferable to use exchangeContext in such cases? Edited December 20, 20178 yr by Jammer
Create an account or sign in to comment