Jump to content

Extending method provider and nullpointerexception


Recommended Posts

Posted

I'm new to java & scripting and I'm gonna have to ask stuff here until I get verified on discord :boge:

I have a class with this method:

public class ActionStates extends MethodProvider {

	public State getState() {
		if(getInventory().isFull()) { // nullPointerException
			return State.BANK;
		}else if(myPlayer().isAnimating()) {
			return State.IDLE;
		}else {
			return State.FISH;
		}
	}

As you can see, I commented the line that throws the nullPointerException

The thing is, that code works when I put it in my main class which extends from Script... Is there something else I need to be able to do in order to use osbot methods in other classes? It looks like it works in eclipse but then I get that when it runs which doesn't make sense to me because getInventory().isFull() should only be returning true or false (not null) shouldn't it?

 

Posted (edited)
6 minutes ago, Chikan said:

That shouldn't be throwing a null pointer, maybe its something in your bank state that isn't null checked?

well that code works in my 'Main' class so I don't think that would be causing any issue. I'm just building the skeleton of my script right now so my action methods just look like this: 

public void bank() {
   log("bank");
}

public void idle() {
   log("idle");
}

public void fish() {
   log("fish");
}

 

Edited by Theorems
Posted

When you're extending MethodProvider, some inherited fields must be initialized. That is the only reason why you're getting null. 

What those fields are, i'm not sure (take a look in Script class and see how it handles it). There are different ways on dealing this situation. @Alek Can tell you the preferred way. I think he mentioned it before in the forums. 

Posted
1 minute ago, dreameo said:

When you're extending MethodProvider, some inherited fields must be initialized. That is the only reason why you're getting null. 

What those fields are, i'm not sure (take a look in Script class and see how it handles it). There are different ways on dealing this situation. @Alek Can tell you the preferred way. I think he mentioned it before in the forums. 

Progamez answer's probably what you mean :)

Posted

Alternatively you could pass a reference to the MethodProvider instance via the class constructor, i.e:

public class SomeClass extends SomeOtherClass implements SomeInterface {

private final MethodProvider mp;

public SomeClass (MethodProvider mp) {

this.mp =mp;

}

//...

}

In your main class which extends Script:

public final SomeClass sc = new SomeClass(this);

-Apa

  • Like 1
Posted (edited)

@Explv

Btw guys. Suppose I have these three classes:

Main (exchangesContext with BankTask), BankTask , Data (Data class contains methods which require api access)

 

Let's say the BankTask needs to use some methods from the Data class. I realised that I need to call exchangeContext inside BankTask aswell but there's obviously no onStart method there.

Should I let the Data class accept MethodProvider in the constructor instead or is it possible or even preferable to use exchangeContext in such cases?

 

Edited by Jammer

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