adc Posted June 16, 2014 Share Posted June 16, 2014 public void sleepFor(int min, int max) throws InterruptedException,NullPointerException { sA.sleep(MethodProvider.random(min, max)); } where sA is my main script instantiating a task (or action, node, or whatever you prefer to call it) that in turn instantiates and passes the sA parameter to the class that this method is in. Link to comment Share on other sites More sharing options...
Hayden Posted June 17, 2014 Share Posted June 17, 2014 lol. Link to comment Share on other sites More sharing options...
adc Posted June 17, 2014 Author Share Posted June 17, 2014 lol. Thanks, that's very useful information and really helps with the issue I posted in the issues section. Link to comment Share on other sites More sharing options...
NotoriousPP Posted June 17, 2014 Share Posted June 17, 2014 (edited) public void sleepFor(int min, int max) throws InterruptedException,NullPointerException { sA.sleep(MethodProvider.random(min, max)); } Not sure why you have this throwing a NullPointerException? Also sleep is a static method, so just call it like MethodProvider.sleep(int); Just do this instead of having it throw a InterruptedExeption; not sure the exact situation you need this for, but this should work for you: public void sleepFor(int min, int max) { try { MethodProvider.sleep(MethodProvider.random(min, max)); } catch (InterruptedException e) { //Catch method } } Edited June 17, 2014 by NotoriousPP Link to comment Share on other sites More sharing options...
Swizzbeat Posted June 17, 2014 Share Posted June 17, 2014 You probably never instantiated sA 1 Link to comment Share on other sites More sharing options...
Khaleesi Posted June 17, 2014 Share Posted June 17, 2014 Yes sA is probably just null ... Link to comment Share on other sites More sharing options...
adc Posted June 17, 2014 Author Share Posted June 17, 2014 You probably never instantiated sA Correct me if I'm wrong, but it's definitely instantiated here, isn't it? Which is why I'm so confused as to why it would be at the top of a stack trace for an NPE package utils; import org.osbot.script.MethodProvider; import org.osbot.script.Script; public class MiscMethods { private Script sA; public MiscMethods(Script sA) { this.sA = sA; } public void sleepFor(int min, int max) throws InterruptedException,NullPointerException { sA.sleep(MethodProvider.random(min, max)); } [...] Also, I've gone ahead and replaced every usage of my custom sleep method with sA.sleep(MethodHandler.random(a,b)); So everything works, but it would be cool if someone could show me why my method isn't working so that I don't have unnecessary spam Link to comment Share on other sites More sharing options...
Ericthecmh Posted June 19, 2014 Share Posted June 19, 2014 Correct me if I'm wrong, but it's definitely instantiated here, isn't it? Which is why I'm so confused as to why it would be at the top of a stack trace for an NPE package utils; import org.osbot.script.MethodProvider; import org.osbot.script.Script; public class MiscMethods { private Script sA; public MiscMethods(Script sA) { this.sA = sA; } public void sleepFor(int min, int max) throws InterruptedException,NullPointerException { sA.sleep(MethodProvider.random(min, max)); } [...] Also, I've gone ahead and replaced every usage of my custom sleep method with sA.sleep(MethodHandler.random(a,b)); So everything works, but it would be cool if someone could show me why my method isn't working so that I don't have unnecessary spam How did you instantiate MiscMethods? Link to comment Share on other sites More sharing options...
Joseph Posted June 19, 2014 Share Posted June 19, 2014 How did you instantiate MiscMethods? this is probably why you get a npe Link to comment Share on other sites More sharing options...
adc Posted June 19, 2014 Author Share Posted June 19, 2014 (edited) How did you instantiate MiscMethods? MiscMethods is instantiated in the exact same way in every class that I need to use any of the methods from it. For example: public class StopScript extends Task { private MiscMethods m = new MiscMethods(sA); public StopScript(Script sA) { super(sA); } public boolean execute() throws InterruptedException { if(m.isPoisoned()) { [...] } m.curePoison(); m.stopScript(); } And literally every other method in MiscMethods operates in the same fashion as sleepFor, only they don't throw any errors: public boolean isPoisoned() { return sA.client.getConfig(102) > 0; } public boolean curePoison() throws InterruptedException { if(isPoisoned()) { if (sA.client.getInventory().contains("Antipoison(1)")) { sA.client.getInventory().interactWithName("Antipoison(1)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } else if (sA.client.getInventory().contains("Antipoison(2)")) { sA.client.getInventory().interactWithName("Antipoison(2)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } else if (sA.client.getInventory().contains("Antipoison(3)")) { sA.client.getInventory().interactWithName("Antipoison(3)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } else if (sA.client.getInventory().contains("Antipoison(4)")) { sA.client.getInventory().interactWithName("Antipoison(4)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } } return false; } Edited June 19, 2014 by adc Link to comment Share on other sites More sharing options...
Ericthecmh Posted June 19, 2014 Share Posted June 19, 2014 MiscMethods is instantiated in the exact same way in every class that I need to use any of the methods from it. For example: public class StopScript extends Task { private MiscMethods m = new MiscMethods(sA); public StopScript(Script sA) { super(sA); } public boolean execute() throws InterruptedException { if(m.isPoisoned()) { [...] } m.curePoison(); m.stopScript(); } And literally every other method in MiscMethods operates in the same fashion as sleepFor, only they don't throw any errors: public boolean isPoisoned() { return sA.client.getConfig(102) > 0; } public boolean curePoison() throws InterruptedException { if(isPoisoned()) { if (sA.client.getInventory().contains("Antipoison(1)")) { sA.client.getInventory().interactWithName("Antipoison(1)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } else if (sA.client.getInventory().contains("Antipoison(2)")) { sA.client.getInventory().interactWithName("Antipoison(2)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } else if (sA.client.getInventory().contains("Antipoison(3)")) { sA.client.getInventory().interactWithName("Antipoison(3)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } else if (sA.client.getInventory().contains("Antipoison(4)")) { sA.client.getInventory().interactWithName("Antipoison(4)", "Drink"); sA.sleep(MethodProvider.random(300,500)); return true; } } return false; } I'm surprised that the other methods work. They should all be throwing NPE. Reason: public class StopScript extends Task { private MiscMethods m = new MiscMethods(sA); < -- you initialize m here public StopScript(Script sA) { super(sA); < -- you set sA here. } public boolean execute() throws InterruptedException { if(m.isPoisoned()) { [...] } m.curePoison(); m.stopScript(); } BUT, in Java, m = new MiscMethods(sA) would be run before the constructor is called. Therefore, you initialize MiscMethods with sA = null, and then the constructor sets sA. For example: public class Time { public long time1 = System.nanoTime(); public long time2; public Time(){ time2 = System.nanoTime(); } public void print(){ System.out.println(time1 < time2); } } This will print true, indicating that time1 is initialized before the constructor is even called. 1 Link to comment Share on other sites More sharing options...
adc Posted June 21, 2014 Author Share Posted June 21, 2014 I'm surprised that the other methods work. They should all be throwing NPE. Reason: public class StopScript extends Task { private MiscMethods m = new MiscMethods(sA); < -- you initialize m here public StopScript(Script sA) { super(sA); < -- you set sA here. } public boolean execute() throws InterruptedException { if(m.isPoisoned()) { [...] } m.curePoison(); m.stopScript(); } BUT, in Java, m = new MiscMethods(sA) would be run before the constructor is called. Therefore, you initialize MiscMethods with sA = null, and then the constructor sets sA. Having started with Actionscript 3 (based on Java), I had enough experience to know this-- until, like you, I was surprised to find that it actually worked when done in that order. However, because it did work I assumed it was fine. Thanks for the help Link to comment Share on other sites More sharing options...
Ericthecmh Posted June 21, 2014 Share Posted June 21, 2014 Having started with Actionscript 3 (based on Java), I had enough experience to know this-- until, like you, I was surprised to find that it actually worked when done in that order. However, because it did work I assumed it was fine. Thanks for the help Your welocme but I totally did not understand the rest of your response... Was that the problem or not lol Link to comment Share on other sites More sharing options...