Jump to content

Creating classes..


Recommended Posts

Posted (edited)

Hi everyone, sorry for the noob question. But I'm simply trying to get other classes to work from the main class. I can't figure out what I'm doing wrong.. Once the code runs it thows an error when trying to send a log in the Tester class.

 

Main:

	public Tester g = new Tester();
	public String stateT;

	@Override
	public void onStart() {
		log("test1");
	}

	private enum State {
		TEST, WAIT
	};

	private State getState() {
		if (1 == 1)
			return State.TEST;
		return State.WAIT;
	}

	@Override
	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case TEST:
			log("test2");
			g.logTest();
		case WAIT:
			break;
		}
		return random(200, 300);
	}

	@Override
	public void onExit() {
		log("");
	}

	@Override
	public void onPaint(Graphics2D g) {
	}
}

 

Tester Class:

public class Tester {
	private MethodProvider ggg;

	public Tester(MethodProvider ggg) {
	          this.ggg = ggg;
	     }

	public Tester() {
	}
  
	public void logTest() {
		ggg.log("test222222222222");
		NPC rGuide = ggg.npcs.closest("RuneScape Guide");
		ggg.log(rGuide);
	}
}

 

Thanks in advance for any help!

Edited by Mushphang
Posted (edited)

@Mushphang you are not passing the MethodProvider parameter to your Tester class, you are calling the empty constructor. This means that when you call logTest, a NullPointerException will be thrown as the ggg variable is null.

You should initialize your Tester instance in onStart instead, and pass a reference to a MethodProvider instance. As your main class extends MethodProvider, you can pass "this" as the parameter.

Edited by Explv
  • Like 3
Posted (edited)
15 minutes ago, Explv said:

You should initialize your Tester instance in onStart instead, and pass a reference to MethodProvider.

so something like this?

 

	public void onStart() {
		log("test1");
		Tester g = new Tester(this);
	}

edit: oh didn't see your edit, but that did it! much appreciated!

I declared the public Tester Test = new Tester(this); at a global level versus on start, not sure if that's an issue there at all.

 

Edited by Mushphang
Posted
9 minutes ago, Mushphang said:

so something like this?

 


	public void onStart() {
		log("test1");
		Tester g = new Tester(this);
	}

 

Well you will still need to store the variable globally, otherwise you won't be able to access it outside of the onStart method.

Declare the variable globally, but initialise it in onStart.

Posted
7 minutes ago, Mushphang said:

so something like this?

 


	public void onStart() {
		log("test1");
		Tester g = new Tester(this);
	}

 

Tester.java:

public class Tester {

  private final Script script;

  public Tester(Script script) {
      this.script = script;
  }

  public void Print(String message) {
      script.log(message);
  }

}

Your script main

public void onStart() {
  Tester tester = new Tester(this)
  tester.print("hello");
}

Wrote it in the reply box so sorry if there are any typos or if I missed anything. Hopefully that cleared things up a bit!

-Apa

Posted
3 minutes ago, Explv said:

Well you will still need to store the variable globally, otherwise you won't be able to access it outside of the onStart method.

Declare the variable globally, but initialise it in onStart.

That did it!

 

Thank you so much for your help, you have no idea how much confusion you cleared up.

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