Jump to content
RS13159265

new Bank().open() is causing very strange behaviour. Raising null exception, but it's not null.

Recommended Posts

The following code: 

package MasterScripts;

import org.osbot.rs07.api.Bank;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author="Me", info="NA", logo="", name="TestFunctions", version=1)
public class TestFunctions extends Script {

    @Override
    public int onLoop() throws InterruptedException {
        log("Starting script...");
        Bank b = new Bank();
        log("b is "+b);
        if (b == null) { log("b is null"); }
        try {
            log("Calling b.open()");
            b.open();
        } catch (Exception e) {
            log("b.open() generating exception e: "+e);
        }
        return 10000;
    }

}

Generates 

Quote

[INFO][Bot #1][06/04 09:34:02 PM]: Starting script...
[INFO][Bot #1][06/04 09:34:02 PM]: b is org.osbot.rs07.api.Bank@65ebe8f5
[INFO][Bot #1][06/04 09:34:02 PM]: Calling b.open()
[INFO][Bot #1][06/04 09:34:02 PM]: b.open() generating exception e: java.lang.NullPointerException
[INFO][Bot #1][06/04 09:34:02 PM]: Finishing function

What???

As we check, b is a valid pointer, not null. Despite that, calling b.open() generates a NullPointerException. I really have no idea what could be going on. 

Ideas?

Link to post
Share on other sites
Posted (edited)
3 hours ago, RS13159265 said:

The following code: 



package MasterScripts;

import org.osbot.rs07.api.Bank;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author="Me", info="NA", logo="", name="TestFunctions", version=1)
public class TestFunctions extends Script {

    @Override
    public int onLoop() throws InterruptedException {
        log("Starting script...");
        Bank b = new Bank();
        log("b is "+b);
        if (b == null) { log("b is null"); }
        try {
            log("Calling b.open()");
            b.open();
        } catch (Exception e) {
            log("b.open() generating exception e: "+e);
        }
        return 10000;
    }

}

Generates 

What???

As we check, b is a valid pointer, not null. Despite that, calling b.open() generates a NullPointerException. I really have no idea what could be going on. 

Ideas?

Just use getBank().open()

Edited by Khaleesi
Link to post
Share on other sites
Posted (edited)
1 hour ago, RS13159265 said:

Thanks. 

For my understanding:

1) What does new Bank() do / refer to? 

2) And, reiterating, why does a non-null object raise a Null exception? 


1. You're creating a new instance of the Bank class, this is an OSBot API class which extends MethodProvider. Any class which extends MethodProvider needs to have some context provided, so that it has access to all the other OSBot API class instances (e.g. inventory, equipment, etc.). In your case, you have not provided this context, and so all of these API references are null. You don't *need* to create an instance of the Bank class anyway, because OSBot provides you one that is correctly setup, which you access via getBank().
2. It is the `open` function throwing a null pointer exception. This means that something the open function is trying to access is null (again because you have not exchanged context, so the internal fields in the Bank class are null)


You can see the open function here:

epXNEqD.png

On the 4th line of the function you can see `this.inventory.isItemSelected()`

`this.inventory` will be null, as you have not exchanged context. Therefore this function call will throw a NullPointerException.

This would probably work:
 

Bank bank = new Bank();
bank.exchangeContext(getBot());


exchangeContext sets all the internal context fields in the API class instance using the provided Bot reference. You can see the implementation here:

4Nu06jd.png

However there is no need for it, because you can access the already setup instance using `getBank()`.

Edited by Explv
  • Like 2
Link to post
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.

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