Theorems Posted December 20, 2017 Posted December 20, 2017 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?
Chikan Posted December 20, 2017 Posted December 20, 2017 That shouldn't be throwing a null pointer, maybe its something in your bank state that isn't null checked?
Theorems Posted December 20, 2017 Author Posted December 20, 2017 (edited) 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, 2017 by Theorems
dreameo Posted December 20, 2017 Posted December 20, 2017 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.
Explv Posted December 20, 2017 Posted December 20, 2017 2 minutes ago, progamerz said: exchangeContext(getbot()) iirc This. After creating an instance of your class, you then need to call exchangeContext once.
Eagle Scripts Posted December 20, 2017 Posted December 20, 2017 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
Jammer Posted December 20, 2017 Posted December 20, 2017 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()); }
dreameo Posted December 20, 2017 Posted December 20, 2017 5 minutes ago, Eagle Scripts said: Progamez answer's probably what you mean Yea that might be his preferred method which is deprecated 1
Theorems Posted December 20, 2017 Author Posted December 20, 2017 Thanks, that worked. (although .exchangeContext() has line through it in eclipse because it's deprecated I guess).
Explv Posted December 20, 2017 Posted December 20, 2017 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.
progamerz Posted December 20, 2017 Posted December 20, 2017 23 minutes ago, dreameo said: Yea that might be his preferred method which is deprecated There is no other way lol
dreameo Posted December 20, 2017 Posted December 20, 2017 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.
Apaec Posted December 20, 2017 Posted December 20, 2017 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 1
Jammer Posted December 20, 2017 Posted December 20, 2017 (edited) @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, 2017 by Jammer