Solved errors

Discussion in 'Spigot Plugin Development' started by M7MDMCGamer, May 2, 2017.

  1. how to understand plugin errors in console
     
  2. Is this your plugin or someone else's?

    Usually you'll get a lot of spam about internal spigot or bukkit errors but as you navigate the stack trace you'll eventually find a line number this is typically a good place to start looking like:

    null pointer exception someFile.java line 23:
    If it's your plugin that has the error it will tell you the line that caused it.

    There are a bunch of different errors you'll commonly find in a spigot stack trace and here are their common causes.

    Null Pointer Exception (NPE): these are thrown when you try to access a field or method of a class that is Null.
    Common Cause - trying to do
    Code (Text):

    player.getInventory().getItemInHand().getType()
     
    will throw a NPE if the player has no item in their hand.

    So if any previous class is null, trying to access a method or field will cause this to happen, not just getType(), ANY OTHER method or field. Likewise if the player object in the above example was null for some reason you'll get an NPE on anything you try to access beyond player.anyMethod();


    ConcurrentModificationException: this thrown when you try to modify a list/HashMap etc while iterating over it.

    Code (Text):

    for(ItemStack is:someSavedList) {
        if(is.getType() == Material.DIAMOND)
            someList.remove(is); //causes concurrent modification error

        is.setType() == Material.EMERALD;
        someSavedList().add(is); //also causes error
    }

     
    Solution: modify your list outside of the loop OR use iterator() to navigate the list.


    ClassCastException: this is caused by trying to cast one object to another when the objects are not compatible.

    Code (Text):

    Block blk = e.getClickedBlock();
    If (blk.getState() instanceof Furnace)
    {
         Furnace furn = (Furnace) furnace;
         //this causes a class cast exception you must cast a blockstate in this instance.



    }

     
    Solution: check your classes to see what they extend before casting. In this instance Furnace extends blockstate. Thus the code above would need to be;
    Furnace furn = (Furnace) blk.getState();



    IndexOutOfBounds Error: called when you try to access an element of an array which does not exist, for example:

    Code (Text):

    String[] letters = {"a","b","c"};

    String aLetter = letters[3];   //causes an out of bounds error because the size of this array is 3, however it's first element "a" is located at index 0.  

     
    This error is also commonly found when iterating over the elements of an array with a loop. If the array is ever null or empty and you try to get an element by an index you will get an out of bounds.

    Solution: Always check that your array is not null and it's size is > the index you are trying to access.



    These are a few of the common errors I see here in stack traces, they offer great insight as to what has gone wrong with a plugin.


    Sent from my iPhone using Tapatalk