Java

Discussion in 'Spigot Plugin Development' started by PCPSells, Jun 2, 2016.

  1. Okay, so here's my code..
    Code (Text):
    package listeners;
    import ActionBarAPI.ActionBarAPI;
    import me.PCPSells.MaineT;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import stats.Stats;
    public class RightClickToken implements Listener {
      MaineT plugin;
      public RightClickToken(MaineT pl) {
        this.plugin = pl; }
    @EventHandler
      public void onPlayerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        ItemStack air = new ItemStack(Material.AIR);
        if (e.getItem().getType() == Material.getMaterial(plugin.getConfig().getString("tokens.TokenID"))) {
            if (e.getItem().getItemMeta().getDisplayName().equals(plugin.getConfig().getString("tokens.TokenName").replace("&", "§"))) {
                p.playSound(p.getLocation(), Sound.valueOf(plugin.getConfig().getString("sounds.Success")), 1.0F, 1.0F);
                ActionBarAPI bar = new ActionBarAPI(p, plugin.getConfig().getString("ActionBarMsgs.Profit").replace("&", "§").replace("%prefix%", plugin.getConfig().getString("messages.Prefix").replace("&", "§")).replace("%amount%", "" + p.getItemInHand().getAmount()));
                bar.sendPlayerAnnouncement();
                Stats.addPoints(p, p.getItemInHand().getAmount());
                p.setItemInHand(air);
                p.updateInventory();
              return;
            } else {
                e.setCancelled(false);
                return; }
        } else {
            e.setCancelled(false);
            return;
        }}}
    and the console error I get tells me that EVERY time a player interacts with ANYTHING...
    I get a JavaIOException error
    How do I throw the error and make this huge ass console leak stop? Please help..
     
  2. Can you show the stacktrace or show/tell which line is causing the issue?
     
  3. Please show us the full code and the full error report.
     
  4. This is extremely messy code, I won't explain how to fix your messiness.

    Are you sure it's an IOException? My guess it would be a NullPointerException when you try to access the item, item's meta, and the item meta's display name without actually checking if they exist first.
     
  5. It's a NullPointerException
     
  6. How would I fix this? Never had this issue before.
     
  7. Can you show us what the error in your console says? Copy and paste please.

    If it is what Xilixir thinks it is:

    Before the if statement write the following code:

    If(e.getItem() == null) return;
     
  8. You have to check if the item is not null, the item's item meta is not null, and the item meta's display name isn't null - always before using them.
     
  9. How you fix this is learning the basics of java, and how to format your code so it is actually readable.
    https://www.spigotmc.org/wiki/how-to-learn-java-programming/

    NullPointerException means you are trying to use data that points nowhere and simply does not exist. A variable in java is a "reference" or "pointer" to a location in memory, and that location is not defined or filled with any data until you give the variable a value.

    The exception are primitive types (int, boolean, float, long, double and the like) which are initialized to zero if no number is given.

    Code (Java):
    int owls;  // value is 0
    int ducks = 3; // value is 3

    String beans; // value is null, trying to use this for anything throws NullPointerException or other errors
    String cheese = "asdf"; // value is defined

    Random rnd1; // not initialized, null and unusable
    Random rnd2 = new Random(); // new is used to create a new Random object and assign it to rnd2
    rnd1 = new Random(); // now rnd1 is initialized as well, with another Random object
     
  10.