Jump to content

Java.lang.NullpointerException


Recommended Posts

Posted

I am making a Chickenkiller script for educational purpose. it was running okay until i got the error in the screenshot:image.png.69205079d45fd803ad2bcd4876fb196e.png

I just dont see what im doing wrong. on google i found the following: "The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable. So you have a reference to something that does not actually exist." i dont understand what it means can sombody help?

The code i was using:

import org.osbot.rs07.api.model.NPC;

import org.osbot.rs07.script.Script;

import org.osbot.rs07.script.ScriptManifest;

import java.awt.*;

@ScriptManifest(name = "Chickenkiller", author = "Snatsbats", version = 1.0, info = "", logo = "")

public class Chickenkiller extends Script {
@Override
public void onStart() {
log("Let's get started");
}

@Override
public int onLoop() throws InterruptedException {
log("loop works");
NPC npcName = getNpcs().closest(npc -> npc.getName().startsWith("Chicken"));
// Area lumbChicken = new Area(
// new int[][]{
// { 3225, 3301 },
// { 3225, 3301 },
// { 3237, 3301 },
// { 3237, 3301 },
// { 3237, 3293 },
// { 3237, 3293 },
// { 3225, 3293 },
// { 3225, 3293 }
// }
// );
// WebWalkEvent webEvent = new WebWalkEvent(lumbChicken);
attackNPC(npcName);
return 750; //The amount of time in milliseconds before the loop starts over
}
public void attackNPC(NPC enemyName){
if(enemyName.isAttackable() && myPlayer().getInteracting() == null){
enemyName.interact("Attack");
}
}
@Override
public void onExit() {

//Code here will execute after the script ends

}

@Override
public void onPaint(Graphics2D g) {

//This is where you will put your code for paint(s)

}

}
Posted
21 hours ago, pooki123 said:

I just dont see what im doing wrong. on google i found the following: "The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable. So you have a reference to something that does not actually exist." i dont understand what it means can sombody help?

The method is returning null so npcName doesn't exist. You need to null check stuff like that.

if (npcName != null) {
	// Call stuff
}

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
  • Recently Browsing   0 members

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