我收到了以下发布的例外情况。我已经尝试更改构造函数中传递的参数以获得新的Ballot对象但是没有取得任何成功。此外,错误也给出了一个来自Ballot类的线,但是我没有看到它与主要内容的相关性。这是我的第一个计算机科学课,我非常感谢你的建议,并提前抱歉我的代码可能不是超级高效/干净。
错误:线程中的异常" main" java.lang.NullPointerException
<\ n>在Ballot。(Ballot.java:55)在Assig4.main(Assig4.java:54)
**
第55行:点击[i] = false;
第54行:ballotsAr.add(新的 选票(tempId,tempCategory,tempCandidates));
**
这是在命令行提示符处读取的文本文件:
2
33333:最佳吉他:Telecaster,Les Paul,Stratocaster,JazzMaster
23234:最佳功放:Jcm800,5150,双整流器,Plexi
/**
* Alex Drizos
* CS401
* Main Class to execute voting e machine
*/
//import section
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.*;
import javax.swing.*;
public class Assig4 {
public static void main (String [] args) throws IOException
{
/* variables */
String ballotsFile = args[0]; // gets the file name from command line
JFrame theWindow; //assign variable name for main window
ArrayList <Ballot> ballotsAr = new ArrayList<>(); //creates array for jpanel ballot objects
JButton login, submitVote;
int numBallots;
int counter = 0; //to count each file read in loop
/*create window*/
theWindow = new JFrame ("Voting E Machine");
theWindow.setLayout(new FlowLayout());
/* read in ballot info */
//objects - open and setup scanner for file
File myfile = new File (ballotsFile);
Scanner textScan = new Scanner(myfile); // reads in date from text file
numBallots = Integer.parseInt(textScan.nextLine()); //read in first line (number of ballots) and parse to int
while (textScan.hasNextLine()) //one ballot's data for each loop
{
//read in each ballot's info
//take the first line as a string
String [] tempStr = textScan.nextLine().split(":");
//parse line into proper data values
int tempId = Integer.parseInt(tempStr[0]); //take first value for id
String tempCategory = tempStr[1]; // take second value as category type
String [] tempCandidates = tempStr[2].split(","); // takes the arbitrary number of candidates and splits into separate string variables
//create ballot object
ballotsAr.add(new Ballot(tempId,tempCategory,tempCandidates));
counter++; //adds counter to while loop
} // end of loop to read in ballot text file contents
textScan.close(); //closes ballots.txt file
/*add Ballot panels*/
for (int i =0; i<ballotsAr.size();i++)
{
theWindow.add(ballotsAr.get(i));
}
/*add local components*/
submitVote = new JButton("Submit Vote(s)");
login = new JButton("Login");
theWindow.add(submitVote);
theWindow.add(login);
/*write actionListener class to handle login and vote submissions*/
/*pack and set window to visible*/
theWindow.pack();
theWindow.setVisible(true);
} //end of main
}
//import section
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Ballot extends JPanel {
//instance variables
private int id;
private JLabel categoryLabel;
private String categoryS;
private ArrayList <JButton> candidatesButton = new ArrayList<JButton>();
private ArrayList<String> candidatesS = new ArrayList<String>();
private int [] numVotes;
private boolean[] clicked=null;
//constructor
public Ballot(int _id,String _category,String [] _candidatesS)
{
//set variables
id = _id;
categoryS = _category;
for (int t= 0; t<_candidatesS.length; t++)
{
candidatesS.add(_candidatesS[t]);
} //end of loop to copy array to arrayList
categoryLabel = new JLabel(categoryS);
numVotes = new int [candidatesS.size()];
//align label
categoryLabel.setHorizontalAlignment(SwingConstants.CENTER);
// set layout for ballot panel
setLayout(new GridLayout(1,candidatesS.size()+1));
//add the category label to the panel
add(categoryLabel);
//create action listener object
ActionListener listener = new BListener();
//loop to create arbitrary number of candidate buttons
for (int i = 0; i < candidatesS.size(); i++)
{
candidatesButton.add(new JButton(candidatesS.get(i)));
clicked[i] = false;
candidatesButton.get(i).addActionListener(listener);
add(candidatesButton.get(i));
} // end of loop
} //end of ballot constructor
//method to return click status
public boolean getStatusClicked(int i)
{
return clicked[i];
}
//accessor methods
public String getCategoryS() {return categoryS;}
public String getCandidateString(int i) {return candidatesS.get(i);} //not right
public int getNumCandidates() {return candidatesButton.size();}
public int getId() {return id;}
public int getVoteCount(int i) {return numVotes[i];}
//mutator methods
//listener to toggle the status of buttons when clicked
private class BListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int i = 0; //counter variable for arraylist calls
for (JButton b: candidatesButton) //change to REGULAR FOR LOOP??
{
if (e.getSource() == candidatesButton.get(i))
{
clicked[i] = !clicked[i];
if (clicked[i])
{
candidatesButton.get(i).setForeground(Color.RED);
numVotes[i]++;
}
else
{
candidatesButton.get(i).setForeground(Color.BLACK);
numVotes[i]--;
}
}
i++; //increment int counter
} // end of cycle through all buttons
} // end of actionperformed
} //end of listener
//internal methods
//private void printToFile(){;}
} // end of ballot class