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.

NPE when trying to access MethodProvider instance

Featured Replies

public class Main extends Script {
	private ArrayList<Task> tasks = new ArrayList<Task>();
	
	public static MethodProvider MP = new MethodProvider();
	
	@Override
	public void onStart() throws InterruptedException {
		Collections.addAll(tasks, new Bank(), new Teleport(), new BuyBoots());
	}

	@Override
	public int onLoop() throws InterruptedException {
		for (Task task : tasks) {
			if (task != null && task.validate()) {
				task.execute();
			}
		}
		return random(250, 750);
	}
}
public class BuyBoots implements Task {

	@Override
	public boolean validate() {
		return !Main.MP.getInventory().contains("Coins");
	}

	@Override
	public void execute() {
		Main.MP.log("Executing BuyBoots task.");
	}
}

This part is giving me an NPE:

return Main.MP.getInventory().contains("Coins");

I tried defining a MethodProvider instance in the BuyBoots class, tried using a getter method in the Main class instead of a static variable etc. but when I start the script this line gives me an NPE and the script stops responding. Can anyone help?

 

Try a private method provider type in your buyboots class, then in the onstart of main call the constructor for buyboots class, passing in (this) to assign to the private method provider type in the buyboots class

 

main class-

onstart {

buyboots = new buyboots(this)

}

//

boots class-

private methodprovider methods

public void buyboots (methodprovider mp) {

methods = mp

}

then u can do 

return methods.getInventory().contains("Coins");

Edited by Avrae

Avrae works but  this will fix your problem without change any of your old code. 

public static MethodProvider MP = this;//script extends the MethodProvider

You could also fix that by calling MP.exchangeContext(getBot()) in onStart()

39 minutes ago, Malcolm said:

Unless you are calling a method from your main class I would just extend MethodProvider in your other classes and exchange context, therefore inheriting all of the methods from that class,


public class BuyBoots extends MethodProvider implements Task {

    @Override
    public boolean validate() {
        return !getInventory().contains("Coins");
    }

    @Override
    public void execute() {
        log("Executing BuyBoots task.");
    }
}

public class Main extends Script {
    
    private ArrayList<Task> tasks = new ArrayList<Task>();
    
    private final BuyBoots boots = new BuyBoots();
    
    
    public void onStart() {
        boots.exchangeContext(getBot());
        Collections.addAll(tasks, new Bank(), new Teleport(), boots); //would use the same logic with the other classes as well.
    }

 

That's cool :) I've just been passing the methodprovider like i said above, could you explain the collections.addAll part for me? Is this giving the boots class access to the bank and teleport classes and methods without the need for a methodprovider?

so you can just do bank.xyz in the boots class instead of mp.bank.xyz?

34 minutes ago, Malcolm said:

@Avrae


        Collections.addAll(tasks, new Bank(), new Teleport(), boots); //would use the same logic with the other classes as well.

All this does is add the tasks to the ArrayList here:


    private ArrayList<Task> tasks = new ArrayList<Task>();

You would not be able to access the methods of the other classes (Bank/Teleport/Any other tasks you have) but at the same time if a script has this kind of structure you should never have to.

All that gets done is the loop goes through all of the tasks and if the validate method returns true it will execute that task. (the execute method in that specific task class)

 


	@Override
	public int onLoop() throws InterruptedException {
		for (Task task : tasks) {
			if (task != null && task.validate()) {
				task.execute();
			}
		}
		return random(250, 750);
	}

Ah! That makes more sense, thanks for the explanation gonna try this soon :)

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.