NullPointerException Tableau Java

Résolu/Fermé
TriiNoxYs Messages postés 180 Date d'inscription lundi 14 juillet 2014 Statut Membre Dernière intervention 27 juin 2015 - Modifié par TriiNoxYs le 27/06/2015 à 13:17
TriiNoxYs Messages postés 180 Date d'inscription lundi 14 juillet 2014 Statut Membre Dernière intervention 27 juin 2015 - 27 juin 2015 à 14:44
Salut ! Je suis actuellement entrain de coder un plugin Mincraft sous Spigot 1.8.7 qui ressemble un peu à un de genre BuildingGame ou BuildBattle (Hypixel) et j'ai une erreur NullPointerException sur un tableau. La plupart du temps ça vient du fait qu'on ecrit "int[] tab;" au lieu de "int[] tab = new int[8];" mais dans mon cas j'ai bien déclaré mon tableau. Je pense que c'est un petit probleme tout con mais je vois vraiment pas ^^

J'ai vu sur StackOverflow que si on met le tableau hors du constructeur ça règle ce problème mais ça m'embête vraiment de faire ca :/

Code d'erreur:
[12:19:23] [Server thread/ERROR]: Could not pass event PlayerInteractEvent to BuildTime v0.3
org.bukkit.event.EventException
	at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:227) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:194) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:694) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0]
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) [?:1.7.0]
	at java.util.concurrent.FutureTask.run(FutureTask.java:166) [?:1.7.0]
	at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:718) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560) [spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	at java.lang.Thread.run(Thread.java:722) [?:1.7.0]
Caused by: java.lang.NullPointerException
	at fr.TriiNoxYs.BuildTime.handlers.Plot.setVotes(Plot.java:69) ~[?:?]
	at fr.TriiNoxYs.BuildTime.handlers.Votes.setVote(Votes.java:76) ~[?:?]
	at fr.TriiNoxYs.BuildTime.events.Players.onInterractWithItem(Players.java:144) ~[?:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0]
	at java.lang.reflect.Method.invoke(Method.java:601) ~[?:1.7.0]
	at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.7.jar:git-Spigot-3ccbe77-a6a57a9]
	... 19 more 


Code:
public class Plot{
    
    private int plotID;
    private Player owner;
    private Location loc;
    private int votes[] = new int[8];
    private int score;
    
    public Plot(int plotID, Player owner, Location loc, int votes[], int score){
        this.plotID = plotID;
        this.owner = owner;
        this.loc = loc;
        this.votes = votes;
    }
    
    public int getPlotID(){
        return plotID;
    }
    
    public Player getOwner(){
        return owner;
    }
    
    public Location getLoc(){
        return loc;
    }
    
    public World getWorld(){
        return loc.getWorld();
    }
    
    public int[] getVotes(){
        return votes;
    }
    
    public int getScore(){
        //score = votes[0]+votes[1]+votes[2]+votes[3]+votes[4]+votes[5]+votes[6]+votes[7];
        
        /*
        for(int i = 0; i < 8; i++){
            score += votes[i];
        }
        */
        
        score = 42;
        return score;
    }
    
    public void setPlotID(int plotID){
        this.plotID = plotID;
    }
    
    public void setOwner(Player owner){
        this.owner = owner;
    }
    
    public void setVotes(int playerID, int vote){
        playerID--;
        //Bukkit.broadcastMessage("votes[0]: " + this.votes[0]);
        votes[playerID] = vote; //La ligne 69 est içi                    <<<------ LIGNE 69
        //Bukkit.broadcastMessage("this.votes[playerID] = vote;");
        //Bukkit.broadcastMessage("votes[0]: " + this.votes[0]);
    }
    
} 


Merci d'avance à ceux qui me répondront et m'aideront :)
Cordialement,
TriiNo'
A voir également:

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
27 juin 2015 à 13:16
Bonjour,

Tu as beau faire
private int votes[] = new int[8];
à l'initialisation de l'objet, la valeur de
votes
sera écrasée au moment du constructeur avec
this.votes = votes;
et à ce moment là, tu dois avoir
votes=null
ce qui provoque le NullPointerException...
1
TriiNoxYs Messages postés 180 Date d'inscription lundi 14 juillet 2014 Statut Membre Dernière intervention 27 juin 2015 43
Modifié par TriiNoxYs le 27/06/2015 à 13:25
Voici l'homme le plus rapide du monde pour répondre o_O
Merci d'avoir répondu aussi vite !

J'ai rajouté "votes = new int[8];" dans mon constructeur, je n'ai plus d'erreur ça à l'air de marcher, je verrais par la suite si ça ne pose aucn autre problème

Merci à toi :D
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
27 juin 2015 à 13:34
Le problème qui risque d'y avoir est lié au fait que tu ne prennes jamais en compte le paramètre
votes
qui est passé au constructeur, alors qu'il pourrait potentiellement contenir des valeurs utiles (ou alors ce paramètre ne sert à rien et il faut le supprimer)

La question que l'on pourrait se poser c'est pourquoi on a eu null dans ce constructeur.
Un moyen d'identifier le problème serait de faire comme ceci :

public Plot(int plotID, Player owner, Location loc, int votes[], int score){
    this.plotID = plotID;
    this.owner = owner;
    this.loc = loc;
    if (votes==null)
        throw new IllegalArgumentException("votes is null");
    if (votes.length!=8)
        throw new IllegalArgumentException("votes.length is: "+votes.length);
    this.votes = votes;
}

Ça va créer une autre erreur ("votes is null") mais avec la stacktrace associée on pourra savoir à quel moment ce constructeur est appelé avec une valeur null et donc corriger si nécessaire.
0
TriiNoxYs Messages postés 180 Date d'inscription lundi 14 juillet 2014 Statut Membre Dernière intervention 27 juin 2015 43
27 juin 2015 à 13:38
Benh enfaite de base votes est null, mais sa valeur ets changée apres (quand les joueurs votent chacun leur tour).
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015 > TriiNoxYs Messages postés 180 Date d'inscription lundi 14 juillet 2014 Statut Membre Dernière intervention 27 juin 2015
27 juin 2015 à 13:40
Donc le paramètre
votes
ne sert à rien, si c'est pour passer la valeur
null
et ne pas en tenir compte après, autant le supprimer.
0
TriiNoxYs Messages postés 180 Date d'inscription lundi 14 juillet 2014 Statut Membre Dernière intervention 27 juin 2015 43
Modifié par TriiNoxYs le 27/06/2015 à 13:46
Benh si apres je m'en sert:
 public int[] getVotes(){
        return votes;
    }
    
    public int getScore(){
        //score = votes[0]+votes[1]+votes[2]+votes[3]+votes[4]+votes[5]+votes[6]+votes[7];
        
        for(int i = 0; i < 8; i++){
            score += votes[i];
        }
        
        return score;
    }


EDIT: Ok je viens de comprendre, j'ai suprimmé le parametre mais gardé la variable, je pensais que tu me disais de TOUT suprimmer.
Le constructeur ressemble à ça maintenant:
public Plot(int plotID, Player owner, Location loc, int score){
        this.plotID = plotID;
        this.owner = owner;
        this.loc = loc;
    }
0