得到错误! ! ! ! ! [英] Getting an error! ! ! ! !
本文介绍了得到错误! ! ! ! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它给我这个错误,我不知道如何解决它:
its giving me this error and i dont know how to fix it:
Exception in thread "main" java.lang.NullPointerException
at Testing.main(Testing.java:61)
我尝试过:
What I have tried:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Testing
{
public static void main(String args[])
{
List<String> teams = new ArrayList<String>();
// List<String> ghosts = new ArrayList<String>();
// HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
String ghoststr;
teams.add("A");
teams.add("B");
teams.add("C");
teams.add("D");
teams.add("E");
int ghost = teams.size() % 2;
ghoststr = "GHOST";
if (ghost == 1 )
{
teams.add(ghoststr);
//ghosts.add("GHOST");
}
//teams.add("G");
//teams.add("F");
int totalRounds = teams.size()-1;
int matchesPerRound = teams.size()/2;
Game[][] rounds = new Game[totalRounds][matchesPerRound];
//String ght = teams.get(teams.size()-1);
for (int round = 0; round< totalRounds; round++)
{
for (int match = 0; match < matchesPerRound; match++)
{
int home = (round + match) % (teams.size() - 1);
int away = (teams.size() - 1 - match + round) % (teams.size() - 1);
String team1 = teams.get(home);
String team2 = teams.get(away);
if (match == 0)
{
away = teams.size()-1;
}
//if ( team1 != team2)
rounds[round][match].team1= team1;
rounds[round][match].team2 = team2;
//rounds[round][match] = Game.team1 + " v " + team2 ;
}
}
for (int i = 0; i < rounds.length; i++) {
for(int j = 0; j < rounds.length ; j++)
{
System.out.println("Round " + (i + 1));
if ( !(rounds[i][j].team1.equals(ghoststr) || rounds[i][j].team2.equals(ghoststr)) )
{
String printstr = rounds[i][j].team1 + "V" + rounds[i][j].team2;
System.out.println(printstr);
System.out.println();
}
}
}
}
}
class Game
{
String team1;
String team2;
}
推荐答案
来自评论。
OP有声明了数组但是在填充时没有创建对象的新实例。
需要类似的东西:
From comments.
OP had declared the array but had not created new instances of the object when populating it.
Something similar to this was needed:
rounds[round][match] = new Game;
rounds[round][match].team1= team1;
rounds[round][match].team2 = team2;
这篇关于得到错误! ! ! ! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文