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.

Extending method provider and nullpointerexception

Featured Replies

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?

 

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

  • Author
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

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. 

2 minutes ago, progamerz said:

exchangeContext(getbot()) iirc

 

This.

After creating an instance of your class, you then need to call exchangeContext once.

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

Yh, make an instance of ActionStates in your main class. And exchangeContext in your onstart method.

ActionStates ActionStates = new ActionStates();

public void onStart() {

ActionStates.exchangeContext(getbot());

}

5 minutes ago, Eagle Scripts said:

Progamez answer's probably what you mean :)

Yea that might be his preferred method which is deprecated :troll: 

  • Author

Thanks, that worked. :) (although .exchangeContext() has line through it in eclipse because it's deprecated I guess).

7 minutes ago, Theorems said:

Thanks, that worked. :) (although .exchangeContext() has line through it in eclipse because it's deprecated I guess).

It's not really deprecated though, it's marked for internal use, nothing to stop you using it though.

23 minutes ago, dreameo said:

Yea that might be his preferred method which is deprecated :troll: 

There is no other way lol

2 minutes ago, progamerz said:

There is no other way lol

You reference MethodProvider from 'Main class' that extends Script to all of your other classes.  

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

@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

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.