Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Null pointer on getNpcs

Featured Replies

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?

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 by Slut

  • Author

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

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.

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...

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

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.