Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • GameObject throwing NullPointerException when it shouldn't?


    vhh
     Share

    Recommended Posts

    In my script, the bot teleports to House and then uses a mounted amulet of glory to go to edgeville from the house.

    However, as soon as he is in the House, the script fails and returns a NullPointerException for the Amulet of Glory (in specific, the line that includes "Edge.interact("Edgeville"); in the bottom snippet.
    But if I then stop the script and start it again from within the House, he uses the Amulet fine, meaning the GameObject is in fact not Null? If the bottom snippet returns true and the bot clicks the glory (which it does), how is the second snippet changing the Glory to be Null?

    It's almost as if loading in a new area (the house) makes the GameObject Null, but if I am already in the area with the GameObject, it is not null?

    GameObject Edge = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Amulet of Glory"));
    if (Edgeville.contains(getLocalPlayer())) {
        Inventory.interact("Teleport to house", "Break");
        sleepUntil(() -> House.contains(getLocalPlayer()), Calculations.random( 5000,8000));
    }
    if (House.contains(getLocalPlayer())) {
        Edge.interact("Edgeville");
        sleepUntil(() -> Edge.contains(getLocalPlayer()), Calculations.random( 5000,8000));
    }
    Link to comment
    Share on other sites

    8 minutes ago, Neffarion said:

    Make sure you null check "Edge" because it will be null if it has not loaded the gameobject yet (or it isnt nearby)

    Hi Neffarion,

    I believe I am null checking it when defining the object:

    GameObject Edge = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Amulet of Glory"));
    

    Should I be doing this again elsewhere?

    Link to comment
    Share on other sites

    2 minutes ago, vhh said:

    Hi Neffarion,

    I believe I am null checking it when defining the object:

    GameObject Edge = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Amulet of Glory"));
    

    Should I be doing this again elsewhere?

    No. You are passing a null check into the filter of GameObjects.closest()
    If there are no gameobjects that respect that filter then the result of Edge will be null.
    So you need to check if Edge is null afterwards:

     

    if (House.contains(getLocalPlayer()) && Edge != null && Edge.interact("Edgeville")) {
        sleepUntil(() -> Edgeville.contains(Client.getLocalPlayer()), Calculations.random(5000,8000));
    }

     

    Link to comment
    Share on other sites

    44 minutes ago, Neffarion said:

    No. You are passing a null check into the filter of GameObjects.closest()
    If there are no gameobjects that respect that filter then the result of Edge will be null.
    So you need to check if Edge is null afterwards:

     

    if (House.contains(getLocalPlayer()) && Edge != null && Edge.interact("Edgeville")) {
        sleepUntil(() -> Edgeville.contains(Client.getLocalPlayer()), Calculations.random(5000,8000));
    }

     

    Hi Neffaerion,

    I have tried to implement your suggestion, which resulted in nothing happening, as the IF statement came back false. I changed the "Edge != null"  to "Edge == null", and the IF statement came back true, but it couldn't interact with the Glory as the object is null.

    Your IF statement passes if I run the script from the House, as the Glory does not show as null. 

    I am really puzzled with this.

     

    This comes back as false and my script runs endlessly not doing anything:

    if (House.contains(getLocalPlayer()) && Edge != null && Edge.interact("Edgeville")) {
        sleepUntil(() -> Edgeville.contains(Client.getLocalPlayer()), Calculations.random(5000,8000));
    }

    This comes back as true, but it cannot continue to interact with the Glory as it is null, resulting in my script throwing a NullPointerException

     

    if (House.contains(getLocalPlayer()) && Edge == null && Edge.interact("Edgeville")) {
        sleepUntil(() -> Edgeville.contains(Client.getLocalPlayer()), Calculations.random(5000,8000));
    }
    Link to comment
    Share on other sites

    7 hours ago, vhh said:

    Edge == null && Edge.interact("Edgeville")

    This will do nothing but throw a NPE at you, because you're ensuring Edge is null before interacting with it.

    My best guess is that you're calling the code below only once, outside a method, so it's a class variable. In that case, when you're logged out, you start the script, it finds the gameobject once, and because it's logged out it can't be found. Now your Edge variable has null as its value. This is not how you should be doing it.

    10 hours ago, vhh said:

    GameObject Edge = GameObjects.closest(gameObject -> gameObject != null && gameObject.getName().equals("Amulet of Glory"));

    @Override
    private int onLoop() { ...

    Instead, whenever you need to interact with your POH's glory, search for the gameobject again. You'd be calling it in your onLoop as shown in the code below.

    @Override
    private int onLoop() {
        if (isInHouse()) {
        	if (shouldTeleportAway()) {
            	GameObject wallGlory = GameObjects.closest("Amulet of Glory");
              	if (wallGlory != null && wallGlory.interact("Edgeville")) {
                        MethodProvider.sleepUntil(() -> isAtEdgeville(), 10000);
                    }
            } else {
             	... 
            }
        } else {
         	... 
        }
    }

     

    Link to comment
    Share on other sites

    As an extra add (this is more of a query than anything), is it possible that the glory (assuming it's the wall mounted one), could be categorized as a WallObject not a GameObject?

    Link to comment
    Share on other sites

    On 1/22/2021 at 12:19 PM, Hashtag said:

    This will do nothing but throw a NPE at you, because you're ensuring Edge is null before interacting with it.

    My best guess is that you're calling the code below only once, outside a method, so it's a class variable. In that case, when you're logged out, you start the script, it finds the gameobject once, and because it's logged out it can't be found. Now your Edge variable has null as its value. This is not how you should be doing it.

    Instead, whenever you need to interact with your POH's glory, search for the gameobject again. You'd be calling it in your onLoop as shown in the code below.

    @Override
    private int onLoop() {
        if (isInHouse()) {
        	if (shouldTeleportAway()) {
            	GameObject wallGlory = GameObjects.closest("Amulet of Glory");
              	if (wallGlory != null && wallGlory.interact("Edgeville")) {
                        MethodProvider.sleepUntil(() -> isAtEdgeville(), 10000);
                    }
            } else {
             	... 
            }
        } else {
         	... 
        }
    }

     

    Thank you Hashtag, I was indeed calling it outside the method.

    Link to comment
    Share on other sites

    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
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.