Jump to content

Null Pointer Problem


Swhit

Recommended Posts

Hello all,

 

I am trying to make a simple script that will walk to a rockfall, mine it, then walk back to the bank chest in motherlode mining. My code so far is as follows:  

 

 

import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.utility.Area;
import org.osbot.rs07.api.Map;
import org.osbot.rs07.input.mouse.MiniMapTileDestination;
import org.osbot.rs07.api.Mouse;

import java.awt.*;

@ScriptManifest(author = "Swhit", info = "Runs to rockfall, mines it, then runs back to bank",
name = "Rock Runner", version = 0.1, logo = "")

@SuppressWarnings("unused")

/*Runs to rockfall, mines it, then runs back to bank */

public class main extends Script {

private enum State {
MINE, WALK_TO_ROCK, WALK_TO_BANK, BANK
};

// private static final int[] VEIN_ID = {
// //mine these!
// 26661, 26664, 26662, 26663
// }; //these are the vein IDs

private static final int[] ROCKFALL_ID = {
//rockfall ID nums
26679, 26680
};

boolean deposited;
boolean mined;

//rectangle in front of motherlode mining bank
private static final Area BANK_AREA = new Area(3757, 5670, 3760, 5664);
private static final Area MINE_AREA = new Area(3759, 5653, 3763, 5651);

private Position[] path = {
new Position(3760, 5666, 0), //bank
new Position(3762, 5653, 0), //near rockfall
new Position(3761, 5653, 0) //testing purposes only

};

@Override
public void onStart() {
log("Let's get started!"); //logs to console below window
//This loop is only one once when starting code.
//use to initialize timers, variables, etc.
deposited = false;
mined = false;

}

private State getState() {
// if (inventory.isFull() && MINE_AREA.contains(myPlayer() ) )
//example of working if ^
if (BANK_AREA.contains(myPlayer() ) && !deposited) {
return State.BANK;
}
else if (BANK_AREA.contains(myPlayer() ) && deposited) {
return State.WALK_TO_ROCK;
}
else if (MINE_AREA.contains(myPlayer() ) && !mined) {
return State.MINE;
}

return State.WALK_TO_BANK;
}

private void traversePath(Position[] path, boolean reversed) throws InterruptedException {
if (!reversed) {
for (int i = 1; i < path.length; i++) {
if (!walkTile(path)) {
i--;
}
}
} else {
//reversed = true
for (int i = path.length-2; i > 0; i--) {
if (!walkTile(path)) {
i++;
}
}
}

}

private boolean walkTile(Position p) throws InterruptedException {
Mouse clicker = new Mouse();
clicker.move(new MiniMapTileDestination(bot, p));
sleep(random(200,300));
clicker.click(false); //false = left click

int failsafe = 0;
while (failsafe < 10 && myPlayer().getPosition().distance(p) > 3) {
sleep (300);
failsafe++;
if (myPlayer().isMoving() )
failsafe = 0;
} //while
if (failsafe == 10)
return false;
return true;
}



@Override
public int onLoop() throws InterruptedException {
switch (getState() ) {
case MINE:
log("case: MINE");
if (!myPlayer().isAnimating() ) {
RS2Object rFall = objects.closest(ROCKFALL_ID);
if (rFall != null) {
rFall.interact("Mine");
sleep( random( 1000, 1500 ) );
log("rock was mined");
//check if the rock was mined??
mined = true;
}

// rFall = objects.closest(ROCKFALL_ID);
// if (rFall != null) {
// mined = true;
// }
}
break;
case WALK_TO_BANK:
log("case: WALK_TO_BANK");
traversePath(path, true);
sleep( random( 1500, 2500 ) );

//resetting mined variable
if (!MINE_AREA.contains(myPlayer() ) && mined) {
log("MINE AREA DOES NOT CONTAIN PLAYER");
mined = false;
}

//inventory.dropAll();
break;
case WALK_TO_ROCK:
log("case: WALK TO ROCK");
traversePath(path, false);
sleep( random( 1500, 2500) );

//resetting deposited to false
if (!BANK_AREA.contains(myPlayer() ) && deposited) {
log("BANK AREA DOES NOT CONTAIN PLAYER");
deposited = false;
}

break;
case BANK:
log("case: BANK");
//banking
RS2Object bankBooth = objects.closest("Bank chest");
if (bankBooth != null) {
if (bankBooth.interact("Use")) {
log("banking");
while(!bank.isOpen() ) {
sleep(250);
}
bank.depositAllExcept("Dragon pickaxe");
deposited = true;
}
}

break;

} //switch
return random(200, 300); //delay
}

@Override
public void onExit() {
log("Thanks for running my script, ya hobo.");
//only one once, when user ends script. Used commonly to
//log details of how script went. For example, how many
//teas you stole... etc.
}

@Override
public void onPaint(Graphics2D g) {
//used to display writing and info to screen.
}

}

 

The error I am getting occurs while banking and gives the following print out:

[iNFO][bot #1][05/13 08:13:41 PM]: case: WALK TO ROCK

[ERROR][bot #1][05/13 08:13:41 PM]: Error in script executor!
java.lang.NullPointerException
at org.osbot.rs07.script.MethodProvider.execute(kc:642)
at org.osbot.rs07.api.Mouse.move(no:14)
at org.osbot.rs07.api.Mouse.move(no)
at main.walkTile(main.java:96)
at main.traversePath(main.java:79)
at main.onLoop(main.java:150)
at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(qj:191)
at java.lang.Thread.run(Unknown Source)
 

 

 

Any ideas?

Edited by Swhit
Link to comment
Share on other sites

Well, now the indentation is messed up but at least the alternating background line colors are gone. What the heck.


I think the error is in here: 

private boolean walkTile(Position p) throws InterruptedException {
Mouse clicker = new Mouse();
clicker.move(new MiniMapTileDestination(bot, p));
sleep(random(200,300));
clicker.click(false); //false = left click

int failsafe = 0;
while (failsafe < 10 && myPlayer().getPosition().distance(p) > 3) {
sleep (300);
failsafe++;
if (myPlayer().isMoving() )
failsafe = 0;
} //while
if (failsafe == 10)
return false;
return true;
}

Also, I made this script based off a tutorial by /user/Pandemic: LINK The confusion came when trying to implement his movement methods, because I couldn't find some of the functions or entities that he used.

Link to comment
Share on other sites

The problem is here:

Mouse clicker = new Mouse();
clicker.move(new MiniMapTileDestination(bot, p));
sleep(random(200,300));
clicker.click(false); //false = left click

You cannot instantiate your own instances of API classes like that without exchanging bot contexts with it.

What you simply need to do (in the context of the Script class, like in your case here) is use getMouse() instead, as Script already inherits from MethodProvider and has pre-initialized instances of all the API classes.

 

so it should just be:

getMouse().move(new MiniMapTileDestination(bot, p));
sleep(random(200,300));
getMouse().click(false); //false = left click
  • Like 1
Link to comment
Share on other sites

 

The problem is here:

Mouse clicker = new Mouse();
clicker.move(new MiniMapTileDestination(bot, p));
sleep(random(200,300));
clicker.click(false); //false = left click

You cannot instantiate your own instances of API classes like that without exchanging bot contexts with it.

What you simply need to do (in the context of the Script class, like in your case here) is use getMouse() instead, as Script already inherits from MethodProvider and has pre-initialized instances of all the API classes.

 

so it should just be:

getMouse().move(new MiniMapTileDestination(bot, p));
sleep(random(200,300));
getMouse().click(false); //false = left click

Ahh, very cool. Thanks!

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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.

  • Recently Browsing   0 members

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