0

I have a run() method that runs every second and it has to pass data (such as the current time in seconds since the start of the timer) to a method in a different class 'Spawner'.

Run method:

@Override
public void run() { 
    counter++;
    if(Main.running[0] == true)
    {
        color = "Red";
        spawn = "base";
        world = "world";
        Spawner spawnerObject = new Spawner();
        spawnerObject.loadSpawner(counter, spawn, color, world);
        run[0]++;
        if(run[0] == 30)
        {
            Main.running[0] = false;
            run[0] = 0;
        }
    }
}

I'm not sure but the NPE I get at 'spawnerObject.loadSpawner(counter, spawn, color, world);' can only have its origin at the spawnerObject.

All I want is run that method every second but I can't figure out how to call the non static method from 'Spawner'. This is the method:

public void loadSpawner(Integer counter, String spawner, String color, String world)
{ //code        
}

Please can someone tell me how to invoke that method? My current thing with the spawnerObject is probably terribly wrong.

Error/Stacktrace:

[10:39:24] [Server thread/WARN]: [ColorKeyBattle] Task #5 for ColorKeyBattle v1.0 generated an exception
java.lang.NullPointerException
    at org.Ziron5.main.Spawner.loadSpawner(Spawner.java:79) ~[?:?]
    at org.Ziron5.main.TimerClass.run(TimerClass.java:59) ~[?:?]
    at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftTask.run(CraftTask.java:71) ~[start.jar:git-Spigot-952179b-43207df]
    at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [start.jar:git-Spigot-952179b-43207df]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:698) [start.jar:git-Spigot-952179b-43207df]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [start.jar:git-Spigot-952179b-43207df]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [start.jar:git-Spigot-952179b-43207df]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [start.jar:git-Spigot-952179b-43207df]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_40]

Complete loadSpawner() Method:

It's quite a long method so I put it on pastebin: http://pastebin.com/iDNDSQQc

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Ziron5
  • 1
  • 2
  • Is `Spawner` part of the bukkit API or is it a class you created yourself? If you created the class youself you can use Kameron's answer. Else I don't see what could have caused the NPE maybe try posting the stacktrace aswell. – Roan Mar 14 '15 at 10:47
  • I edited the stacktrace in – Ziron5 Mar 14 '15 at 11:13
  • Hmm this might be a silly idea but try changing the type of `counter` in your loadSpawner method to `int` instead of `Integer` this is the only thing I see that might go wrong although it shouldn't. – Roan Mar 14 '15 at 11:21
  • 1
    Roan, because of Autoboxing int or Integer should not make a difference. @Ziron5 Please post the actual code of your loadSpawner. That will tell us where the NPE is coming from. – Kameron Mar 14 '15 at 11:56
  • http://pastebin.com/iDNDSQQc This is the whole loadSpawner() method – Ziron5 Mar 14 '15 at 12:15
  • @Ziron5 are you sure that in your spawner class plugin is not null? Maybe try checking that as that is probably what causes the NPE at line 79 – Roan Mar 15 '15 at 14:24

4 Answers4

0

Maybe I am a bit confused, but you have loadSpawner as an instance method, and you want it to be accessible from the class right, or usable without creating the object spawnerObject?

Try changing it to

public static void loadSpawner(Integer counter, String spawner, String color, String world)
{ 
//code        
}

Then you could call it with

Spawner.loadSpawner()
spongebob
  • 8,370
  • 15
  • 50
  • 83
Kameron
  • 117
  • 1
  • 11
  • Did that and still have the NPE at Spawner.loadSpawner(args) what could possibly be null in there? – Ziron5 Mar 14 '15 at 10:59
  • We need to see the actual code for loadSpawner. That is where the exception is originating. //code does not quite cut it... – Kameron Mar 14 '15 at 11:58
0

Dont Create Object for Spawner again and again in run this will make new instance of spawner class everytime you go inside run(). use

public static Spawner spawnerObject = new Spawner();

out side run() like this

public static Spawner spawnerObject = new Spawner();
   @Override
public void run() { 
    counter++;
    if(Main.running[0] == true)
    {
        color = "Red";
        spawn = "base";
        world = "world";
        spawnerObject.loadSpawner(counter, spawn, color, world);
        run[0]++;
        if(run[0] == 30)
        {
            Main.running[0] = false;
            run[0] = 0;
        }
    }
}
Umesh Chauhan
  • 1,460
  • 1
  • 17
  • 26
  • Maybe i don't understand it, but isn't static meant so you don't have to instantiate an object. `Spawner.loadSpawner();` being static can be called directly like this. Like the other answer suggests. – WonderWorld Mar 14 '15 at 10:50
0

The stacktrace says that exception is thrown at line 79 of class Spawner, which is part of the loadSpawner method.

if(plugin.getLoc(world, "Basespawners", color, "loc1", "Y") < plugin.getLoc(world, "Basespawners", color, "loc2", "Y"))

Any variable above may be null at this time. Debug your code.

System.out.println(plugin);
System.out.println(world);
System.out.println(color);

To avoid asking such kind of question again, I suggest you to learn how to read stacktraces by yourself.

Community
  • 1
  • 1
spongebob
  • 8,370
  • 15
  • 50
  • 83
0

Usually I do this (put this in the Spawner class) :

public static Spawner spawn;

public Spawner getSpawner() {
    if(spawn == null) {
        spawn = new Spawner();
    }
return spawn;
}

Now you can do:

Spawner.getSpawner().loadSpawner(...);