Help with NullPointerException [Resolved]

Discussion in 'Plugin Development' started by Ahniolator, Sep 23, 2011.

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

    Ahniolator

    I've been playing with this for awhile, and I thought I almost had something and then I got a NP. I've tried a few things, and it doesn't seem to be working. Does anyone have any suggestions? NP in code is highlighted red in first image. Thanks in advance for any constructive input! :D

    Edit: Even though I fixed the first one, I got a new one. I'm totally lost on this one because it works for another class file, but not here for some reason.

    Ehh, I fixed it somehow.

    Edit: Got a new one. Reusing old thread

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  2. Need the code of your PlayerListener.
     
  3. Offline

    Ahniolator

    Added to op
     
  4. It's not likely, but in the line where the NullPointerException happens,
    only currentArmor.length can be the culprit.
    I don't know what Bukkit returns when the player has no armor at all, but you might want to check what currentArmor is after you've set it.

    Also, about your catch (Exception e):
    You send the player a message to look up the error message in the forum. Point is: None will appear when you catch it. You have to do e.printStackTrace();
     
  5. Offline

    Ahniolator

    Really? From what I'm aware, I'm getting the NullPointerException when I call the createNewFile() method from the invManager class. As for the printStackTrace error, I missed that part.

    Whenever you call the getContents() or getArmorContents() it still returns the number of slots in the inventory or armor slots, but when there is "nothing" in one of those slots it returns the index of that slot as null and moves on. Having all of the armor slots empty shouldn't be a problem because it would be similar to this:

    Code:
    currentArmor[0] == null;
    currentArmor[1] == null;
    currentArmor[2] == null;
    currentArmor[3] == null;
    which is why I catch the NullPointer in the for loop when I'm converting it all into a string, and replace it all with !,!,!,!; instead of actual values like 325,1,0,0 or similar. When I try to reconstruct it, it checks if the string is equal to !, then it would return null instead of a string allowing for the complete reconstruction of the ItemStack object within the ItemStack[] array. Unless, of course, I am wrong and it just doesn't work anyway.
     
  6. Offline

    Feed_Dante

    Without seeing the full source (or at least all files in the stack-trace) there's not mutch I can do.

    (This is just a guess as I can't really tell what part of you code is what line)
    Are you sure invManager has a value? You mention it works in another class file so I'm guessing you have something like this:

    declare invManager = null;
    BCSPlayerListener x = new BCSPlayerListener(invManager)
    invManager = new invManager()
    [other class file] y = new [other class file](invManager)

    Thus BCSPlayerListener always has a null invManager.


    Part 2:
    Code:java
    1. } catch (Exception e) {
    2. player.sendMessage(ChatColor.RED + "[BurningCS] Something went wrong! Check the console!");
    3. return;
    4. }

    Nothing will ever be printed to the console as you gracefully handle the error.
    Add either e.getMessage(), e.printStackTrace(), or some combo of the two.

    BTW you can use
    Code:
    [syntax=java][/syntax]
    to add line numbers and syntax highlighting.
     
  7. If your invManager is null, you must be passing a null invManager in the constructor.
     
  8. Offline

    Ahniolator

    @Pandemoneus @Feed_Dante

    Alright, I reformatted the code portion of the post so you could read it properly and added the full stack-trace. Hopefully you guys can get something out of this. I also added the source for the invManager as well
     
  9. Offline

    Feed_Dante

    @Ahniolator

    I would add:
    Code:java
    1. public void createNewFile(File file, String dir) {
    2. if (file == null)
    3. System.out.println("Error: File is null");
    4. if (dir == null)
    5. System.out.println("Error: Dir is null");
    6. if (file == null || dir == null)
    7. return;
    8.  
    9.  
    10. public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
    11. if (invManager == null) {
    12. System.out.println("invManager is null");
    13. return;
    14. }
    Too determine exactly what is null.


    If invManager is indeed null, I'll need the main plugin code ALL FILES IN THE STACK TRACE to help you further.
     
  10. Offline

    Ahniolator

    @Pandemoneus @Feed_Dante

    invManager is indeed null. Not sure why though, because I referenced it the exact same way in my blockListener class and it worked just fine.

    Fixed it somehow again... Time to get to work on the other things that I needed to do :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
Thread Status:
Not open for further replies.

Share This Page