Jump to content

NPE when trying to access MethodProvider instance


Recommended Posts

Posted
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?

 

Posted (edited)

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
Posted
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?

Posted
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

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