Getting this: 'IllegalArgumentException: Plugin cannot be null' on PlayerJoinEvent!

Discussion in 'Plugin Development' started by SpongyBacon, Jun 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    SpongyBacon

    Hey :3
    I haven't been on here in a while… so here's my current issue: When I login to my server, I get an error, and then the parts of my plugin that are executed on player join don't work.

    StackTrace:
    Code:
    [18:14:13] [Server thread/ERROR]: Could not pass event PlayerJoinEvent to Mashup v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PlayerList.c(PlayerList.java:251) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.PlayerList.a(PlayerList.java:138) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.LoginListener.c(LoginListener.java:76) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.LoginListener.a(LoginListener.java:42) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:160) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:391) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.scheduleSyncDelayedTask(CraftScheduler.java:100) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    at com.SpongyBacon.Mashup.Utils.Fireworks.random(Fireworks.java:115) ~[?:?]
    at com.SpongyBacon.Mashup.Events.Join.onJoin(Join.java:33) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-b3084jnks]
    ... 14 more
    [18:14:13] [Server thread/INFO]: SpongyBacon[/192.168.0.2:59909] logged in with entity id 101 at ([Mashup] -116.96449807126619, 87.0, 2388.066809246098)
    Join class:
    Code:
    package com.SpongyBacon.Mashup.Events;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    import com.SpongyBacon.Mashup.Main;
    import com.SpongyBacon.Mashup.SB;
    import com.SpongyBacon.Mashup.Skills.Level;
    import com.SpongyBacon.Mashup.Utils.Configuration;
    import com.SpongyBacon.Mashup.Utils.Fireworks;
     
    public class Join implements Listener {
     
    public static Main plugin;
     
    public Join(Main instance) {
    plugin = instance;
    }
     
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    Player p = (Player) e.getPlayer();
    FileConfiguration config = plugin.getConfig();
     
    // Set the custom join message - if the player is new, do this
    if (config.get(p.getUniqueId().toString()) == null) {
    // Spawns a random firework at new players location
    Fireworks.random(p.getLocation());
     
    e.setJoinMessage(ChatColor.YELLOW + "[+] Welcome " + ChatColor.GOLD
    + p.getName() + ChatColor.YELLOW + " to " + ChatColor.RED
    + "Project Mashup " + ChatColor.YELLOW + "!");
    } else { // Otherwise, do this
    e.setJoinMessage(ChatColor.DARK_GRAY + "[+] Welcome back to "
    + ChatColor.DARK_AQUA + "Project Mashup"
    + ChatColor.DARK_GRAY + ", " + ChatColor.DARK_AQUA
    + p.getName() + ChatColor.DARK_GRAY + "!");
    }
     
    // Check if all the configuration files are generated
    Configuration.check(p);
     
    // Set the players displayName
    if (config.getString(p.getUniqueId().toString() + ".nick")
    .equalsIgnoreCase("none")) {
    p.setDisplayName(ChatColor.RED + "[Lvl " + Level.calculate(p) + "] "
    + ChatColor.RESET + p.getName());
    } else {
    String nick = config
    .getString(p.getUniqueId().toString() + ".nick")
    .replaceAll("(&([a-f0-9]))", "\u00A7$2");
     
    p.setDisplayName(ChatColor.RED + "[Lvl " + Level.calculate(p) + "] "
    + ChatColor.RESET + "" + nick);
    }
     
    // Gives/updates scoreboard for all players on the server
    for (Player pl : Bukkit.getOnlinePlayers()) {
    SB.update(pl);
    }
     
    }
     
    }
    
    Line 33 of the join class (as specified by the stacktrace):
    Code:
    Fireworks.random(p.getLocation());
    Fireworks class: This class spawns fireworks, and the method 'random' spawns a completely random firework at the specified location!
    Code:
    package com.SpongyBacon.Mashup.Utils;
     
    import java.util.Random;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.Location;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Firework;
    import org.bukkit.inventory.meta.FireworkMeta;
     
    import com.SpongyBacon.Mashup.Main;
     
    public class Fireworks {
     
    public static Main plugin;
     
    public Fireworks(Main instance) {
    plugin = instance;
    }
     
    // Getting the random colour for the firework
    private static Color getColor(int i) {
    Color c = null;
     
    if (i == 1) {
    c = Color.AQUA;
    }
    if (i == 2) {
    c = Color.BLACK;
    }
    if (i == 3) {
    c = Color.BLUE;
    }
    if (i == 4) {
    c = Color.FUCHSIA;
    }
    if (i == 5) {
    c = Color.GRAY;
    }
    if (i == 6) {
    c = Color.GREEN;
    }
    if (i == 7) {
    c = Color.LIME;
    }
    if (i == 8) {
    c = Color.MAROON;
    }
    if (i == 9) {
    c = Color.NAVY;
    }
    if (i == 10) {
    c = Color.OLIVE;
    }
    if (i == 11) {
    c = Color.ORANGE;
    }
    if (i == 12) {
    c = Color.PURPLE;
    }
    if (i == 13) {
    c = Color.RED;
    }
    if (i == 14) {
    c = Color.SILVER;
    }
    if (i == 15) {
    c = Color.TEAL;
    }
    if (i == 16) {
    c = Color.WHITE;
    }
    if (i == 17) {
    c = Color.YELLOW;
    }
     
    return c;
    }
     
    public static void random(Location loc) {
    final Firework fw = (Firework) loc.getWorld().spawnEntity(loc,
    EntityType.FIREWORK);
    FireworkMeta fwm = fw.getFireworkMeta();
     
    Random r = new Random();
    int rt = r.nextInt(5) + 1;
     
    Type type = Type.BALL;
    if (rt == 1)
    type = Type.BALL;
    if (rt == 2)
    type = Type.BALL_LARGE;
    if (rt == 3)
    type = Type.BURST;
    if (rt == 4)
    type = Type.CREEPER;
    if (rt == 5)
    type = Type.STAR;
     
    int r1i = r.nextInt(17) + 1;
    int r2i = r.nextInt(17) + 1;
    Color c1 = getColor(r1i);
    Color c2 = getColor(r2i);
     
    // Random firework effects
    FireworkEffect effect = FireworkEffect.builder()
    .flicker(r.nextBoolean()).withColor(c1).withFade(c2).with(type)
    .trail(r.nextBoolean()).build();
    fwm.addEffect(effect);
     
    // Random power amount
    int rp = r.nextInt(2) + 1;
    fwm.setPower(rp);
     
    // Setting the meta to all the info above
    fw.setFireworkMeta(fwm);
     
    // Random detonation delay
    int delay = r.nextInt(40) + 1;
    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    public void run() {
    fw.detonate();
    }
    }, delay);
    }
     
    }
    
    Line 115 of the fireworks class:
    Code:
    int rp = r.nextInt(2) + 1;
    Hope you can help :3 If you need to see any more classes, just ask!
     
  2. Offline

    fireblast709

    SpongyBacon
    Step 1) remove all the static keywords you used.
    Step 2) fix the error you created WITHOUT using static
    Then you probably fixed the error. This is probably a typical case of using static without knowing what it does or how it acts
     
  3. Offline

    SpongyBacon

    I know exactly how to use static, and it's really needed with my plugin.
    How would my join event trigger a firework from the random class without it?
     
  4. Offline

    fireblast709

    SpongyBacon The second part of that post contradicts the first. In most cases (read as: mostly all cases) there is no need of static, but more lacking the experience with Java to fix it properly.
     
  5. Offline

    SpongyBacon

    Right... so with your expertise, would you please explain how to fix it then?
     
  6. Offline

    xTigerRebornx

    SpongyBacon He has already explained how. Remove the use of statics, then reimplement the code that previously used statics using a non-static context
     
  7. Offline

    Tirelessly

    SpongyBacon You never initialized the variable plugin in the Firework class.
     
  8. Offline

    SpongyBacon

    Sorry, but how would I do that? Would I use a constructor like used to use the main class?

    Does this not count?
    public Join(Main instance) {
    plugin = instance;
    }

    Or would I need to do publicstatic Main plugin = null; or something? I've never had a problem with this method before :O

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  9. Offline

    xTigerRebornx

    SpongyBacon Make it so that your Fireworks class isn't just a bunch of static methods. The way you currently have it, it depends on that 'plugin' variable (which is defined through a constructor) to call your random() method, but you use it in a static context, therefor that constructor may never be called. Instead of using a bunch of static methods, make it work so that you can access an instance of Fireworks that has the plugin variable properly defined.
     
  10. Offline

    SpongyBacon

    xTigerRebornx Alright, so I removed all the static's from the fireworks class, and then added 'public Fireworks fw;' to my join class, and added that into the constructor so it looks like this:
    Code:
    public static Main plugin;
        public Fireworks fw;
     
        public Join(Main instance, Fireworks fire) {
            plugin = instance;
            fw = fire;
        }
     
  11. Offline

    Tirelessly

    SpongyBacon That isn't your firework class, so no.
     
  12. Offline

    xTigerRebornx

    Tirelessly If he removed the statics and passes in an instance of Fireworks that has a valid plugin variable, then it should be fine. The only real problem I see is the static modifier on his 'plugin' variable.
    SpongyBacon Why do you make 'plugin' static? There is no need for it to be static, and there is generally a OOP-friendly way to accomplish what you want. Use static only when absolutely needed.
     
  13. Offline

    Tirelessly

    xTigerRebornx That isn't the problem, it's just bad practice. He just needs to initialize it.
     
  14. Offline

    JBoss925

    I've literally seen that firework code in so many places it's impossible to count. Everyone is taking credit for that guy's work.
     
  15. Offline

    Garris0n

    Which is a problem. It's not the problem OP made the thread about, but it's something that should not be allowed to continue further. Sometimes, somebody will have an error I could easily just tell them how to fix, however I might notice that their code doesn't follow the proper naming conventions. In that case, I would tell them to fix their code first before getting in to what their "actual" problem is, because getting into the habit of using bad naming conventions (or other bad practices) is very problematic later on.

    @OP Despite what you may think, static is not the solution to all of your problems. It may seem like that at first, but the way you are using it is not the correct way and goes against the concept of object-oriented programming.

    Also, package names shouldn't have uppercase letters in them. Perhaps you should read this.
     
  16. Offline

    xTigerRebornx

    Tirelessly It may not be his exact problem, but its what caused his problem to occur. The overuse of statics in his code just makes him think he can use it whenever, which should never be the case. His code required access to a non-static variable, but his methods are static. He didn't correctly understand what static is, and how it should be used, so he just did what his IDE said and made the 'plugin' variable in Fireworks static, thinking it would fix his problems. It clearly didn't, which is why he is here.
     
  17. Offline

    SpongyBacon

    xTigerRebornx Garris0n Tirelessly Sorry for the late responce- I wasn't on my laptop for a few days. I've read through everything you've said here, and i'm going to see if it fixes my problems :3

    JBoss925 Eh, kinda hard to give the guy credit when the plugin isn't public. I'll probably add him into the credits thingy on my site once I release the server.
     
Thread Status:
Not open for further replies.

Share This Page