Jump to content

Node Tutorial - NullPointerException


sesamest

Recommended Posts

MainClass:

import core.*;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nodes.*;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;


@ScriptManifest(author = "sesamest", info = "Version 0.02", name = "Cow Killer/Hide Grabber", version = 0)
public class MainClass extends Script {

        private String status = "";
        private List<Node> nodes = new ArrayList<>();
	
	@Override
	public void onStart() {
            nodes.add(new Banking(this));
	}
	
	@Override 
	public int onLoop() throws InterruptedException {
            for(Node n : nodes) {
                if(n.validate()) {
                    status = n.status();
                    if(n.execute()) {
                        return random(200,400);
                    }
                }
            }
            return random(200, 310);
	}
	
	@Override
	public void onExit() {
		log("Thanks for using this wonderful script!");
	}

	@Override
	public void onPaint(Graphics g) {
            g.setColor(Color.black);
            g.drawString(status, 6, 6);
            g.setColor(Color.white);
            g.drawString(status, 5, 5);
	}
	
}

Banking Node Class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package nodes;

import core.Constants;
import core.Node;
import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.model.RS2Object;

/**
 *
 * @author LivingRoom
 */
public class Banking extends Node {
    
    private static final int[] BANKOBJECT_ID = { 24101 };
    
    public Banking(Script sA) {
        super(sA);
    }
    
    @Override
    public String status() {
        return "Banking";
    }
    
    private boolean isBoothVisible() {
        return true;
       /* RS2Object booth = sA.closestObject(BANKOBJECT_ID);
        if(booth != null)
            if(booth.getPosition().distance(sA.myPosition()) <= 5)
                return true;
        return false; */
    }

    @Override
    public boolean validate() throws InterruptedException {
        return sA.client.getLocalNPCs().contains("Banker");
    }
    
    public boolean openBank() throws InterruptedException {
        RS2Object booth = sA.closestObject(BANKOBJECT_ID);
        if (booth != null) {
            if(!booth.isVisible()) {
                sA.client.moveCameraToPosition(booth.getPosition());
            }
            if (booth.interact("Bank")) {
                while (!sA.client.getBank().isOpen())
                    sA.sleep(250);
                sA.client.getBank().depositAll();
            }
	}
        return false;
    }

    @Override
    public boolean execute() throws InterruptedException {
        if(sA.client.getInventory().contains(Constants.COWHIDE)){
            if(openBank()) {

            }
        }
        
        return true;
    }
    
}

I believe myself to be relatively capable of programming in Java. It is odd that it keeps throwing a NullPointerException on Line 43 Validate() function.

 

Any advice would be helpful. I believe it is something to do with the Script object being null when parsed to the Banking class.

Link to comment
Share on other sites

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import org.osbot.script.Script;

/**
 *
 * @author LivingRoom
 */
public abstract class Node {
    
    public Script sA;
    
    public Node(Script A) {
        this.sA = sA;
    }
    
    public String status() {
        return "";
    }
    
    public abstract boolean validate() throws InterruptedException;
    public abstract boolean execute() throws InterruptedException;
}

Node class.

 

and by goodness, you guys are incredibly prompt at helping!

 

 

 

EDIT:

 

changed the validate to return this:

return sA.closestObject(BANKOBJECT_ID) != null;

but still causes an NullPointerException

Edited by sesamest
Link to comment
Share on other sites

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import org.osbot.script.Script;

/**
 *
 * @author LivingRoom
 */
public abstract class Node {
    
    public Script sA;
    
    public Node(Script A) {
        this.sA = sA;
    }
    
    public String status() {
        return "";
    }
    
    public abstract boolean validate() throws InterruptedException;
    public abstract boolean execute() throws InterruptedException;
}

Node class.

 

and by goodness, you guys are incredibly prompt at helping!

 

I have no life, nor friends, nor loved ones.

 

You misspelled sA (what you meant to type) in the Node constructor argument. Right now you're just assigning the instance variable sA a reference to itself which is, of course, null.

Edited by Swizzbeat
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...