Jump to content

Java.lang.NullpointerException


pooki123

Recommended Posts

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)

}

}
Link to comment
Share on other sites

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
}
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...