Rumple Posted November 30, 2018 Posted November 30, 2018 I keep getting this error. I believe I called everything correctly. 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; } }
HeyImJamie Posted November 30, 2018 Posted November 30, 2018 You're not null checking. Also, you can simplify your methods by just returning the boolean value of widget.getAmount > 0
jca Posted November 30, 2018 Posted November 30, 2018 (edited) For each boolean return widget != null && widget.getItemAmount() > 0; Edited December 1, 2018 by jca
Rumple Posted December 1, 2018 Author Posted December 1, 2018 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?!
jca Posted December 1, 2018 Posted December 1, 2018 (edited) 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 December 1, 2018 by jca
Rumple Posted December 1, 2018 Author Posted December 1, 2018 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.
jca Posted December 1, 2018 Posted December 1, 2018 (edited) 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 December 1, 2018 by jca
Khaleesi Posted December 1, 2018 Posted December 1, 2018 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
Rumple Posted December 1, 2018 Author Posted December 1, 2018 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
liverare Posted December 1, 2018 Posted December 1, 2018 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 // ... } }
Rumple Posted December 4, 2018 Author Posted December 4, 2018 (edited) 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 December 4, 2018 by Rumple
R I F T Posted December 4, 2018 Posted December 4, 2018 (edited) I think you should print the contents of your booleans so you see what you're getting back. If they don't return what you expect, you've found your problem Edited December 4, 2018 by R I F T