GeeOhB Posted October 9, 2018 Share Posted October 9, 2018 So my scripts lagging out with null errors when it gets to a certain part of the script. It's supposed to kill druids and loot them after they're dead. I can start it and it'll say "killing Druids" and it might kill one or it might run off and get loot from the ground off someone else's kill. That's fine. But once it picks it up it lags to holy fuck and that's when I get the errors. It's supposed to attack the Druids and sleep until they die (or I need to eat). Then loot. Here's the code snippet: Spoiler public void killDruid() { log("Killing Druids"); NPC chaosDruid = getNpcs().closest("Chaos Druid"); if (chaosDruid != null && chaosDruid.exists() && !myPlayer().isAnimating() && !myPlayer().isUnderAttack() && chaosDruid.isAttackable() ) { chaosDruid.interact("Attack"); new ConditionalSleep(2500, 500) { @Override public boolean condition() throws InterruptedException { return !chaosDruid.exists() || needToEat(); } }.sleep(); } List<String> lootItems = Arrays.asList(new String []{"Grimy tarromin" ,"Grimy harralander", "Grimy avantoe", "Grimy irit leaf", "Grimy kwuarm", "Grimy ranarr weed", "Grimy lantadyme", "Grimy dwarf weed", "Grimy cadantine", "Snape grass", "Law rune", "Nature rune", "Death rune", "Air rune", "Body rune", "Earth rune", "Mind rune", "Mithril bolts", "Ensouled chaos druid head", "Silver ore", "Tooth half", "Loop half", "Uncut sapphire", "Uncut emerald", "Uncut ruby", "Uncut diamond", "Uncut dragonstone", "Dragonstone", "Rune spear", "Rune battleaxe", "Rune 2h sword", "Rune kiteshield", "Rune square shield", "Runite bar", "Dragon spear", "Dragon med helm", "Shield left half" }); GroundItem item = getGroundItems().closest(n -> (n != null && n.exists() && lootItems.contains(n.getName()))); item.interact("take"); log("Attempting to loot " + item.getAmount() + " " + item.getName()); new ConditionalSleep(5000) { @Override public boolean condition() throws InterruptedException { return !item.exists(); } }.sleep(); } I get errors in the logger on line 27 [the main onLoop method killDruid()] and line 54, inside the killDruid() method [item.interact("take");]. Can anyone help me identify what's causing the script to lag? It still runs, it's just laggy as fuck and takes up 40% CPU and 2GB RAM and I can't stop it or click on it or scroll the logger. I'm pretty new to this so any help appreciated. I think I've got null checks in the right places, and I'm lost. Quote Link to comment Share on other sites More sharing options...
Ragnar Lothbrok Posted October 9, 2018 Share Posted October 9, 2018 (edited) GroundItem item = getGroundItems().closest(n -> (n != null && n.exists() && lootItems.contains(n.getName()))); //This can still return null item.interact("take"); Change that to: GroundItem item = getGroundItems().closest(n -> lootItems.contains(n.getName()); if (item != null) { item.interact("take"); } You're doing your null check inside your lambda expression however if that call returns null you're not doing a further null check before trying to interact. Edited October 9, 2018 by Ragnar Lothbrok Quote Link to comment Share on other sites More sharing options...
Explv Posted October 9, 2018 Share Posted October 9, 2018 (edited) 5 minutes ago, GeeOhB said: So my scripts lagging out with null errors when it gets to a certain part of the script. It's supposed to kill druids and loot them after they're dead. I can start it and it'll say "killing Druids" and it might kill one or it might run off and get loot from the ground off someone else's kill. That's fine. But once it picks it up it lags to holy fuck and that's when I get the errors. It's supposed to attack the Druids and sleep until they die (or I need to eat). Then loot. Here's the code snippet: Hide contents public void killDruid() { log("Killing Druids"); NPC chaosDruid = getNpcs().closest("Chaos Druid"); if (chaosDruid != null && chaosDruid.exists() && !myPlayer().isAnimating() && !myPlayer().isUnderAttack() && chaosDruid.isAttackable() ) { chaosDruid.interact("Attack"); new ConditionalSleep(2500, 500) { @Override public boolean condition() throws InterruptedException { return !chaosDruid.exists() || needToEat(); } }.sleep(); } List<String> lootItems = Arrays.asList(new String []{"Grimy tarromin" ,"Grimy harralander", "Grimy avantoe", "Grimy irit leaf", "Grimy kwuarm", "Grimy ranarr weed", "Grimy lantadyme", "Grimy dwarf weed", "Grimy cadantine", "Snape grass", "Law rune", "Nature rune", "Death rune", "Air rune", "Body rune", "Earth rune", "Mind rune", "Mithril bolts", "Ensouled chaos druid head", "Silver ore", "Tooth half", "Loop half", "Uncut sapphire", "Uncut emerald", "Uncut ruby", "Uncut diamond", "Uncut dragonstone", "Dragonstone", "Rune spear", "Rune battleaxe", "Rune 2h sword", "Rune kiteshield", "Rune square shield", "Runite bar", "Dragon spear", "Dragon med helm", "Shield left half" }); GroundItem item = getGroundItems().closest(n -> (n != null && n.exists() && lootItems.contains(n.getName()))); item.interact("take"); log("Attempting to loot " + item.getAmount() + " " + item.getName()); new ConditionalSleep(5000) { @Override public boolean condition() throws InterruptedException { return !item.exists(); } }.sleep(); } I get errors in the logger on line 27 [the main onLoop method killDruid()] and line 54, inside the killDruid() method [item.interact("take");]. Can anyone help me identify what's causing the script to lag? It still runs, it's just laggy as fuck and takes up 40% CPU and 2GB RAM and I can't stop it or click on it or scroll the logger. I'm pretty new to this so any help appreciated. I think I've got null checks in the right places, and I'm lost. Maybe you want to check that item != null before interacting with it? NullPointerException is quite clear, the object is null, and you're trying to do something with it. Also declare the List of loot items as a class variable, rather than re-creating the list every time killDruid is called. Edited October 9, 2018 by Explv Quote Link to comment Share on other sites More sharing options...
GeeOhB Posted October 9, 2018 Author Share Posted October 9, 2018 (edited) 37 minutes ago, Ragnar Lothbrok said: GroundItem item = getGroundItems().closest(n -> (n != null && n.exists() && lootItems.contains(n.getName()))); //This can still return null item.interact("take"); Change that to: GroundItem item = getGroundItems().closest(n -> lootItems.contains(n.getName()); if (item != null) { item.interact("take"); } You're doing your null check inside your lambda expression however if that call returns null you're not doing a further null check before trying to interact. Interestingly I had it written like that before but it was still lagging. I changed it to see if it would make any difference but it didn't Thanks for the reply though., I'll change it back. Quote Maybe you want to check that item != null before interacting with it? NullPointerException is quite clear, the object is null, and you're trying to do something with it. Am I not doing that though? I've got a null check before I interact with the item haven't I? Quote Also declare the List of loot items as a class variable, rather than re-creating the list every time killDruid is called. OK, so I put that list of items at the start of the script instead of inside the method, yeah? Edited October 9, 2018 by GeeOhB Quote Link to comment Share on other sites More sharing options...
Explv Posted October 9, 2018 Share Posted October 9, 2018 1 hour ago, GeeOhB said: Am I not doing that though? I've got a null check before I interact with the item haven't I? GroundItem item = getGroundItems().closest(n -> (n != null && n.exists() && lootItems.contains(n.getName()))); Means, find the closest ground item that isn't null, and exists, and has a name that is in my list of loot names. What happens if no item is found that matches those parameters? It will return null. So "item" will be null. So you need to check item != null before interacting Quote Link to comment Share on other sites More sharing options...
GeeOhB Posted October 9, 2018 Author Share Posted October 9, 2018 Ok cool, I've updated and it seems to be working better! Thanks guys! Quote Link to comment Share on other sites More sharing options...