我正在尝试从文本文件中读取这些名称,但出现错误。这是我的主要方法的相关部分,以及有问题的方法:
public class Prog4
{
int mostVoteCount = 0;
int mostVotes = 0;
int mostVotesIndex = 0;
int fewestVoteCount = 0;
int fewestVotes = 0;
int fewestVotesIndex = 0;
String[] candidateArray;
int[] votesArray;
public static void main(String args[]) throws Exception
{
//Scanner stdin = new Scanner(System.in);
Scanner stdin = new Scanner(new File("test.txt"));
int num_votes, num_candidates, election_num = 1;
System.out.println("Welcome to Prog4!\n");
Prog4 p4 = new Prog4();
while (stdin.hasNextLine())
{
System.out.println("Results for election " + election_num);
p4.read_candidates(stdin, p4.candidateArray);
p4.read_votes(stdin, p4.votesArray);
System.out.println("Total votes: " + p4.votesArray.length);
}
System.out.println("Done. Normal termination of Prog4.");
}
和有问题的方法:
public void read_candidates(Scanner stdin, String candidates[])
{
int candidateCount = 0;
candidateCount = (stdin.nextInt());
for (int i = 0; i < candidateCount; i++)
candidates[i] = (stdin.next());
}
这是我在“测试”文本文件中使用的测试数据:
4
欧文
杰克
斯科特
罗比
15 0 1 1 2 3 1 0 0 0 0 1 2 2 1 1
这是堆栈跟踪吗?
java.lang.NullPointerException
at Prog4.read_candidates(Prog4.java:75)
at Prog4.main(Prog4.java:35)