Jump to content

*FIXED* java.lang.NullPointerException -> What does this mean? & How do I fix it?


Mephisto

Recommended Posts

So I wrote a little script and tried to run it. And it gave me this error:

java.lang.NullPointerException
    at org.osbot.rs07.event.InteractionEvent.<init>(zi:86)
    at Thieverclass.onStart(Thieverclass.java:51)
    at org.osbot.rs07.event.ScriptExecutor.IIIIiiiiIIii(bp:284)
    at org.osbot.rs07.event.ScriptExecutor.start(bp:20)
    at org.osbot.lB.IIIIiiiiIIii(ws:46)
    at org.osbot.NB.iIiIiiiIiiiI(yab:177)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

How can I fix this? &  What does this error mean exactly ? 
(when I google it I still don't understand it fully)
 

The script I wrote:

  if (lumbridgeSquare != null){
    if (lumbridgeSquare.contains(myPlayer())){
        log("Player arrived in Lumbridge"); //Arrived in Lumbridge
        MethodProvider.sleep(MethodProvider.random(2000, 3000));
        getWalking().walk(lTRoute_1);
        if (lTrapdoor != null){
            if (lTrapdoor.isVisible()) {
                MethodProvider.sleep(MethodProvider.random(1000, 3000));
                execute(climbdownTrapdoor);
        }
      }
    }
  }

 

Edit: Found the flaw, had something to do with reusing an event. 
 

 

Edited by Mephisto
Link to comment
Share on other sites

whats on line 51 from Thieverclass? 'at Thieverclass.onStart(Thieverclass.java:51)'

looks like you're checking if one thing is not null/visible but then executing an event, are you reusing an event, try making a fresh event before you execute?

Edited by Fruity
  • Like 1
Link to comment
Share on other sites

22 minutes ago, Fruity said:

whats on line 51 from Thieverclass? 'at Thieverclass.onStart(Thieverclass.java:51)'

looks like you're checking if one thing is not null/visible but then executing an event, are you reusing an event, try making a fresh event before you execute?

I was indeed reusing events.

I fixed it now :) 

Thanks  @Fruity & @dormic for the quick replies ! :) 

Link to comment
Share on other sites

You fixed the problem in this particular case but you didn't find the answers to your original questions so you won't be able to debug your code the next time it happens.

Here:

 

4 hours ago, Mephisto said:

What does this error mean exactly ?

A null pointer exception is a type of runtime exception that occurs when a null value was used when an object instance was required. Examples include calling methods or accessing fields of a variable that is null.

By default all reference (non-primitive) variables in Java are null.

Object variableOne = new Object();
Object variableTwo = null;
Object variableThree;

Only the first variable points to an actual object instance. The second and third variables are both pointing to null.

Calling any Object methods from the second or third variables will throw a null pointer exception because the variable is not pointing to a valid Object instance, it's pointing to null.

 

4 hours ago, Mephisto said:

How can I fix this?

You always read the stack trace. The null pointer exception occurred on the first line and all the other lines represent the call stack - all the methods that have been called in the program, which eventually resulted in this error:

 

java.lang.NullPointerException
    at org.osbot.rs07.event.InteractionEvent.<init>(zi:86)           < here is where the null pointer exception occurred
    at Thieverclass.onStart(Thieverclass.java:51)                            < here is where you called the method that threw the exception
    at ...
    at ...
    ...
 

Usually you are going to fix the first line, because that is where the exception occurred, but in this case the first line is part of the API, which means that you cannot change it. But you can instead change the code from the second line and make sure that you are not passing a null value to the method from the first line.

  • Heart 1
Link to comment
Share on other sites

32 minutes ago, Script Kid said:

You fixed the problem in this particular case but you didn't find the answers to your original questions so you won't be able to debug your code the next time it happens.

Here:

 

A null pointer exception is a type of runtime exception that occurs when a null value was used when an object instance was required. Examples include calling methods or accessing fields of a variable that is null.

By default all reference (non-primitive) variables in Java are null.


Object variableOne = new Object();
Object variableTwo = null;
Object variableThree;

Only the first variable points to an actual object instance. The second and third variables are both pointing to null.

Calling any Object methods from the second or third variables will throw a null pointer exception because the variable is not pointing to a valid Object instance, it's pointing to null.

 

You always read the stack trace. The null pointer exception occurred on the first line and all the other lines represent the call stack - all the methods that have been called in the program, which eventually resulted in this error:

 

java.lang.NullPointerException
    at org.osbot.rs07.event.InteractionEvent.<init>(zi:86)           < here is where the null pointer exception occurred
    at Thieverclass.onStart(Thieverclass.java:51)                            < here is where you called the method that threw the exception
    at ...
    at ...
    ...
 

Usually you are going to fix the first line, because that is where the exception occurred, but in this case the first line is part of the API, which means that you cannot change it. But you can instead change the code from the second line and make sure that you are not passing a null value to the method from the first line.

Thanks for explaining this, I finally get it now :o  :).

  • Like 1
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...