Solved NullPointerException

Discussion in 'Spigot Plugin Development' started by cayote20, Jun 18, 2020.

Thread Status:
Not open for further replies.
  1. Line with NPE:
    Code (Java):
    this.isFactionLeader = faction.getLeader().equals(player.getName());
    (faction != null, faction.getLeader() == null)
    faction.leader:
    Code (Java):
    this.leader = configurationManager.getFactions().getString("Factions." + identifier + ".Leader");
    (configurationManager != null, configurationManager.getFactions() != null, identifier != null)

    factions.yml:
    Code (Text):
    Factions:
      test:
        Name: 'tester '
        Leader: ''
        Members: []
        DeputyRank: 9
        RanksNames: []
        Permissions: []
        Spawn:
          X: -938.693195271851
          Y: 64.0
          Z: 760.1740995693649
          Pitch: 24.150015
          Yaw: 120.4389
          W: world
          isSet: true
    Your help will be appreciated!
     
  2. Check if Player == null
     
  3. Debug:
    Code (Java):
    System.out.println(player == null);
    Output:
    Code (Text):
    [12:34:22 INFO]: false
     
  4. Can you send the console error pls ?
     
  5. drives_a_ford

    Moderator

    You're saying that #getLeader returns null. Yet you try to use its method:
     
    • Like Like x 1
  6. I want to make it not null, because it doesn't have to return null.
     
  7. In your factions.yml, try to make a value for "Leader: 'test' "
     
  8. Code (Text):
    [15:01:05 ERROR]: Could not pass event PlayerJoinEvent to CloudFactions v1.0
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-79a30d7-acbc348]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-79a30d7-acbc348]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:500) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:485) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.PlayerList.onPlayerJoin(PlayerList.java:346) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.PlayerList.a(PlayerList.java:166) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.LoginListener.b(LoginListener.java:159) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.LoginListener.e(LoginListener.java:57) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.NetworkManager.a(NetworkManager.java:233) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.ServerConnection.c(ServerConnection.java:140) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:845) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot.jar:git-Spigot-79a30d7-acbc348]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_231]
    Caused by: java.lang.NullPointerException
        at md.sintez.cloudfactions.system.units.FactionPlayer.<init>(FactionPlayer.java:21) ~[?:?]
        at md.sintez.cloudfactions.system.events.Events.onJoin(Events.java:27) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_231]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_231]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_231]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-79a30d7-acbc348]
        ... 14 more
     
  9. Ok, I will try it now.
     
  10. Same exception :(
     
  11. Can you send code around
    Code (Java):
    this.isFactionLeader = faction.getLeader().equals(player.getName());
    . And can tell me what is write on line 21 of your FactionPlayer.java class
     
  12. FactionPlayer.java:21:
    Code (Java):
    this.isFactionLeader = faction.getLeader().equals(player.getName());
    FactionPlayer.java:
    Code (Java):
    package md.sintez.cloudfactions.system.units;

    import md.sintez.cloudfactions.system.configurations.ConfigurationManager;
    import org.bukkit.entity.Player;

    import java.util.Set;

    public class FactionPlayer {
        private Player player;
        private Faction faction;
        private boolean isFactionLeader, isDeputyLeader, isInFaction;
        private int factionRank;

        private ConfigurationManager configurationManager;

        public FactionPlayer(Player player) {
            this.configurationManager = new ConfigurationManager();
            this.player = player;
            this.faction = new Faction(getFactionIdentifier());
            if(this.faction.factionExists()) {
                this.isFactionLeader = faction.getLeader().equals(player.getName());
                if(faction.getMembers().containsKey(player.getName())) {
                    this.isDeputyLeader = faction.getMembers().get(getPlayer().getName()) == faction.getDeputyRank();
                }
                this.isInFaction = new Faction(getFactionIdentifier()).factionExists();
                this.factionRank = configurationManager.getFactions().getInt("Factions." + faction.getIdentifier() + ".Members." + player.getName());
            }
        }

        public Player getPlayer() {
            return player;
        }

        private String getFactionIdentifier() {
            Set<String> factions = configurationManager.getFactions().getConfigurationSection("Factions").getKeys(false);
            for(String factionIdentifier : factions) {
                Faction faction = new Faction(factionIdentifier);
                if(faction.getLeader().equals(getPlayer().getName()) || faction.getMembers().containsKey(getPlayer().getName())) return factionIdentifier;
            }
            return "";
        }

        public Faction getFaction() {
            return faction;
        }

        public boolean isFactionLeader() {
            return isFactionLeader;
        }

        public boolean isDeputyLeader() {
            return isDeputyLeader;
        }

        public boolean isInFaction() {
            return isInFaction;
        }

        public int getFactionRank() {
            return factionRank;
        }
    }
     
     
  13. You use default faction plugin ? Or faction UUID ? Or other ?
     
  14. It is my own plugin...
     
  15. Ok so can you show me your method getLeader() from the object Faction ?
     
  16. Code (Java):
    this.leader = configurationManager.getFactions().getString("Factions." + identifier + ".Leader");
    Code (Java):
        public String getLeader() {
            return leader;
        }
     
  17. From what I can see, it's supposed to work, but strangely enough, it doesn't.
    If you set a data in Leader: '' (for exemple your username) and try System.out.prinln(faction.getLeader()) what return you in the console ?
     
  18. Debug (at the moment of faction creation):
    Code (Java):
    System.out.println("Leader: " + configurationManager.getFactions().getString("Factions." + factionIdentifier + ".Leader"));
    Output:
    Code (Java):
    [15:45:42 INFO]: Leader:
    No exceptions
     
  19. Did you try the same think with making a fake username as faction leader (in your faction.yml) ?

    Do it is normal that when you create a faction the leader case stay empty ? It shoul be your username normaly
     
  20.  
Thread Status:
Not open for further replies.