Jump to content

Need help with classes, Java [class included]


Satire

Recommended Posts

Ok before anyone laughs, I'm new to Java and I seriously have no idea what's wrong. This should work, works very well in c#. 


I have a class here

http://pastebin.com/UPUdfhdx

And I wanted to call the function by making a new instance of that class 

MakeMoneyHandler moneyhandler = new MakeMoneyHandler();
moneyhandler.Mining(0);

However, when I run it, I get this
 

[ERROR][bot #1][12/13 03:50:13 PM]: Error in script executor!
java.lang.NullPointerException
at org.osbot.rs07.script.MethodProvider.log(lp:841)
at MakeMoneyHandler.Mining(MakeMoneyHandler.java:22)
at main.onLoop(main.java:824)
at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(qi:31)
at java.lang.Thread.run(Unknown Source)


There is nothing wrong with the code as I copied it to main and it all worked 100% fine.

Don't worry about the code itself and say "this check is wrong", I know there are some errors and I'm re-writing it as I post this. I just wanted a seperate class that handles my functions. Also don't worry about the while loop, It was there for another test.  


 
Edited by lol0
Link to comment
Share on other sites

 

Ok before anyone laughs, I'm new to Java and I seriously have no idea what's wrong. This should work, works very well in c#. 

I have a class here

http://pastebin.com/UPUdfhdx

And I wanted to call the function by making a new instance of that class 

MakeMoneyHandler moneyhandler = new MakeMoneyHandler();
moneyhandler.Mining(0);

However, when I run it, I get this

 

[ERROR][bot #1][12/13 03:50:13 PM]: Error in script executor!
java.lang.NullPointerException
at org.osbot.rs07.script.MethodProvider.log(lp:841)
at MakeMoneyHandler.Mining(MakeMoneyHandler.java:22)
at main.onLoop(main.java:824)
at org.osbot.rs07.event.ScriptExecutor$InternalExecutor.run(qi:31)
at java.lang.Thread.run(Unknown Source)

There is nothing wrong with the code as I copied it to main and it all worked 100% fine.

Don't worry about the code itself and say "this check is wrong", I know there are some errors and I'm re-writing it as I post this. I just wanted a seperate class that handles my functions. Also don't worry about the while loop, It was there for another test.  

 

 

public class MakeMoneyHandler {
     private MethodProvider ctx;
     
     public MakeMoneyHandler(MethodProvider ctx){
          this.ctx = ctx;
     }

     public void killShit(){
       ctx.log("I killed the game homie");
     }

}

public Main extends Script{
   private MakeMoneyHandler = new MakeMoneyHandler(this);
}
Edited by Chris
Link to comment
Share on other sites

public class MakeMoneyHandler {
     private MethodProvider ctx;
     
     public MakeMoneyHandler(MethodProvider ctx){
          this.ctx = ctx;
     }

     public void killShit(){
       ctx.log("I killed the game homie");
     }

}

public Main extends Script{
   private MakeMoneyHandler = new MakeMoneyHandler(this);
}

You legend! You just saved main from going to 3k+ lines. 

Link to comment
Share on other sites

You legend! You just saved main from going to 3k+ lines. 

 

Or you could also create a separate class instead of using nested classes. I would look into a task based system if you want to be more organized. There's several tutorials on the site about how to do this.

 

Basically each 'task' has its own class and then in your main class you just add a new instance of each task to the task manager, which loops through each task and executes it if it should be active, which is determined by a method inside each task class, usually isActive() which would return a boolean depending on the condition you place within said method.

 

Then you can have a task / separate class for banking per say, and another for combat, etc.

 

in Main.java:

@[member='Override']
	public void onStart() {
			Settings settings = new Settings(this);
			settings.setCallback(b -> {
				addTask(new SwapTask(this));
				addTask(new TradeTask(this));
				});
	}

in TradeTask.java for example:

package com.loudpacks.script;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.script.MethodProvider;
import org.osbot.rs07.utility.ConditionalSleep;

import com.loudpacks.api.Task;
import com.loudpacks.gui.Settings;

public class TradeTask extends Task {
	
	private final Area RUINS = new Area(2386, 4829, 2415, 4855);
	private final Area INNER_RUINS = new Area(2404, 4837, 2395, 4845);
	private final Area PORTAL_AREA = new Area(2402, 4833, 2397, 4836);
	
	public TradeTask(MethodProvider api){
		super(api);
	}

	@[member=Override]
	public boolean isActive() {
		return RUINS.contains(api.myPlayer());
	}

	@[member=Override]
	public void onStart() {
	

	}

	@[member=Override]
	public void onLoop() {
// code to execute when active
	}

	@[member=Override]
	public void onEnd() {
	

	}

}

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

Or you could also create a separate class instead of using nested classes. I would look into a task based system if you want to be more organized. There's several tutorials on the site about how to do this.

 

Basically each 'task' has its own class and then in your main class you just add a new instance of each task to the task manager, which loops through each task and executes it if it should be active, which is determined by a method inside each task class, usually isActive() which would return a boolean depending on the condition you place within said method.

 

Then you can have a task / separate class for banking per say, and another for combat, etc.

 

I'll take a look into this then. Damn this is gonna take a while to fix up. 

Thank you!

Edited by lol0
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...