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
  • Widget Checking: Null or not


    kamilo
     Share

    Recommended Posts

    Was experiencing problems with this so might as well spent some time on it make it good;

    Regular Tested snippet;

    /* Null Pointer Error (NPE); Example: 
    	 * 	WidgetChild smithItem = getWidgets().getWidget(312).getChild(22);
    	 * 	-> if widget 312 is null you'll get an NPE 
    	 * 	Therefore, always use:  getWidgets().getWidgetChild(Parent_ID, Child_ID) */
    
    	// Checking using Map Widget
    	int Parent_ID = 595;
    	int Child_ID = 24;
    	int GrandChild_ID = 1;
    
    	Widget parent = getWidgets().getWidget(Parent_ID);
    	WidgetChild child = getWidgets().getWidgetChild(Parent_ID, Child_ID);
    	WidgetChild grandchild = getWidgets().getWidgetChild(Parent_ID, Child_ID, GrandChild_ID);
    	
    	if (parent != null && parent.isVisible()) {
    			//log("Widget Parent: found.");
    		if (child != null && child.isVisible()) {
    				//log("Widget Child: found.");
    			if (grandchild != null && child.isVisible()) {
    				//log("Widget GrandChild: found.");
    			} else {
    				//log("Widget GrandChild: Not found.");
    			}
    		} else {
    			//log("Widget Child: Not found.");
    		}
    	} else {
    		//log("Widget Parent: Not found.");
    	}

     

    Made it into a method

    public boolean widgetCheck(int Parent_ID, int Child_ID, int GrandChild_ID) {
    		  	boolean check;		  	
    		  	Widget parent = getWidgets().getWidget(Parent_ID);
    			WidgetChild child = getWidgets().getWidgetChild(Parent_ID, Child_ID);
    			WidgetChild grandchild = getWidgets().getWidgetChild(Parent_ID, Child_ID, GrandChild_ID);
    			
    				if (parent != null && parent.isVisible()) {
    					if (child != null && child.isVisible()) {
    						if (grandchild != null && child.isVisible()) {
    							check = true;
    						} else {
    							check = false;					
    						}
    					} else {
    						check = false;				
    					}
    				} else {
    					check = false;
    				}			
    			return check;
    	  }

    Returns True if widget isn't null and false if widget is null.

     

    thoughts?

     

    Link to comment
    Share on other sites

     public boolean widgetCheck(int...IDs) {
    		  	boolean check;
    		  	List<Integer> WidgetIds = new ArrayList<Integer>();
    		  	
    		  	for (int ID: IDs) {
    		  		WidgetIds.add(ID);
    		  	}		  	
    		  		if (WidgetIds.size() >= 1) {
    		  			int Parent_ID = WidgetIds.get(0);
    		  			Widget parent = getWidgets().getWidget(WidgetIds.get(0));
    		  			if ((WidgetIds.size() >= 2)) {
    		  				int Child_ID = WidgetIds.get(1);
    		  				WidgetChild child = getWidgets().getWidgetChild(Parent_ID, Child_ID);	
    		  				if (WidgetIds.size() >= 3) { // can also be equal == 
    		  					int GrandChild_ID = WidgetIds.get(2);
    		  					WidgetChild grandchild = getWidgets().getWidgetChild(Parent_ID, Child_ID, GrandChild_ID);
    		  					if (grandchild != null && child.isVisible()) { // => Widget has 3 values; checking if grandchild is null or not
    								check = true;
    							} else {
    								check = false;					
    							}
    		  				} else if (child != null && child.isVisible()) { // => Widget has only 2 values; checking if child is null or not
    		  					check = true;
    		  				} else {
    		  					check = false;
    		  				} 
    		  			} else if (parent != null && parent.isVisible()) { // => Widget has only 1 value; therefore checking if parent is null or not
    		  				check = true;
    		  			} else {
    		  				check = false;
    		  			}		  			
    		  		}	
    			return check;
    	  }

    New method for checking if widget is null or not, can check parent, parent;child, or parent;child;grandchild

    this one is more efficient incase i need to only check parent and child, need feedback on this ^^^^

    Link to comment
    Share on other sites

     public boolean nullCheck(int...IDs) {
    		  	boolean check;
    		  	List<Integer> WidgetIds = new ArrayList<Integer>();
    		  	
    		  	for (int ID: IDs) {
    		  		WidgetIds.add(ID);
    		  	}		  	
    		  		if (WidgetIds.size() >= 1) {
    		  			int Parent_ID = WidgetIds.get(0);
    		  			Widget parent = getWidgets().getWidget(WidgetIds.get(0));
    		  			if ((WidgetIds.size() >= 2)) {
    		  				int Child_ID = WidgetIds.get(1);
    		  				WidgetChild child = getWidgets().getWidgetChild(Parent_ID, Child_ID);	
    		  				if (WidgetIds.size() >= 3) { // can also be equal == 
    		  					int GrandChild_ID = WidgetIds.get(2);
    		  					WidgetChild grandchild = getWidgets().getWidgetChild(Parent_ID, Child_ID, GrandChild_ID);
    		  					if (grandchild != null && child.isVisible()) { // => Widget has 3 values; checking if grandchild is null or not
    								check = true;
    							} else {
    								check = false;					
    							}
    		  				} else if (child != null && child.isVisible()) { // => Widget has only 2 values; checking if child is null or not
    		  					check = true;
    		  				} else {
    		  					check = false;
    		  				} 
    		  			} else if (parent != null && parent.isVisible()) { // => Widget has only 1 value; therefore checking if parent is null or not
    		  				check = true;
    		  			} else {
    		  				check = false;
    		  			}		  			
    		  		} else {
    		  			check = false;
    		  		}
    			return check;
    	  }

    same method as above forgot to add check = false at the bottom.

     

    Link to comment
    Share on other sites

    9 hours ago, Nuclear Nezz said:

    What is the goal here?

    basically an easy method segment to check if widget, parent, child or grandchild is null or not

    just lookin for feedback since i was experiencing issues with NPE 

    Link to comment
    Share on other sites

    14 minutes ago, kamilo said:

    basically an easy method segment to check if widget, parent, child or grandchild is null or not

    just lookin for feedback since i was experiencing issues with NPE 

    if you're looking for like:

    if a is null, return ?, if a.child(b) is null, return ??, if a.child(b).child(c) is null, return ???
    If you're just checking to see if *any* of them will return null (as deep as it goes) then just doing
    getWidgets().getWidgetChild(parent,child,grandchild) will return null if any of them are null.

    If you're trying to find out at which point it turns null, you could just do:
     

    Widget w = getWidgets().getWidget(ids.get(0));
    if w == null return 0
    WidgetChild wc = ids.size() > 1 ? w.getChild(ids.get(1)) : null;
    if wc == null, return 1
    for i = 2 in ids.size()
        wc = wc.getChild(ids.get(i))
        if wc == null return i
        
        

    Or something along those lines, at least. Granted, since you know it's only ever at most going to be 3 deep, you could just check it in their various lines.
    parent = getwidget(0)
    if parent null or not visible return false

    if checking size is 1, return true
    child = parent.getChild(1)
    if child null or not visible return false

    if checking size is 2, return true

    etc

    Since you only have 3 at most you're checking, you don't need your for loops, also when you have:
    int...IDs
    IDs is an array, you can access it like any other array, IDs[0]
    I'd also suggest you look a bit at Java name conventions, camelCase is preferred for variable and method names, unless the variable is final. Underscores aren't often used.

    So in this instance it would be ids[0] not IDs, but with ID it's a little odd, and generally up to you. I still prefer the camelCase. npcID, idOfNpc, etc.

    Classes are named with the upper camel case, WidgetFinder, etc.

    So when I see a variable like: WidgetIDs.size() I immediately think of a static method from a class, not a method from a variable.

     

    Oh I forgot the isVisible check in the code block, wc == null || !wc.isVisible

    I guess overall I'm still not sure what your code is trying to accomplish. As it currently stands, I believe you only actually check the deepest widget, nothing before it, which kinda defeats the purpose of checking as you are anyway.
    For example, you have: if the size is >= 1, grab parent
    but you don't check parent, instead you check if size >= 2, if it is, grab (parent,child)
    you don't check that widget, instead you check if size >= 3, if it is, grab (parent,child,grandchild)
    Instead since that's how you're checking it, you'd really just need
    if size == 0, error
    else if size == 1, grab parent and check it
    else grab widget(widgetIds) and check it. If it's size 2, then you'll hit your 2nd conditional and exit. If it's size 3, then you'll pass through 2nd conditional, into 3rd, and hit this and exit.
     

    Link to comment
    Share on other sites

    24 minutes ago, Nuclear Nezz said:

    if you're looking for like:

    if a is null, return ?, if a.child(b) is null, return ??, if a.child(b).child(c) is null, return ???
    If you're just checking to see if *any* of them will return null (as deep as it goes) then just doing
    getWidgets().getWidgetChild(parent,child,grandchild) will return null if any of them are null.

    If you're trying to find out at which point it turns null, you could just do:
     

    Widget w = getWidgets().getWidget(ids.get(0));
    if w == null return 0
    WidgetChild wc = ids.size() > 1 ? w.getChild(ids.get(1)) : null;
    if wc == null, return 1
    for i = 2 in ids.size()
        wc = wc.getChild(ids.get(i))
        if wc == null return i
        
        

    Or something along those lines, at least. Granted, since you know it's only ever at most going to be 3 deep, you could just check it in their various lines.
    parent = getwidget(0)
    if parent null or not visible return false

    if checking size is 1, return true
    child = parent.getChild(1)
    if child null or not visible return false

    if checking size is 2, return true

    etc

    Since you only have 3 at most you're checking, you don't need your for loops, also when you have:
    int...IDs
    IDs is an array, you can access it like any other array, IDs[0]
    I'd also suggest you look a bit at Java name conventions, camelCase is preferred for variable and method names, unless the variable is final. Underscores aren't often used.

    So in this instance it would be ids[0] not IDs, but with ID it's a little odd, and generally up to you. I still prefer the camelCase. npcID, idOfNpc, etc.

    Classes are named with the upper camel case, WidgetFinder, etc.

    So when I see a variable like: WidgetIDs.size() I immediately think of a static method from a class, not a method from a variable.

     

    Oh I forgot the isVisible check in the code block, wc == null || !wc.isVisible

    I guess overall I'm still not sure what your code is trying to accomplish. As it currently stands, I believe you only actually check the deepest widget, nothing before it, which kinda defeats the purpose of checking as you are anyway.
    For example, you have: if the size is >= 1, grab parent
    but you don't check parent, instead you check if size >= 2, if it is, grab (parent,child)
    you don't check that widget, instead you check if size >= 3, if it is, grab (parent,child,grandchild)
    Instead since that's how you're checking it, you'd really just need
    if size == 0, error
    else if size == 1, grab parent and check it
    else grab widget(widgetIds) and check it. If it's size 2, then you'll hit your 2nd conditional and exit. If it's size 3, then you'll pass through 2nd conditional, into 3rd, and hit this and exit.
     

    oh well say I want to check just the parent I can enter 1 widget id, it will do just that

    and if I want to check Parent AND child I can just enter 2 ids , it will do that

    if grandchild, it will do also that

    basically one method and I can use it for any widget check; that is my goal. sorry to not clarify before 

     

    thank you for this; camelCase is preferred for variable and method names, unless the variable is final. Underscores aren't often used.

    ^ regarding this, I decided to switch it up a bit to make it easier to read for myself, I can easily use this method, if its going to mess up my code later I won't use it from the start, let me know

    Link to comment
    Share on other sites

    I mean when it's your own code, you can do whatever you want.

    But if you ever program with another Java developer, you should really get into the habit of using the Java naming conventions.

     

    If that's all you're trying to do, then yeah you'd just have 3 simple checks

    1. edge case, check if given argument is length 0, if it is return w/e is helpful for your code.

    2. if argument length is 1, check parent, return parent info.

    3. use getWidgetChild(ids) to get the widget of any depth, whether it's 1 child, a grand child, or a great great grand child, and check the info on that.

    You have a lot of code that does kinda the same thing. Basically the checks for children and grandchildren in your code is the same thing, and you don't need to do it.

    It also means you're grabbing the same widgets 3 times by the end. Grab parent first, then parent and child, then parent, child, and grandchild.

    Not that it means much in the short term, but imagine calling that 100,000 times.

    Generally when you see chunks of code that do the same thing, but just repeated a few times in a slightly different way, you can refactor it to be less copy-paste code.

    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.