Jump to content

Anyone know why this might throw an NPE?


adc

Recommended Posts

public void sleepFor(int min, int max) throws InterruptedException,NullPointerException {
    sA.sleep(MethodProvider.random(min, max));
}

Not sure why you have this throwing a NullPointerException?

 

Also sleep is a static method, so just call it like MethodProvider.sleep(int);

Just do this instead of having it throw a InterruptedExeption; not sure the exact situation you need this for, but this should work for you:

public void sleepFor(int min, int max) {
     try {
          MethodProvider.sleep(MethodProvider.random(min, max));
     } catch (InterruptedException e) {
          //Catch method
     }
}
Edited by NotoriousPP
Link to comment
Share on other sites

You probably never instantiated sA

 

Correct me if I'm wrong, but it's definitely instantiated here, isn't it? Which is why I'm so confused as to why it would be at the top of a stack trace for an NPE :(

package utils;

import org.osbot.script.MethodProvider;
import org.osbot.script.Script;

public class MiscMethods {

    private Script sA;
    public MiscMethods(Script sA) {
        this.sA = sA;
    }

    public void sleepFor(int min, int max) throws InterruptedException,NullPointerException {
    sA.sleep(MethodProvider.random(min, max));
    }
    [...]

Also, I've gone ahead and replaced every usage of my custom sleep method with 

sA.sleep(MethodHandler.random(a,b));

So everything works, but it would be cool if someone could show me why my method isn't working so that I don't have unnecessary spam

Link to comment
Share on other sites

Correct me if I'm wrong, but it's definitely instantiated here, isn't it? Which is why I'm so confused as to why it would be at the top of a stack trace for an NPE sad.png

package utils;

import org.osbot.script.MethodProvider;
import org.osbot.script.Script;

public class MiscMethods {

    private Script sA;
    public MiscMethods(Script sA) {
        this.sA = sA;
    }

    public void sleepFor(int min, int max) throws InterruptedException,NullPointerException {
    sA.sleep(MethodProvider.random(min, max));
    }
    [...]

Also, I've gone ahead and replaced every usage of my custom sleep method with 

sA.sleep(MethodHandler.random(a,b));

So everything works, but it would be cool if someone could show me why my method isn't working so that I don't have unnecessary spam

 

How did you instantiate MiscMethods?

Link to comment
Share on other sites

How did you instantiate MiscMethods?

 

 

MiscMethods is instantiated in the exact same way in every class that I need to use any of the methods from it. For example:

public class StopScript extends Task {
    private MiscMethods m = new MiscMethods(sA);
   
    public StopScript(Script sA) {
        super(sA);
    }
    public boolean execute() throws InterruptedException {
        if(m.isPoisoned()) {
        [...]
        }
        m.curePoison();
        m.stopScript();
}

And literally every other method in MiscMethods operates in the same fashion as sleepFor, only they don't throw any errors:

public boolean isPoisoned() {
        return sA.client.getConfig(102) > 0;
    }

    public boolean curePoison() throws InterruptedException {
        if(isPoisoned()) {
            if (sA.client.getInventory().contains("Antipoison(1)")) {
                sA.client.getInventory().interactWithName("Antipoison(1)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            } else if (sA.client.getInventory().contains("Antipoison(2)")) {
                sA.client.getInventory().interactWithName("Antipoison(2)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            } else if (sA.client.getInventory().contains("Antipoison(3)")) {
                sA.client.getInventory().interactWithName("Antipoison(3)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            } else if (sA.client.getInventory().contains("Antipoison(4)")) {
                sA.client.getInventory().interactWithName("Antipoison(4)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            }
        }
        return false;
    }
Edited by adc
Link to comment
Share on other sites

 

MiscMethods is instantiated in the exact same way in every class that I need to use any of the methods from it. For example:

public class StopScript extends Task {
    private MiscMethods m = new MiscMethods(sA);
   
    public StopScript(Script sA) {
        super(sA);
    }
    public boolean execute() throws InterruptedException {
        if(m.isPoisoned()) {
        [...]
        }
        m.curePoison();
        m.stopScript();
}

And literally every other method in MiscMethods operates in the same fashion as sleepFor, only they don't throw any errors:

public boolean isPoisoned() {
        return sA.client.getConfig(102) > 0;
    }

    public boolean curePoison() throws InterruptedException {
        if(isPoisoned()) {
            if (sA.client.getInventory().contains("Antipoison(1)")) {
                sA.client.getInventory().interactWithName("Antipoison(1)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            } else if (sA.client.getInventory().contains("Antipoison(2)")) {
                sA.client.getInventory().interactWithName("Antipoison(2)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            } else if (sA.client.getInventory().contains("Antipoison(3)")) {
                sA.client.getInventory().interactWithName("Antipoison(3)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            } else if (sA.client.getInventory().contains("Antipoison(4)")) {
                sA.client.getInventory().interactWithName("Antipoison(4)", "Drink");
               sA.sleep(MethodProvider.random(300,500));
                return true;
            }
        }
        return false;
    }

 

I'm surprised that the other methods work. They should all be throwing NPE.

 

Reason:

public class StopScript extends Task {
    private MiscMethods m = new MiscMethods(sA);     < -- you initialize m here
   
    public StopScript(Script sA) {
        super(sA);                                   < -- you set sA here.
    }
    public boolean execute() throws InterruptedException {
        if(m.isPoisoned()) {
        [...]
        }
        m.curePoison();
        m.stopScript();
}

BUT, in Java, m = new MiscMethods(sA) would be run before the constructor is called. Therefore, you initialize MiscMethods with sA = null, and then the constructor sets sA.

 

For example:


public class Time {
	public long time1 = System.nanoTime();
	public long time2;
	
	public Time(){
		time2 = System.nanoTime();
	}
	
	public void print(){
		System.out.println(time1 < time2);
	}
}

This will print true, indicating that time1 is initialized before the constructor is even called.

  • Like 1
Link to comment
Share on other sites

I'm surprised that the other methods work. They should all be throwing NPE.

 

Reason:

public class StopScript extends Task {
    private MiscMethods m = new MiscMethods(sA);     < -- you initialize m here
   
    public StopScript(Script sA) {
        super(sA);                                   < -- you set sA here.
    }
    public boolean execute() throws InterruptedException {
        if(m.isPoisoned()) {
        [...]
        }
        m.curePoison();
        m.stopScript();
}

BUT, in Java, m = new MiscMethods(sA) would be run before the constructor is called. Therefore, you initialize MiscMethods with sA = null, and then the constructor sets sA.

 

 

Having started with Actionscript 3 (based on Java), I had enough experience to know this-- until, like you, I was surprised to find that it actually worked when done in that order. However, because it did work I assumed it was fine. Thanks for the help :)

Link to comment
Share on other sites

Having started with Actionscript 3 (based on Java), I had enough experience to know this-- until, like you, I was surprised to find that it actually worked when done in that order. However, because it did work I assumed it was fine. Thanks for the help smile.png

 

Your welocme :) but I totally did not understand the rest of your response... Was that the problem or not lol

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