In Main:
Equipe Eq1 = new Equipe(J,E);
Equipe Eq2 = new Equipe(J,E);
while(Eq1.equals(Eq2))
Eq2 = new Equipe(J,E);
Match m = new Match(Eq1,Eq2);
String ChercherJoueur = m.QuelEquipe(m.hasBall());
In Class Equipe:
public Vector<Joueur> VJ;
public Equipe(Vector<Joueur> E, Vector<Entraineur> Ent) {
VJ = new Vector<Joueur>();
//rest of the logic
}
public Equipe() {
}
In Class Match:
Equipe Eq1 = new Equipe();
Equipe Eq2 = new Equipe();
public Match(Equipe Eq1, Equipe Eq2) {
Eq1 = this.Eq1;
Eq2 = this.Eq2;
}
public String QuelEquipe(Joueur J)
{
boolean found = Eq1.ChercherJoueur(J);
if(found == true)
return "EQ1";
else
return "EQ2";
}
public Joueur hasBall()
{
Joueur J = null;
int i = 0;
boolean found = false;
NullPointerException-------> System.out.println(Eq1.VJ.get(i).isBall());
System.out.println(Eq2.VJ.get(i).isBall());
while(!found)
{
if((Eq1.VJ.get(i).isBall())==true)
{
found = true;
J= Eq1.VJ.get(i);
}
else if((Eq2.VJ.get(i).isBall())==true)
{
found =开发者_运维百科 true;
J= Eq2.VJ.get(i);
}
i++;
}
return J;
}
}
I think is all I need to include here to inform u about the situation.. I get a NullPointerException when I do "m.QuelEquipe(m.hasBall());" that can be traced back to where I pointed in The Class Match.. I know exactly what the exception means, and I'm changing their references with "Eq1 = this.Eq1;Eq2 = this.Eq2;" anyway.. sooo where is flow to get the code to work??
Your constructor using two args does initialize the vector but the parameterless constructor doesn't
public Equipe(Vector<Joueur> E, Vector<Entraineur> Ent) {
VJ = new Vector<Joueur>(); //<-- OK
//rest of the logic
}
public Equipe() {
//<-- errrk
}
Then when you invoke:
System.out.println(Eq2.VJ.get(i).isBall());
You're using really invoking:
Eq2.null.get <-- NullPointerException
But the real problem though is in the Match constructor:
public Match(Equipe Eq1, Equipe Eq2) {
Eq1 = this.Eq1;
Eq2 = this.Eq2;
}
Here you're assigning to the local variable Eq1 the value of the instance variable Eq1 you really want it the other way around:
public Match(Equipe Eq1, Equipe Eq2) {
this.Eq1 = Eq1;
this.Eq2 = Eq2;
}
BTW, this is not C# and in Java as a coding conventions both, methods and attributes start with lowecase and opening brace goes in the same line ( although this last part is not as relevant as the naming convention )
I hope this helps.
You are calling the default constructor public Equipe()
which does not initialize VJ
. Just remove the constructor if you are not going to use it.
This will fix your problem:
Match.java
private final Equipe eq1;
private final Equipe eq2;
public Match(final Equipe eq1, final Equipe eq2)
{
this.eq1 = eq1;
this.eq2 = eq2;
}
精彩评论