mcleod63 2 Share Posted March 8, 2023 Can someone explain why I can't get Players.getLocal().getLevel(); to work in onStart(). It immediately throws an NPE error. I wrote a simple script, with lots of logs, to confirm it's not my node system: import org.dreambot.api.methods.Calculations; import org.dreambot.api.methods.container.impl.Inventory; import org.dreambot.api.methods.container.impl.bank.Bank; import org.dreambot.api.methods.interactive.Players; import org.dreambot.api.methods.tabs.Tab; import org.dreambot.api.methods.tabs.Tabs; import org.dreambot.api.script.AbstractScript; import org.dreambot.api.script.Category; import org.dreambot.api.script.ScriptManager; import org.dreambot.api.script.ScriptManifest; import org.dreambot.api.utilities.Sleep; @ScriptManifest(description = "Testing why combat level causing errors.", name = "TestDB", version = 1.0, author = "NAD", category = Category.MISC) public class Main extends AbstractScript { public static int combatLevel = 0; @Override public void onStart() { log("--- Script has started ---"); //combatLevel = Players.getLocal().getLevel(); // <--- throws errors log("#1 combat level: " + combatLevel); // was set to 0 when declared if (Players.getLocal() != null) { combatLevel = Players.getLocal().getLevel(); log("#2 combat level is now " + combatLevel); } else { log("#2 getLocal() is null, combat level is " + combatLevel); } if (combatLevel == 0) { // it shouldn't be but just checking combatLevel = 1; // must assign a value else crashes later in Fighter program log("#3 should never get here,combat level is " + combatLevel); } else { log("#3 combat level should have the correct value: " + combatLevel); } } @Override public int onLoop() { // A simple code to walk to the bank. if (Bank.open()) { log("Arrived at the bank."); if (!Tabs.isOpen(Tab.INVENTORY)) { if (Tabs.openWithMouse(Tab.INVENTORY)) { Sleep.sleepUntil(() -> Tabs.isOpen(Tab.INVENTORY), 3000); } } if (!Inventory.isEmpty()) { Bank.depositAllItems(); Sleep.sleepUntil(Inventory::isEmpty, 3000); } Sleep.sleep(250, 350); ScriptManager.getScriptManager().stop(); sleep(3000); // need a few seconds for stop() to kick in } else { log("Walking to the bank."); sleep(2500, 3500); } return Calculations.random(350, 500); } } And here's the error: Quote 2023-03-08 14:14:24 [INFO] Connecting to server... 2023-03-08 14:14:25 [INFO] Successfully downloaded the latest web nodes! 2023-03-08 14:14:26 [INFO] You have successfully been logged in as mcleod63! 2023-03-08 14:14:26 [INFO] Your current rank(s): [MEMBERS] 2023-03-08 14:14:26 [INFO] Successfully loaded 242 worlds! 2023-03-08 14:14:26 [INFO] Starting new instance... 2023-03-08 14:14:26 [INFO] Attempting to connect to OSRS... 2023-03-08 14:14:28 [INFO] Connection successful! 2023-03-08 14:14:28 [INFO] Attempting to start new applet... 2023-03-08 14:14:28 [INFO] Preparing gamepack for start... 2023-03-08 14:14:36 [INFO] Successfully started new applet! 2023-03-08 14:14:36 [INFO] Successfully started new instance! 2023-03-08 14:14:53 [INFO] DreamBot has started successfully! 2023-03-08 14:14:55 [INFO] Successfully loaded game cache! 2023-03-08 14:15:14 [INFO] Saving client settings... 2023-03-08 14:15:14 [INFO] Client settings saved successfully! 2023-03-08 14:15:14 [INFO] Successfully saved client settings! 2023-03-08 14:15:21 [INFO] Saving client settings... 2023-03-08 14:15:21 [INFO] Client settings saved successfully! 2023-03-08 14:15:21 [INFO] Successfully saved client settings! 2023-03-08 14:15:22 [INFO] Successfully refreshed scripts! 2023-03-08 14:15:30 [INFO] Now loading TestDB... 2023-03-08 14:15:30 [INFO] Saving client settings... 2023-03-08 14:15:30 [INFO] Client settings saved successfully! 2023-03-08 14:15:30 [INFO] Successfully saved client settings! 2023-03-08 14:15:30 [INFO] Starting TestDB now! 2023-03-08 14:15:30 [INFO] --- Script has started --- 2023-03-08 14:15:30 [INFO] #1 combat level: 0 2023-03-08 14:15:30 [ERROR] The script had a problem starting java.lang.NullPointerException at org.dreambot.api.wrappers.interactive.Player.getLevel(Player.java:43) at Main.onStart(Main.java:31) at org.dreambot.api.script.AbstractScript.run(AbstractScript.java:171) at java.base/java.lang.Thread.run(Thread.java:829) 2023-03-08 14:15:30 [INFO] Script set to running! 2023-03-08 14:15:30 [INFO] Starting random event solver Login Handler... 2023-03-08 14:15:30 [INFO] Starting auto-login handler... 2023-03-08 14:15:30 [INFO] Stopped TestDB! 2023-03-08 14:15:30 [INFO] Stopping script: TestDB 2023-03-08 14:15:31 [INFO] Successfully downloaded the latest web nodes! 2023-03-08 14:15:31 [WARN] TestDB had a problem starting. 2023-03-08 14:15:39 [INFO] Saving client settings... 2023-03-08 14:15:39 [INFO] Client settings saved successfully! 2023-03-08 14:15:39 [INFO] Successfully saved client settings! The NPE occurs at line 31 in onStart, where I do a null check and then try to set the combatLevel variable ?? I'm confused. Players.getLocal() is not null, and getLevel() is an int, so where is the null pointer exception coming from? More importantly, how do I fix it? Can it be that onStart runs before the client is fully up to speed? I did try insertiing a 2 minute sleep before setting the combat level, but that didn't fix it. Any assistance would be greatly appreciated. Link to comment Share on other sites More sharing options...
hashbang 8 Share Posted March 9, 2023 (edited) You're not logged in (I'm guessing). First you need to check if the Player instance is not null before fetching the combat level Player me = Players.getLocal(); if (me != null) { combatLevel = me.getLevel(); } // Wrap this in a loop to try fetch the combat level repeatedly until logged in. Edited March 12, 2023 by hashbang Link to comment Share on other sites More sharing options...
mcleod63 2 Author Share Posted March 9, 2023 Thanks hashbang. I tried that but Players.getLocal() isn't null, so I repeatedly got NPEs by executing the Players.getLocal().getLevel() line inside the loop. I imagine jagex will pick up on this quickly and ban me, if I can't solve my problem soon. I also tried a loop with Client.isLoggedIn but found that I was always logged in, so passed by the while loop, and still got the NPEs when I tried to set the combat level. int count = 0; while (!Client.isLoggedIn()) { sleep(10000); count++; if (count >= 12) stop(); } Does anyone have any other ideas I can try? I've exhausted my small bit of DB3 knowledge, and tried to find answers in both the forum and Javadocs but failed. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now