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
  • NullPointerExeption Error Smithing Script


    G_G

    Recommended Posts

    Hi,

    I tried making my first script  and I'm getting an NullpointerExeption error that I can't seem to solve. I tried making the code as clear as possible by deviding it into smaller functions, and I'm getting the error when running the smith() function below. Can anybody tell my what I do wrong?

            private void smith() {
    		GameObject anvil = getGameObjects().closest(gameobject -> gameobject != null && gameobject.hasAction("Smith"));
    		WidgetChild smithItem = getWidgets().getWidget(312).getChild(22);
    		
    		if (anvil != null && anvil.interact("Smith")) {
    			if (widgetCheck(smithItem)) {
    				if(smithItem!= null && smithItem.interact("Smith")) {
    					sleepUntil(() -> isFinished(), 8000);	
    				}
    			}
    		}
    	}
    
    	// Check if the widget is open.
    	private boolean widgetCheck(WidgetChild widget) {
    		return widget != null && widget.isVisible();
    	}
    
    	// Are we finished smithing?
    	private boolean isFinished() {
    		return getInventory().count("Iron bar") < 5; //because I only make iron platebodies
    	}

     

    Link to comment
    Share on other sites

    Thanks, that got rid of the error. But the bot still won't interact with the widget. instead it just hovers above another item in the Smithing menu.

    Link to comment
    Share on other sites

    for (WidgetChild i : getWidgets().getWidget(312).getChildren()) {
    	for (WidgetChild j : i.getChildren()) {
    		if (j != null && j.getItem().getID() == 1205) {
    			j.interact();
    		}
    	}
    }

    If you want to future-proof your script you can use this. It will work even if they change the widgets for the interface due to the fact that it is based on the item id. I already set the iron platebody id.

    Link to comment
    Share on other sites

    Thanks for the help!

    I think I understand how widgets work now, but I still can't get the script to work correctly. I tried to debug by logging different steps and found that the selected widget is allways null and therefore the script will never get to the interact line. Does anybody know what can cause this?

    Link to comment
    Share on other sites

    On 9/2/2019 at 11:39 AM, G_G said:

    Thanks for the help!

    I think I understand how widgets work now, but I still can't get the script to work correctly. I tried to debug by logging different steps and found that the selected widget is allways null and therefore the script will never get to the interact line. Does anybody know what can cause this?

    Try deleting your cache located in "C:/Users/{You}/jagexcache"

    Link to comment
    Share on other sites

    This is the latest version I was trying. The last line in the log is "Check completed", but it doesn't do anything after that.

    import org.dreambot.api.script.ScriptManifest; // API can be found here: https://dreambot.org/javadocs/
    import org.dreambot.api.script.impl.TaskScript;
    import org.dreambot.api.methods.map.Area;
    import org.dreambot.api.script.Category;
    
    @ScriptManifest(author = "G_G", name = "Smither", version = 0.1, description = "Smith Iron Platebodies", category = Category.SMITHING)
    public class Main extends TaskScript {
    
    	private static Area bankArea = new Area(3181, 3439, 3185, 3435, 0);
    	private static Area smithArea = new Area(3186, 3426, 3189, 3424, 0);
    
    	/**
    	 * When the user starts the script, this code gets run once in the beginning.
    	 */
    	@Override
        public void onStart() {
    		log("STARTING!");
    		addNodes(new BankNode(), new SmithNode());
    	}
    
    	@Override
    	public void onExit() {
    		log("STOPPED!");
    	}
    	
    	
    	//////////////////////////GETTERS///////////////////////////////
    	
    	/*
    	 * Getter function for the banking area
    	 */
    	static Area getBankArea(){
    		return bankArea;
    	}
    	
    	/*
    	 * Getter function for the smithing area
    	 */
    	static Area getSmithArea() {
    		return smithArea;
    	}
    	
    }
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.script.TaskNode;
    import org.dreambot.api.wrappers.interactive.NPC;
    
    /*
     * @author G_G
     * @version 1.0
     * @date 31/08/2019
     */
    public class BankNode extends TaskNode {
    
    	@Override
    	public int priority() {
    		return 2;
    	}
    
    	/*
    	 * This node will only be executed if this method returns true.
    	 * 
    	 * @return true if all conditions are met.
    	 */
    	@Override
    	public boolean accept() {
    		return inventoryCheck();
    	}
    
    	/*
    	 * This acts as the loop where we will interact with the game. Ideal logic is to
    	 * have one action per loop to avoid unexpected incidents such as lag spikes
    	 * messing with our script.
    	 * 
    	 * @return time in milliseconds to sleep before looping again.
    	 */
    	@Override
    	public int execute() {
    		if (Main.getBankArea().contains(getLocalPlayer())) {
    			bank();
    		} else {
    			if (getWalking().walk(Main.getBankArea().getRandomTile())) {
    				sleep(Calculations.random(3000, 5500));
    			}
    		}
    		return Calculations.random(300, 600);
    	}
    
    	/////////////////////////////////////////////////////////////////////
    	/*
    	 * Check if the necessary items are in the inventory.
    	 */
    	private boolean inventoryCheck() {
    		boolean status = false;
    		if (getInventory().contains("Hammer") && getInventory().count("Iron bar") > 5) {
    			status = true;
    		}
    		return status;
    	}
    
    	/*
    	 * Method for banking.
    	 */
    	private void bank() {
    		NPC banker = getNpcs().closest(npc -> npc != null && npc.hasAction("Bank"));
    		if (banker != null && banker.interact("Bank")) {
    			if (sleepUntil(() -> getBank().isOpen(), 9000)) {
    				if (getBank().depositAllExcept(item -> item != null && item.getName().contains("Hammer"))) {
    					if (sleepUntil(() -> !getInventory().contains("Iron Platebody"), 8000)) {
    						if (getBank().withdrawAll(item -> item != null && item.getName().contains("Iron bar"))) {
    							if (sleepUntil(() -> !getInventory().contains("Iron bar"), 8000)) {
    								if (getBank().close()) {
    									sleepUntil(() -> !getBank().isOpen(), 8000);
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	
    }
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.script.TaskNode;
    import org.dreambot.api.wrappers.interactive.GameObject;
    import org.dreambot.api.wrappers.widgets.WidgetChild;
    
    /*
     * @author G_G
     * @version 1.0
     * @date 31/08/2019
     */
    public class SmithNode extends TaskNode {
    
    	/*
    	 * Every node has its own priority. The higher the priority number is, the more
    	 * important it is.
    	 * 
    	 * @return priority of this node
    	 */
    	@Override
    	public int priority() {
    		return 1;
    	}
    
    	/*
    	 * This node will only be executed if this method returns true.
    	 * 
    	 * @return true if all conditions are met.
    	 */
    	@Override
    	public boolean accept() {
    		return true;
    	}
    
    	/*
    	 * This acts as the loop where we will interact with the game. Ideal logic is to
    	 * have one action per loop to avoid unexpected incidents such as lag spikes
    	 * messing with our script.
    	 * 
    	 * @return time in milliseconds to sleep before looping again.
    	 */
    	@Override
    	public int execute() {
    		if (Main.getSmithArea().contains(getLocalPlayer())) {
    			smith();
    		} else {
    			if (getWalking().walk(Main.getSmithArea().getRandomTile())) {
    				sleep(Calculations.random(3000, 5500));
    			}
    		}
    		return Calculations.random(300, 600);
    	}
    
    	/////////////////// METHODS/////////////////////////
    
    	// Method for smitting
    	private void smith() {
    		GameObject anvil = getGameObjects().closest(gameobject -> gameobject != null && gameobject.hasAction("Smith"));
    		WidgetChild smithItem = getWidgets().getWidgetChild(312, 22);
    
    		if (anvil != null && anvil.interact("Smith")) {
    			log("Opened interface succesfully");
    		    if (widgetCheck(smithItem)) {
    		    	log("check completed");
    		    	smithItem.interact();
    				log("Interacted succesfully");
    				sleepUntil(() -> isFinished(), 8000);
    				log("sleeping");
    		    }
    		}
    	}
    
    	// Check if the widget is open.
    	private boolean widgetCheck(WidgetChild widget){
    		boolean check = false;
    		if (widget != null && widget.isVisible() && widget.getItem().getID() == 1205) {
    			check = true;
    		}
    		return check;
    	}
    
    	// Are we finished smithing?
    	private boolean isFinished() {
    		return getInventory().count("Iron bar") < 5;
    	}
    
    }

     

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • 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.