ryank645 Posted November 21, 2018 Share Posted November 21, 2018 (edited) I am experiencing an issue when trying to implement a boolean from class B to Class A. instead of validating and returning the truth value. a NPE is outputted in the console window. I have Initialised class B in class A: WalkingHandler w = new WalkingHandler(); and have attempted to implement like so: if (m.players.closest(p -> p != null && !p.equals(m.myPlayer()) && (m.myPosition().getY() > 3522) && p.getPosition().distance(m.myPosition()) < 20) == null || m.settings.getRunEnergy() > 20) { w.Walk(WildyPath[i]); } This is my class B: import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.utility.Condition; public class WalkingHandler{ main m = new main(); boolean loggedIn = m.getClient().isLoggedIn(); boolean inDanger = m.players.closest(p -> p != null && !p.equals(m.myPlayer()) && (m.myPosition().getY() > 3522) && p.getPosition().distance(m.myPosition()) < 20) != null; boolean lowStamina = m.settings.getRunEnergy() < 20 && m.getInventory().contains(item -> item.getName().contains("Stamina") && item != null); public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!loggedIn || inDanger || lowStamina); } }); m.execute(WalkthePath); } } and this is the error I am receiving in the log output: Error in script onStart(): MyScript Build java.lang.NullPointerException at WalkingHandler.<init>(WalkingHandler.java:12) at WalkToDestination.<init>(WalkToDestination.java:35) at main.onStart(main.java:47) at org.osbot.rs07.event.ScriptExecutor.iIIiiiiiiIII(zf:209) at org.osbot.rs07.event.ScriptExecutor.start(zf:11) at org.osbot.db.iIIiiiiiiIII(pab:171) at org.osbot.s.iIiIIiiiIiiI(ex:1) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Any help would be greatly appreciated. Edited November 21, 2018 by ryank645 Quote Link to comment Share on other sites More sharing options...
jca Posted November 21, 2018 Share Posted November 21, 2018 Declare the booleans inside the walk() method Quote Link to comment Share on other sites More sharing options...
Ragnar Lothbrok Posted November 21, 2018 Share Posted November 21, 2018 (edited) I would change those 3 booleans to functions as once they're assigned in the current state they wont update. Edited November 21, 2018 by Ragnar Lothbrok Quote Link to comment Share on other sites More sharing options...
ryank645 Posted November 21, 2018 Author Share Posted November 21, 2018 @jca after adding the boolean values to the walk() method, the script will no longer crash on start, however once the script reaches the point when the walk method is called, it again produces the NPE on the first boolean in the list. @Ragnar Lothbrok like this for example? boolean loggedIn(){ return m.getClient().isLoggedIn(); } Quote Link to comment Share on other sites More sharing options...
jca Posted November 21, 2018 Share Posted November 21, 2018 8 minutes ago, ryank645 said: @jca after adding the boolean values to the walk() method, the script will no longer crash on start, however once the script reaches the point when the walk method is called, it again produces the NPE on the first boolean in the list. @Ragnar Lothbrok like this for example? boolean loggedIn(){ return m.getClient().isLoggedIn(); } Probably caused by the getClient() Does Main extend Script? If so instead of doing main = new Main(), you should pass getBot().getMethods() to a MethodProvider const in the constructor of your WalkingHandler class and then call getClient() on that Quote Link to comment Share on other sites More sharing options...
Ragnar Lothbrok Posted November 21, 2018 Share Posted November 21, 2018 (edited) 19 minutes ago, ryank645 said: @jca after adding the boolean values to the walk() method, the script will no longer crash on start, however once the script reaches the point when the walk method is called, it again produces the NPE on the first boolean in the list. @Ragnar Lothbrok like this for example? boolean loggedIn(){ return m.getClient().isLoggedIn(); } import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.utility.Condition; public class WalkingHandler{ private main m = new main(); public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!isLoggedIn() || isInDanger() || isLowStamina()); } }); m.execute(WalkthePath); } private boolean isLoggedIn() { return m.getClient().isLoggedIn(); } private boolean isInDanger() { return m.players.closest(p -> p != null && !p.equals(m.myPlayer()) && (m.myPosition().getY() > 3522) && p.getPosition().distance(m.myPosition()) < 20) != null; } private boolean isLowStamina() { m.settings.getRunEnergy() < 20 && m.getInventory().contains(item -> item.getName().contains("Stamina") && item != null); } } I'd advise changing this to extend MethodProvider rather than creating a new instance of the main class. Edited November 21, 2018 by Ragnar Lothbrok Quote Link to comment Share on other sites More sharing options...
ryank645 Posted November 21, 2018 Author Share Posted November 21, 2018 (edited) okay this is my WalkingHandler class now: import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.utility.Condition; public class WalkingHandler extends MethodProvider{ MethodProvider m; public WalkingHandler(MethodProvider m) {m.getBot().getMethods();} public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!isLoggedIn() || isInDanger() || isLowStamina()); } }); execute(WalkthePath); } private boolean isLoggedIn() { return getClient().isLoggedIn(); } private boolean isInDanger() { return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null; } private boolean isLowStamina() { return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null); } } Still no luck, have I maybe implemented the Constructor wrong? extending the class from MethodProvider still seams to produce the error. In the other class, the variable m is an instance of the main class. is this correct or should this be replaced? WalkingHandler w = new WalkingHandler(m); The npe now points to the execute line of the Walk() method in the WalkingHandler class also if that is any help. @jca @Ragnar Lothbrok Edited November 21, 2018 by ryank645 Quote Link to comment Share on other sites More sharing options...
jca Posted November 21, 2018 Share Posted November 21, 2018 (edited) 21 minutes ago, ryank645 said: okay this is my WalkingHandler class now: import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.utility.Condition; public class WalkingHandler extends MethodProvider{ MethodProvider m; public WalkingHandler(MethodProvider m) {m.getBot().getMethods();} public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!isLoggedIn() || isInDanger() || isLowStamina()); } }); execute(WalkthePath); } private boolean isLoggedIn() { return getClient().isLoggedIn(); } private boolean isInDanger() { return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null; } private boolean isLowStamina() { return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null); } } Still no luck, have I maybe implemented the Constructor wrong? extending the class from MethodProvider still seams to produce the error. In the other class, the variable m is an instance of the main class. is this correct or should this be replaced? WalkingHandler w = new WalkingHandler(m); The npe now points to the execute line of the Walk() method in the WalkingHandler class also if that is any help. @jca @Ragnar Lothbrok Almost... private final MethodProvider mp; public WalkingHandler(final MethodProvider mp){ this.mp = mp; } Doesn't need to extend MethodProvider and that'll give you a false result. Initialise from your onStart in Main new WalkingHandler(getBot().getMethods()) Then call methods like... mp.getClient().isLoggedIn() Also the boolean isLowStamina() I would change to isLowEnergy() getSettings().getRunEnergy() < 20 Then you can check for and interact with Stamina potion if the event stops. Edited November 21, 2018 by jca Quote Link to comment Share on other sites More sharing options...
Ragnar Lothbrok Posted November 21, 2018 Share Posted November 21, 2018 20 minutes ago, ryank645 said: okay this is my WalkingHandler class now: import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.utility.Condition; public class WalkingHandler extends MethodProvider{ MethodProvider m; public WalkingHandler(MethodProvider m) {m.getBot().getMethods();} public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!isLoggedIn() || isInDanger() || isLowStamina()); } }); execute(WalkthePath); } private boolean isLoggedIn() { return getClient().isLoggedIn(); } private boolean isInDanger() { return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null; } private boolean isLowStamina() { return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null); } } Still no luck, have I maybe implemented the Constructor wrong? extending the class from MethodProvider still seams to produce the error. In the other class, the variable m is an instance of the main class. is this correct or should this be replaced? WalkingHandler w = new WalkingHandler(m); The npe now points to the execute line of the Walk() method in the WalkingHandler class also if that is any help. @jca @Ragnar Lothbrok import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.utility.Condition; public class WalkingHandler extends MethodProvider { public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!isLoggedIn() || isInDanger() || isLowStamina()); } }); execute(WalkthePath); } private boolean isLoggedIn() { return getClient().isLoggedIn(); } private boolean isInDanger() { return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null; } private boolean isLowStamina() { return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null); } } Then: WalkngHandler w = new WalkingHandler(); w.exchangeContext(getBot()); Quote Link to comment Share on other sites More sharing options...
Duhstin Posted November 21, 2018 Share Posted November 21, 2018 ^^ What he said. Should work after. Quote Link to comment Share on other sites More sharing options...
jca Posted November 21, 2018 Share Posted November 21, 2018 2 minutes ago, Ragnar Lothbrok said: import org.osbot.rs07.api.map.Position; import org.osbot.rs07.event.WebWalkEvent; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.utility.Condition; public class WalkingHandler extends MethodProvider { public void Walk(Position NextStep){ WebWalkEvent WalkthePath = new WebWalkEvent(NextStep); WalkthePath.setBreakCondition(new Condition() { @Override public boolean evaluate() { return (!isLoggedIn() || isInDanger() || isLowStamina()); } }); execute(WalkthePath); } private boolean isLoggedIn() { return getClient().isLoggedIn(); } private boolean isInDanger() { return players.closest(p -> p != null && !p.equals(myPlayer()) && (myPosition().getY() > 3522) && p.getPosition().distance(myPosition()) < 20) != null; } private boolean isLowStamina() { return settings.getRunEnergy() < 20 && getInventory().contains(item -> item.getName().contains("Stamina") && item != null); } } Then: WalkngHandler w = new WalkingHandler(); w.exchangeContext(getBot()); Yeah... I prefer exchangeContext(getBot()); However it is deprecated and marked for internal use. If the devs decide to remove it the script will break. Quote Link to comment Share on other sites More sharing options...
ryank645 Posted November 21, 2018 Author Share Posted November 21, 2018 that seemed to do the trick! thanks for the fast replies and the help Quote Link to comment Share on other sites More sharing options...
liverare Posted November 21, 2018 Share Posted November 21, 2018 1 hour ago, jca said: Yeah... I prefer exchangeContext(getBot()); However it is deprecated and marked for internal use. If the devs decide to remove it the script will break. Don't worry about that warning. @Alek has said in the past it's used for internal use, so it's not likely to be removed and it's perfectly fine to use. Quote Link to comment Share on other sites More sharing options...
jca Posted November 21, 2018 Share Posted November 21, 2018 57 minutes ago, liverare said: Don't worry about that warning. @Alek has said in the past it's used for internal use, so it's not likely to be removed and it's perfectly fine to use. Yeah... I’ve heard lots of people saying different things about that. Anyhow, I was just making sure the OP knew that it was deprecated and what that means. Quote Link to comment Share on other sites More sharing options...