Jump to content

Welcome to DreamBot

VIP Enhancement

Want to upgrade your DreamBot experience? Consider signing up for VIP!
VIP allows you to run as many accounts as you want, view the forums ad-free, receive 10% off all script purchases, and so much more!
Visit the store to learn about all of these great features!

Upgrade to VIP Now
Frequently Asked Questions
  • Are you not able to open the client? Make sure you have Java 8 installed
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox

DreamBot is the only Deadman Mode supported bot on the market!

Download the DreamBot client today!





Sign in to follow this  
G_G

NullPointerExeption Error Smithing Script

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
	}

 

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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"

Share this post


Link to post
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;
	}

}

 

Share this post


Link to post
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.

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.

Sign in to follow this  

×
×
  • Create New...