Jump to content

Null pointer on getNpcs


Recommended Posts

Posted

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?

Posted (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 by JPHamlett
Posted

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.

Posted

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.

  • 3 months later...
Posted (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 by braverebel
posting what is output

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...