Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Bug NullPointerException thrown after NullCheck!?

Joined
Mar 23, 2018
Messages
4
Hello everyone. I am currently coding a bot that will calculate the players wealth but am stumped by this bug that keeps occuring.

Code:
java.lang.NullPointerException
    at com.forgottenape101.bots.WealthWatcher.tasks.Calculate.execute(Calculate.java:49)
    at com.runemate.game.api.script.framework.task.TaskBot.onLoop(kub:122)
    at com.runemate.game.api.script.framework.LoopingBot.run(hub:52)
    at com.runemate.game.api.script.framework.AbstractBot.start(zvb:10985)
    at nul.IIIiIIiiIIiiI.run(thc:26)

Keeps being thrown after a certain amount of iterations each time I run the bot. I don't understand why it successfully calculates the players inventory until a random point where it throws this NPE, even after null checking it. Am I missing something here?

Here's the relevant code:
Code:
package com.forgottenape101.bots.WealthWatcher.tasks;

import com.runemate.game.api.hybrid.entities.GameObject;
import com.runemate.game.api.hybrid.local.Camera;
import com.runemate.game.api.hybrid.local.hud.interfaces.Bank;
import com.runemate.game.api.hybrid.local.hud.interfaces.SpriteItem;
import com.runemate.game.api.hybrid.net.GrandExchange;
import com.runemate.game.api.hybrid.region.GameObjects;
import com.runemate.game.api.script.Execution;
import com.runemate.game.api.script.framework.task.Task;

public class Calculate extends Task {


    @Override
    public void execute() {

        GameObject booth = GameObjects.newQuery().names("Bank booth").actions("Bank").results().nearest();

        long wealth = 0;

        if(booth != null){

            if(!booth.isVisible()){

                System.out.println("Turning to see booth");
                Camera.turnTo(booth);
                Execution.delayUntil(()->booth.isVisible(), 200, 400);
            }

            if(Bank.open()){

                SpriteItem[] items = Bank.getItems().toArray(new SpriteItem[77]);

                if(items != null){

                    System.out.println("Total Items to Count: " + items.length);

                    for(int i = 0; i < items.length; i++){

                        int id = items[i].getId();

                        if(id != 0){

                            int q = Bank.getQuantity(id);

                            if(GrandExchange.lookup(id) != null) {

                                System.out.println("Looking up price for "+q+"x " + GrandExchange.lookup(id).getName());

                                int addwealth = GrandExchange.lookup(id).getPrice() * q;

                                wealth += addwealth;

                                System.out.println("Total Wealth: " + wealth);
                            }else{
                                System.out.println("Could not retreive info, moving to next item.");
                            }
                        }

                    }

                }

            }

        }

    }

    @Override
    public boolean validate() {
        return GameObjects.newQuery().names("Bank booth") != null;
    }
}
 
Misfits
Joined
Nov 21, 2016
Messages
1,538
System.out.println("Looking up price for "+q+"x " + GrandExchange.lookup(id).getName());
error points to this line, are all items tradable?
 
Joined
Mar 23, 2018
Messages
4
System.out.println("Looking up price for "+q+"x " + GrandExchange.lookup(id).getName());
error points to this line, are all items tradable?

I'm pretty sure they are, and if they weren't it should have set the if(GrandExchange.lookup(id) != null) boolean to false, thus bypassing the code that would cause the NPE, right?
 
Joined
May 24, 2016
Messages
1,113
The name of the item you are looking up is probably null. Do some more debugging.
 
12 year old normie
Joined
Jan 8, 2015
Messages
2,769
if(GrandExchange.lookup(id) != null) != GrandExchange.lookup(id).getName();
What. You're comparing a string to a boolean?

Also if you want to make sure you still have the right reference to the item, store the lookup in a variable, nullcheck that, then retrieve name using variable.getName()
 
Top