JPHamlett Posted August 10, 2018 Share Posted August 10, 2018 I have the following code package Tasks; import org.osbot.rs07.api.Quests; import org.osbot.rs07.api.map.Area; import org.osbot.rs07.script.MethodProvider; public class GetMilk extends Task { Area area = new Area(x1, y1, x2, y2); public GetMilk(MethodProvider api) { super(api); } @Override public boolean activate() { return !api.getQuests().isComplete(Quests.Quest.COOKS_ASSISTANT) && api.getInventory().getItem("Bucket") != null; } @Override public void execute() { if (!area.contains(api.myPosition())) { api.getWalking().webWalk(area.getRandomPosition()); return; } api.getNpcs().closest("Dairy cow").interact(); } } The walking works, however my script starts spamming NPE when It gets to the Dairy Cow part. Any ideas why this is happening? Quote Link to comment Share on other sites More sharing options...
Alek Posted August 10, 2018 Share Posted August 10, 2018 There's no closest Dairy Cow. Quote Link to comment Share on other sites More sharing options...
Hel Posted August 10, 2018 Share Posted August 10, 2018 (edited) 17 hours ago, Alek said: There's no closest Dairy Cow. @JPHamlett if the cow doesn't exist it can't interact with it. Check to see if it exists like so. NPC closestCow = api.getNpcs().closest("Dairy cow"); if(closestCow != null){ closestCow.interact(); } Edited August 10, 2018 by Slut Quote Link to comment Share on other sites More sharing options...
JPHamlett Posted August 10, 2018 Author Share Posted August 10, 2018 (edited) Even though I can see it on the screen? I also tried the ID from the client and got the same result Edit: Interesting If I do api.log(api.getNpcs().filter(new IdFilter<>(2691)).get(0).getName()); I get this output [INFO][Bot #1][08/10 09:38:17 AM]: [INFO][Bot #1][08/10 09:38:18 AM]: [INFO][Bot #1][08/10 09:38:18 AM]: [INFO][Bot #1][08/10 09:38:20 AM]: [INFO][Bot #1][08/10 09:38:20 AM]: [INFO][Bot #1][08/10 09:38:21 AM]: [INFO][Bot #1][08/10 09:38:21 AM]: [INFO][Bot #1][08/10 09:38:23 AM]: [INFO][Bot #1][08/10 09:38:23 AM]: [INFO][Bot #1][08/10 09:38:24 AM]: [INFO][Bot #1][08/10 09:38:24 AM]: [INFO][Bot #1][08/10 09:38:26 AM]: [INFO][Bot #1][08/10 09:38:26 AM]: [INFO][Bot #1][08/10 09:38:27 AM]: [INFO][Bot #1][08/10 09:38:27 AM]: [INFO][Bot #1][08/10 09:38:29 AM]: [INFO][Bot #1][08/10 09:38:29 AM]: [INFO][Bot #1][08/10 09:38:30 AM]: [INFO][Bot #1][08/10 09:38:30 AM]: [INFO][Bot #1][08/10 09:38:32 AM]: [INFO][Bot #1][08/10 09:38:32 AM]: [INFO][Bot #1][08/10 09:38:33 AM]: [INFO][Bot #1][08/10 09:38:33 AM]: [INFO][Bot #1][08/10 09:38:35 AM]: [INFO][Bot #1][08/10 09:38:35 AM]: [INFO][Bot #1][08/10 09:38:36 AM]: [INFO][Bot #1][08/10 09:38:36 AM]: [INFO][Bot #1][08/10 09:38:38 AM]: [INFO][Bot #1][08/10 09:38:38 AM]: [INFO][Bot #1][08/10 09:38:39 AM]: [INFO][Bot #1][08/10 09:38:39 AM]: Edited August 10, 2018 by JPHamlett Quote Link to comment Share on other sites More sharing options...
Alek Posted August 10, 2018 Share Posted August 10, 2018 Don't use ids, they change all the time. Use the solution Slut posted. Also don't web walk to a random position (use the area itself), that method will fail at some point because a random position can be one that's not walkable. Also web walker makes decisions base off whether or not the destination is an area or position. Quote Link to comment Share on other sites More sharing options...
Lexhanatin Posted August 12, 2018 Share Posted August 12, 2018 Basically when you do... api.getNpcs().closest("Dairy cow").interact(); There is the potential for the script to break because the cow may not exist for a given moment. Instead you need to implement what Slut has said, a null check... NPC closestCow = api.getNpcs().closest("Dairy cow"); if (closestCow != null) { closestCow.interact(); } What this will do is check if the cow exists first. If it does, then it will interact with it. Without this your script will be stuck with an NPE if it detects the cow not existing for just a moment. By only interacting with the cow if it exists, you prevent the NPE from occurring, because you're only interacting with the cow if the null check passes. Without the null check you can potentially interact with a null object. Just because you see the cow doesn't mean the program sees it. The program may be firing the interaction before the cow exists. Hope this helps. Quote Link to comment Share on other sites More sharing options...
braverebel Posted November 15, 2018 Share Posted November 15, 2018 (edited) I know I am rezzing a dead thread, but I just ran into the same issue that the original poster showed, yet all responses missed. The dairy cow has a name (via getName()) of "", and all 4 of it's actions from getAction() is null. I was able to test this by going right next to the dairy cow and using the following code: List<NPC> all = getNpcs().getAll(); for (int i=0;i<all.size();i++){ clog("Npc " + i + ": " + all.get(i).getName() + ", actions : " + stringArrayToString(all.get(i).getActions()),2); } List<NPC> all = getNpcs().getAll(); for (int i=0;i<all.size();i++){ log("Npc " + i + ": " + all.get(i).getName() + ", actions : " + stringArrayToString(all.get(i).getActions())); } protected String stringArrayToString(String [] arr){ String str = ""; for (String a:arr){ str = str + a; } return str; } Forgot to post the output: [INFO][Bot #1][11/14 08:38:38 PM]: Npc 0: Goblin, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 1: Goblin, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 2: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 3: , actions : nullnullnullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 4: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 5: , actions : nullnullnullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 6: Gillie Groats, actions : Talk-tonullnullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 7: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 8: Cow calf, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 9: Cow calf, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 10: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 11: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 12: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 13: Cow calf, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 14: Cow, actions : nullAttacknullnullnull [INFO][Bot #1][11/14 08:38:38 PM]: Npc 15: Cow calf, actions : nullAttacknullnullnull Edited November 15, 2018 by braverebel posting what is output Quote Link to comment Share on other sites More sharing options...
FrostBug Posted November 15, 2018 Share Posted November 15, 2018 Dairy cows are objects, not NPCs Quote Link to comment Share on other sites More sharing options...