简体   繁体   中英

How to call a non static method from a class?

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

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()

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;
        }
    }
}

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.

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(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM