Jump to content

NPE when trying to access MethodProvider instance


Epos OSBot

Recommended Posts

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?

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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