File cannot be null - It isn't...

Discussion in 'Plugin Development' started by CraftCreeper6, May 28, 2014.

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

    CraftCreeper6

    Hello! So my plugin keeps returning null everytime I right click a sign.
    Basically I:
    1) Create a sign
    2) Syntax:
    # [HP]
    # [ Teleport name ]
    3) Set the coords of the teleport name by doing: /HP set [name] [world] [x] [y] [z]
    4) When they right click the sing they get teleported to the coordinates in the config.

    But, it always returns null :/ By the way. If anyone could tell me how to access a config from a class that does not extend JavaPlugin that would be awesome!

    Code (Main):
    Code:java
    1. package me.CraftCreeper6.main;
    2.  
    3. import me.CraftCreeper6.teleport.Teleportation;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Main extends JavaPlugin {
    12.  
    13. public final Teleportation tp = new Teleportation(this);
    14.  
    15. public void onEnable(){
    16.  
    17. System.out.print("[HubblePort] Enabled!");
    18.  
    19. getServer().getPluginManager().registerEvents(tp, this);
    20.  
    21. }
    22.  
    23. public void onDisable(){
    24.  
    25. System.out.print("[HubblePort] Disabled!");
    26.  
    27. }
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd,
    30. String commandLabel, String[] args) {
    31. Player p = (Player) sender;
    32.  
    33. if(commandLabel.equalsIgnoreCase("HP") && args[0].equalsIgnoreCase("Set") && args[1] !=null && args[2] !=null && args[3] !=null && args[4] !=null && args[5] !=null){
    34.  
    35. getConfig().set(args[1] + ".World", args[2]);
    36. getConfig().set(args[1] + ".X", args[3]);
    37. getConfig().set(args[1] + ".Y", args[4]);
    38. getConfig().set(args[1] + ".Z", args[5]);
    39.  
    40. saveConfig();
    41.  
    42. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.RED + "HP" + ChatColor.BLUE + "]" + ChatColor.AQUA + " Succesfully set location for: " + args[1] + " to - World: " + args[2] + " X: " + args[3] + " Y: " + args[4] + " Z: " + args[5]);
    43.  
    44. }
    45.  
    46. return true;
    47. }
    48. }


    Code (Teleportation):
    Code:java
    1. package me.CraftCreeper6.teleport;
    2.  
    3. import me.CraftCreeper6.main.Main;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.World;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.block.Sign;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.block.Action;
    16. import org.bukkit.event.player.PlayerInteractEvent;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.util.Vector;
    19.  
    20. public class Teleportation extends JavaPlugin implements Listener {
    21.  
    22. public static Main plugin;
    23.  
    24. public Teleportation(Main instance) {
    25. plugin = instance;
    26. }
    27.  
    28. public void tP(Player p, World world, int x, int y, int z){
    29.  
    30. Location loc = new Location(world, x, y, z);
    31.  
    32. p.teleport(loc);
    33.  
    34. }
    35.  
    36. @EventHandler
    37. public void onPlayerInteract(PlayerInteractEvent event) {
    38. final Player p = event.getPlayer();
    39. Block block = event.getClickedBlock();
    40.  
    41. if (block != null && block.getType() == Material.SIGN_POST
    42. || block != null && block.getType() == Material.WALL_SIGN) {
    43. Sign sign = (Sign) block.getState();
    44. Action action = event.getAction();
    45. if (action == Action.RIGHT_CLICK_BLOCK) {
    46. if (sign.getLine(0).equalsIgnoreCase("[HP]")
    47. || sign.getLine(0).equalsIgnoreCase(ChatColor.BLUE + "[" + ChatColor.RED + "HP" + ChatColor.BLUE + "]")) {
    48.  
    49. sign.setLine(0, ChatColor.BLUE + "[" + ChatColor.RED + "HP" + ChatColor.BLUE + "]");
    50. sign.update();
    51.  
    52. if(p.hasPermission("HP.Create")){
    53.  
    54. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.RED + "HP" + ChatColor.BLUE + "]" + ChatColor.AQUA + " Remember to set the destination using: /HP set [Name] [World, X , Y, Z]");
    55.  
    56. }
    57.  
    58. final int x = (int) getConfig().get(sign.getLine(1) + ".X");
    59. final int y = (int) getConfig().get(sign.getLine(1) + ".Y");
    60. final int z = (int) getConfig().get(sign.getLine(1) + ".Z");
    61. final World world = (World) getConfig().get(sign.getLine(1) + ".World");
    62.  
    63. p.setVelocity(p.getLocation().getDirection().multiply(-3));
    64. p.setVelocity(new Vector(p.getVelocity().getX(), 9.0D, p.getVelocity().getZ()));
    65.  
    66. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    67.  
    68. @Override
    69. public void run() {
    70.  
    71. tP(p, world, x, y, z);
    72.  
    73. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.RED + "HP" + ChatColor.BLUE + "]" + ChatColor.AQUA + " You have been teleported!");
    74.  
    75. }
    76.  
    77. }, 40);
    78.  
    79. } else {
    80.  
    81. }
    82. }
    83. }
    84. }
    85. }


    Any help appreciated! :)
     
  2. Offline

    CraftCreeper6

    RAFA_GATO_FOFO
    Yes, sorry forgot to post the stack trace here:
    Code:
    [01:09:11 ERROR]: Could not pass event PlayerInteractEvent to HubblePort v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:191) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b29
    74jnks]
            at net.minecraft.server.v1_7_R1.PlayerInteractManager.interact(PlayerInt
    eractManager.java:374) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.a(SourceFile:60)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInBlockPlace.handle(SourceFile
    :9) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.IllegalArgumentException: File cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203) ~[craftbu
    kkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(Yam
    lConfiguration.java:171) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:118) ~
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:112) ~[cr
    aftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            at me.CraftCreeper6.teleport.Teleportation.onPlayerInteract(Teleportatio
    n.java:58) ~[?:?]
            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.ja
    va:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
            ... 15 more
     
  3. CraftCreeper6
    I don't know if that'll fix it but I see that you've extended both classes as JavaPlugin.
    Again I don't know if this is the issue (or even an issue at all) but I've learned that only the main class should have it.
     
  4. Offline

    mythbusterma

    That is most likely the issue, as having the second JavaPlugin, it will likely be looking for a Teleportation folder and its respective configuration. As for accessing the config outside of that class, the easiest way is to maintain a reference to the JavaPlugin class, i.e. a private field that is set in the constructor:
    Code:java
    1.  
    2. public class Teleportation extends Listener {
    3.  
    4. private Main parent_;
    5.  
    6. public Teleportation (Main parent) {
    7. parent_ = parent;
    8. }
    9. // etc etc
    10. }


    Then you can use:
    Code:
    parent_.getConfig()
    Anywhere in your code and it will work correctly.

    P.S. I would strongly advise you to rename the "Main" class to the name of your plugin, it would make more sense.
     
  5. mythbusterma Convention actually discourages starting variables with an underscore. :)
     
    es359 and Konkz like this.
  6. Offline

    1Rogue

    Only extend JavaPlugin in your main class. Other classes are not other plugins
     
  7. Offline

    CraftCreeper6

    1Rogue
    mythbusterma
    Desided to move it all into my Main class :p; I figured out why; Config:
    Code:
    TnT:
      World: Main
      X: '100'
      Y: '70'
      Z: '100'
    
    As you see the int's are in ' ' for some reason, can anyone help? :)
     
  8. Offline

    mythbusterma

    Sorry I meant to suffix it, fixed.

    When setting values via code those quotes will surround the value, and in reading the values they are inconsequential. That isn't your problem.
     
  9. CraftCreeper6
    I've answered this question on your other post.
     
  10. mythbusterma Pretty sure it's against convention to end it with an underscore, too. Nothing wrong with just calling it "parent" :p
     
  11. Offline

    mythbusterma

    The problem is with getters and constuctors using the name parent as an argument, and I prefer that notation to the "this.parent = parent" which I feel is less comprehensible. Coding guidelines I use: http://geosoft.no/development/javastyle.html (see rule number 8)

     
  12. Offline

    1Rogue


    You're setting them as strings, not ints.
     
Thread Status:
Not open for further replies.

Share This Page