Jump to content

Extending method provider and nullpointerexception


Theorems

Recommended Posts

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?

 

Link to comment
Share on other sites

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

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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