Warning...

Discussion in 'Plugin Help/Development/Requests' started by xSora, Aug 1, 2015.

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

    xSora

    I Have i warning, can anywhere help me to fix it ^^
    Code:
    package listeners;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Head implements Listener{
    
        @EventHandler(priority=EventPriority.NORMAL)
        public void onDeath(PlayerDeathEvent e) {
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = e.getEntity().getKiller();
               
                ItemStack Kopf = new ItemStack(397, 1, (short) 3); //Here is the baad Warning...
                ItemMeta meta = Kopf.getItemMeta();
                meta.setDisplayName("§6"+e.getEntity().getName());
                Kopf.setItemMeta(meta);
               
                killer.getInventory().addItem(Kopf);
            }
        }
    
    }
    
     
  2. Online

    timtower Administrator Administrator Moderator

    Moved to plugin development.
    @xSora use the Material enum instead of item ID's
     
  3. Offline

    xSora

    how do it? i'm new, my first plugin ^^
     
  4. Offline

    PDKnight

    The warning appears because 397 is Integer, not a material. Use Material.SKULL_ITEM instead :)
     
  5. Offline

    xSora

    Okay, Thank you ^^

    Another error plz help ^^

    Code:
    package Commands; //Iwas stimmt hier nicht :(
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    
    import me.xSora.SoraSDK.main;
    
    public class ChatClear implements CommandExecutor{
    
        main plugin;
    
        public ChatClear( ) {
      
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
      
            if(sender instanceof ConsoleCommandSender) {
                ChatClearPlayers(100, "Console");
            }
            if(sender instanceof Player) {
                Player p = (Player) sender;
                if(!p.hasPermission("sora.cc")){
                    p.sendMessage("§3[SoraSDK] §7Fehlende Rechte.");
                    return true;
                }
                ChatClearPlayers(100, p.getName());
            }
      
            return true;
        }
    
    
        public void ChatClearPlayers(int zeilen, String name) {
      
            for(int i = 0; i<=zeilen; i++) {
                plugin.getServer().broadcastMessage("§3[SoraSDK] §7Der Chat wurde von §a"+ name +"§7 Geleert");
                }
    
        }
    }
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Aug 1, 2015
  6. Online

    timtower Administrator Administrator Moderator

    @xSora And what is the error then? You aren't telling that.
     
  7. Offline

    xSora

    if i enter /cc the console outpot only errors
     
  8. Online

    timtower Administrator Administrator Moderator

    @xSora We can't help if you don't show us the error itself. Please put it in http://pastebin.com and post the link here.
     
  9. Offline

    xSora

  10. @xSora plugin is null, you need to get the instance through the constructor.
     
  11. Offline

    ChipDev

  12. Offline

    xSora

  13. Moved to Bukkit Alternates.
     
  14. Offline

    Boomer

    Caused by: java.lang.NullPointerException
    at Commands.ChatClear.ChatClearPlayers(ChatClear.java:42) ~[?:?]

    plugin.getServer().broadcastMessage("§3[SoraSDK] §7Der Chat wurde von §a"+ name +"§7 Geleert");

    Means that AT THE MOMENT that the execution reaches this line, there is a null object being dereferenced on this line (ie, trying to access .getSomething() or .doSomething() on a null object. Possibilities: plugin.getServer() may be null, in which case the .broadcastMessage() cant be accessed. Or plugin may be null, and ou can't .getServer() from null.

    The chances of .getServer() returning a null object is insanely small, so the odds are that plugin is null at this point. So trace the execution back - how does the program get to this line, and when it does, have we given value to the plugin object that it uses?

    You have a plugin variable of the type main (your plugin main class name..). You dont initialize plugin within the constructor (where most people do)... you dont have a .set method to set plugin externally. Therefore 99.99999999% chance that the null is plugin {You can confirm by putting the following statement just before the error line:
    Code:
    if (plugin==null){System.err.println("plugin is null indeed");}
    
    and you should see that message in your console/log just before the NPE gets thrown.

    You need to initialize plugin, and through the constructor is the best place to do it, passing an instance of the plugin through the constructor when created in the calling code
     
  15. Offline

    raymart23

    do this instead of main plugin;
    PHP:
    public static main plugin;
    and I think that you might don't need this code do you ?
    Code:
    public ChatClear( ) {
          
        }
    
     
Thread Status:
Not open for further replies.

Share This Page