Jump to content

Error need help. Boolean.


Rumple

Recommended Posts

I keep getting this error. I believe I called everything correctly. 

 

1416961407_errorglassscript.PNG.0c606ab275312192fd63a82757cc918a.PNG

private boolean sodaToBuy(){
        RS2Widget sodaAsh = getWidgets().get(300, 16, 23);
        if(sodaAsh.getItemAmount() < 0)
            return true;
                else
            return false;
    }

    private boolean sandToBuy(){
        RS2Widget bucketSand = getWidgets().get(300, 16, 21);
        if(bucketSand.getItemAmount() < 0)
            return true;
                else
            return false;
    }

Loop

 

@Override
    public int onLoop() throws InterruptedException {
        RS2Widget shop= getWidgets().get(300, 1, 1);
        if(this.hopper.isHopping()) {
            return this.hopper.execute();
        }
        if (getInventory().contains("Coins") && atCrew() == true) {
            log("Had coins and started @ crew");
            crewMember();
            sleep(random(250));
        }else if (getInventory().contains("Coins") && atBank() == true){
            walkToCrew();
            sleep(random(250));
        }else if (!getInventory().contains("Coins") && atCrew() == true) {
            log("Had no coins started @ crew");
            walkToBank();
            sleep(random(250));
            bankAll();
            sleep(random(250));
            withdrawCoins();
            sleep(random(250));
        }else if (!getInventory().contains("Coins") && atBank() == true){
            log("Had no coins started @ bank");
            bankAll();
            sleep(random(250));
            withdrawCoins();
            sleep(random(250));
        }else if (sodaToBuy() == true) {
            buySoda();
        }else if (sodaToBuy() == false){
            if (sandToBuy() == true){
                buySand();
            }
        }else if (sandToBuy() == false && sodaToBuy() == false){
            this.hopper.hop(FrostHopper.HopMode.P2P);
        }else if (getInventory().isFull()){
            walkToBank();
            sleep(random(250));
            bankAll();
        }
    return 250;
    }
}

 

Link to comment
Share on other sites

5 hours ago, HeyImJamie said:

You're not null checking. Also, you can simplify your methods by just returning the boolean value of widget.getAmount > 0

 

5 hours ago, jca said:

For each boolean 

return widget != null && widget.getItemAmount() > 0;

Don’t I have to specify which widget I’m using as I have a few that I am checking. These are just the ones that are getting flagged because I’m checking if there is stock in shop?!

Link to comment
Share on other sites

5 hours ago, Rumple said:

 

Don’t I have to specify which widget I’m using as I have a few that I am checking. These are just the ones that are getting flagged because I’m checking if there is stock in shop?!

It’s throwing a NullPointerException because you’re calling getItemAmount() on a widget that doesn’t exist somewhere in the loop.

private boolean sodaToBuy(){
	RS2Widget sodaAsh = getWidgets().get(300, 16, 23);

	return sodaAsh != null && sodaAsh.getItemAmount() > 0;
}

Also you don't need to use the equal to operator when checking booleans, instead

if ( sodaToBuy() ){
	buySoda();
}

 

Edited by jca
Link to comment
Share on other sites

5 hours ago, jca said:

It’s throwing a NullPointerException because you’re calling getItemAmount() on a widget that doesn’t exist somewhere in the loop.


private boolean sodaToBuy(){
	RS2Widget sodaAsh = getWidgets().get(300, 16, 23);

	return sodaAsh != null && sodaAsh.getItemAmount() > 0;
}

Also you don't need to use the equal to operator when checking booleans, instead


if ( sodaToBuy() ){
	buySoda();
}

 

So one question why do you check null and less than 0. Shouldn’t I be checking to see if there is more than 0?

okay so If I need to check if it’s false do I just add .equals(False) or == false? Because if both are false I need to hop worlds.

Link to comment
Share on other sites

1 hour ago, Rumple said:

So one question why do you check null and less than 0. Shouldn’t I be checking to see if there is more than 0?

okay so If I need to check if it’s false do I just add .equals(False) or == false? Because if both are false I need to hop worlds.

I'm checking for greater than. 

Greater than: > checks if the reference on the left is greater than the reference on the right.

Less than: < check if the reference on the left is less than the reference on the right.  

5 > 2 // true
2 < 5 // true 
5 < 2 // false

So if the shop has greater than 0 buy the soda ash.

To check if it's false add an exclamation mark before the boolean function.

if ( !sodaAshToBuy() ){
	//hop
}

 

Edited by jca
Link to comment
Share on other sites

2 hours ago, jca said:

I'm checking for greater than. 

Greater than: > checks if the reference on the left is greater than the reference on the right.

Less than: < check if the reference on the left is less than the reference on the right.  


5 > 2 // true
2 < 5 // true 
5 < 2 // false

So if the shop has greater than 0 buy the soda ash.

To check if it's false add an exclamation mark before the boolean function.


if ( !sodaAshToBuy() ){
	//hop
}

 

 

1 hour ago, Khaleesi said:

Just add a null check before you get details out of a widget ...
if(bucket != null && bucket.getAmount() >5)

You don't reayy need to use hasBuckets() == true ...
You can just do hasBuckets() instead ^^ // Has Buckets

Use a ! to revert the boolean => !hasBuckets()   // Has no buckets

Thanks guys alot

Link to comment
Share on other sites

NullPointerException occurs when you're trying to do stuff to something that doesn't exist. You need to check to make sure your 'thing' exists first before doing any further checks against it:

RS2Widget sodaAsh = getWidgets().get(300, 16, 23);

if (sodaAsh != null) { // this tells us sodaAsh variables has a value
  if(sodaAsh.getItemAmount() < 0) { // now we can check what's inside of it
    // ...
  }
}

 

Link to comment
Share on other sites

I think im going to cry. I tried this loop 50 different ways nothing seems to work.

So even though shop has stock it keeps trying to hop saying there is no stock.

 

 @Override
    public int onLoop() throws InterruptedException {
        RS2Widget shop = getWidgets().get(300, 1, 0);
        if(this.hopper.isHopping()) {
            return this.hopper.execute();
        }
        if (getInventory().contains("Coins")) {
            log("Had coins and started @ crew");
            crewMember();
            sleep(random(250));
            if (shop != null && sodaToBuy()) {
                log("Soda in stock buying soda!");
                buySoda();
            }
                if (!sodaToBuy()) {
                    if (sandToBuy())
                        buySand();
                }
                        if(!sandToBuy() && !sodaToBuy() && shop != null) {
                            log("No Soda or Sand Hopping!");
                            widgets.closeOpenInterface();
                            this.hopper.hop(FrostHopper.HopMode.P2P);
                        }
        }else if (!getInventory().contains("Coins")) {
            log("Had no coins started @ crew");
            sleep(random(250));
            bankAll();
            sleep(random(250));
            withdrawCoins();
            sleep(random(250));
        } else if (getInventory().isFull()){
            log("Inv full going to bank!");
            walkToBank();
            sleep(random(250));
            bankAll();
        }
    return 250;
    }
}

 

Edited by Rumple
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...