Jump to content

Creating classes..


Mushphang

Recommended Posts

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
Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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