Solved NPE on if statement

Discussion in 'Plugin Development' started by Blackwing_Forged, Nov 18, 2017.

Thread Status:
Not open for further replies.
  1. this is the line its referring to, yes the file exists and is there


    Code:
    if(!(mainClass.raceConfig.contains(p.getUniqueId().toString())))
     {
             mainClass.raceConfig.set(p.getUniqueId().toString(), "Elf");
             try {
             mainClass.raceConfig.save(mainClass.raceFile);
             mainClass.raceConfig.load(mainClass.raceFile);
             } catch (IOException | InvalidConfigurationException e1) {
                      e1.printStackTrace();
             }
             p.sendMessage(ChatColor.AQUA + "You chose the race of Elf");
             p.closeInventory();
    }
     
  2. I suggest to make your code more readable in terms of understanding and debugging.
    What I mean is split up your code for example:

    Code:
    p.getUniqueID().toString()
    will become

    Code:
    ID id = p.getUniqueID();
    idAsString = id.toString();

    if you continue doing this on your code it will become cleaner and more understandable and you can view your variables and values in a debugger or print the values to the console to see which statement gives you a NullPointer.
     
  3. @KilledBycheese
    Alright but the exact line that im getting an error in is the
    if(!(mainClass.raceConfig.contains(p.getUniqueId().toString())))
    the config file exists its in my plugin folder, the player exists because its on an inventory event click so I don't know what could be giving null
     
  4. Online

    timtower Administrator Administrator Moderator

    @Blackwing_Forged Start printing values.
    mainClass
    raceConfig
    p
    raceFile
    If they are all different from null then I don't know the answer either.
     
  5. Offline

    Unknown123

    Post the NPE stack trace
     
Thread Status:
Not open for further replies.

Share This Page