Hi I'm having a lot of trouble with a program I'm doing for some coursework. At the moment my main problems are :
嗨,我在做一些课程作业时遇到了很多麻烦。目前我的主要问题是:
-
The program wont let me use the savep button which calls the savepart() method as it throws the error above.
程序不允许我使用savep按钮,当它抛出上面的错误时,它调用savepart()方法。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Menu.savepart(Menu.java:511) at Menu.actionPerformed(Menu.java:435) at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source) at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source)
-
I can't seem to push the mypart vector into a Jlist as well because of the same error. I want to be able to select multiple participants then write those into a relationships vector.
由于同样的错误,我似乎无法将mypart向量推入Jlist。我想要能够选择多个参与者然后把它们写进一个关系向量。
Yes I know a database would be better to do this all in ... but its a programming course
是的,我知道用数据库做这一切会更好。但这是一门编程课程
public class Menu extends JPanel implements ActionListener {
private static final boolean shouldFill = true;
private static final boolean shouldWeightX = true;
private static String[] dayofthemonth = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31" };
private static String[] monthoftheyear = { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12" };
private static String[] yearoftheyear = { "2009", "2010", "2011", "2012", "2013",
"2014", "2015", "2016", "2017", "2018", "2019", "2020" };
private static String[] hour = { "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23" };
private static String[] minute = { "10", "20", "30", "40", "50", "60"};
private static String[] satype = { "Meeting", "Teleconference", "Lunch"};
private JTabbedPane tabbedPane = new JTabbedPane();
static Vector<appointment> myappt = appointment.readFile("appointment.txt");
static Vector<participant> mypart = participant.readFile("participant.txt");
static Vector<relationships> myrel = relationships.readFile("relationships.txt");
private JComboBox day = new JComboBox(dayofthemonth);
private JComboBox month = new JComboBox(monthoftheyear);
private JComboBox year = new JComboBox(yearoftheyear);
private JComboBox shour = new JComboBox(hour);
private JComboBox sminute = new JComboBox(minute);
private JComboBox dday = new JComboBox(dayofthemonth);
private JComboBox dmonth = new JComboBox(monthoftheyear);
private JComboBox dyear = new JComboBox(yearoftheyear);
private JComboBox dhour = new JComboBox(hour);
private JComboBox dminute = new JComboBox(minute);
private JComboBox atype = new JComboBox(satype);
private JTextField atitletext = new JTextField (40);
private static JTextField pfname = new JTextField (30);
private static JTextField plname = new JTextField (30);
private static JTextField ploc = new JTextField (30);
private static JTextField pjtitle = new JTextField (30);
private JTextArea adescriptiontext = new JTextArea(5,1);
static DefaultListModel model = new DefaultListModel();
JList plist = new JList(model);
static int noparticipant;
static int noappointment;
private final String FORMAT = "dd/MM/yyyy,HH:mm";
public Menu() {
GridBagConstraints c = new GridBagConstraints();
ImageIcon icon = createImageIcon("images/middle.gif");
if (shouldFill) {
// natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
// panel1
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 8;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.weightx = 1;
tabbedPane.addTab("Add A New Appointment", panel);
// Create the combo box
JLabel start = new JLabel("Start Date:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
panel.add(start, c);
JLabel jl1 = new JLabel("Day:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
panel.add(jl1, c);
day.setSelectedIndex(0);
day.addActionListener(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
panel.add(day, c);
JLabel jl2 = new JLabel("Month:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
panel.add(jl2, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 4;
c.gridy = 1;
panel.add(month, c);
JLabel jl3 = new JLabel("Year:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 6;
c.gridy = 1;
panel.add(jl3, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 7;
c.gridy = 1;
panel.add(year, c);
JLabel jstime = new JLabel("Start Time:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
panel.add(jstime, c);
JLabel jshour = new JLabel("Start Hour:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
panel.add(jshour, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
panel.add(shour, c);
JLabel jsminute = new JLabel("Start Minute:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 3;
panel.add(jsminute, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 4;
c.gridy = 3;
panel.add(sminute, c);
JLabel end = new JLabel("End Date:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 5;
panel.add(end, c);
JLabel j20 = new JLabel("Day:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 6;
panel.add(j20, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 6;
panel.add(dday, c);
JLabel jl21 = new JLabel("Month:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 6;
panel.add(jl21, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 4;
c.gridy = 6;
panel.add(dmonth, c);
JLabel j23 = new JLabel("Year:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 6;
c.gridy = 6;
panel.add(j23, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 7;
c.gridy = 6;
panel.add(dyear, c);
JLabel jdtime = new JLabel("End Time:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 8;
panel.add(jdtime, c);
JLabel jdhour = new JLabel("End Hour:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 9;
panel.add(jdhour, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 9;
panel.add(dhour, c);
JLabel jdminute = new JLabel("End Minute:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 9;
panel.add(jdminute, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 4;
c.gridy = 9;
panel.add(dminute, c);
JLabel space = new JLabel(" ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 10;
panel.add(space, c);
JLabel atitle = new JLabel("Appointment Title:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 12;
panel.add(atitle, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 12;
panel.add(atitletext, c);
JLabel jtype = new JLabel("Appointment Type:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 4;
c.gridy = 12;
panel.add(jtype, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 6;
c.gridy = 12;
panel.add(atype, c);
JLabel adescription = new JLabel("Appointment Description:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 14;
panel.add(adescription, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40;
c.weighty = 1.0;
c.gridwidth = 4;
c.gridx = 2;
c.gridy = 14;
panel.add(adescriptiontext, c);
JLabel aplist = new JLabel("Participant List:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 16;
panel.add(aplist, c);
populateplist();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 60;
c.weighty = 1.0;
c.gridwidth = 4;
c.gridx = 2;
c.gridy = 16;
panel.add(plist, c);
JButton savea = new JButton("Save Appointment");
savea.setActionCommand("SaveA");
savea.addActionListener(this); // set this object as the listener
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 6;
c.gridy = 20;
panel.add(savea, c); // add 'save' button to the panel
// pane 2
GridBagConstraints p2 = new GridBagConstraints();
if (shouldFill) {
// natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
JPanel panel1 = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.5;
tabbedPane.addTab("Add A New Participant", panel1);
JLabel partinfo = new JLabel("Enter New Participant Info:");
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 0;
p2.gridy = 0;
panel1.add(partinfo, p2);
JLabel jpfname = new JLabel("Enter First Name");
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 0;
p2.gridy = 1;
panel1.add(jpfname, p2);
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 2;
p2.gridy = 1;
panel1.add(pfname, p2);
JLabel jplname = new JLabel("Enter Last Name");
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 0;
p2.gridy = 2;
panel1.add(jplname, p2);
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 2;
p2.gridy = 2;
panel1.add(plname, p2);
JLabel jpjtitle2 = new JLabel("Enter Location");
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 0;
p2.gridy = 4;
panel1.add(jpjtitle2, p2);
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 2;
p2.gridy = 4;
panel1.add(ploc, p2);
JLabel jpjtitle = new JLabel("Enter Job Title");
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 0;
p2.gridy = 6;
panel1.add(jpjtitle, p2);
p2.fill = GridBagConstraints.HORIZONTAL;
p2.gridx = 2;
p2.gridy = 6;
panel1.add(pjtitle, p2);
JButton savep = new JButton("Save Participant");
savep.setActionCommand("SaveP");
savep.addActionListener(this); // set this object as the listener
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 7;
panel1.add(savep, c); // add 'save' button to the panel
// pane 3
JPanel panel2 = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.5;
tabbedPane.addTab("Delete an Appointment", panel2);
// pane 4
JPanel panel3 = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.5;
tabbedPane.addTab("Delete Participant", panel3);
// pane 5
JPanel panel5 = new JPanel(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.5;
tabbedPane.addTab("Search", panel5);
// Add the tabbed pane to this panel.
add(tabbedPane);
// The following line enables to use scrolling tabs.
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
// TODO Auto-generated constructor stub
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Menu.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void actionPerformed(ActionEvent evt) {
String strAction = evt.getActionCommand();
if (strAction.equals("SaveA")) {
JOptionPane.showMessageDialog(this, "You have saved 'Hello'",
"Hello!", JOptionPane.INFORMATION_MESSAGE);
} else if (strAction.equals("SaveP")) {
savepart();
JOptionPane.showMessageDialog(this, "You have saved 'Hello'",
"Hello!", JOptionPane.INFORMATION_MESSAGE);
}
}
public void saveappt() {
String ssday;
String ssmonth;
String ssyear;
String sshour;
String ssminute;
String seday;
String semonth;
String seyear;
String sehour;
String seminute;
String satitle;
String satype;
String sadescription;
String startdt;
String enddt;
try{
ssday = (String) day.getSelectedItem();
ssmonth = (String) month.getSelectedItem();
ssyear = (String) year.getSelectedItem();
sshour = (String) shour.getSelectedItem();
ssminute = (String) sminute.getSelectedItem();
seday = (String) dday.getSelectedItem();
semonth = (String) dmonth.getSelectedItem();
seyear = (String) dyear.getSelectedItem();
sehour = (String) dhour.getSelectedItem();
seminute = (String) dminute.getSelectedItem();
satitle = (String) atitletext.getText();
sadescription= (String) adescriptiontext.getText();
satype = (String) atype.getSelectedItem();
startdt = ssday + "/" + ssmonth + "/" + ssyear + "," + sshour + ":" + ssminute;
enddt = seday + "/" + semonth + "/" + seyear + "," + sehour + ":" + seminute;
Date startdate;
Date enddate;
ParsePosition pos;
pos = new ParsePosition(0);
SimpleDateFormat checker = new SimpleDateFormat ("dd/MM/yyyy,hh:mm");
startdate = checker.parse(startdt,pos);
ParsePosition pos1;
pos1 = new ParsePosition(0);
enddate = checker.parse(enddt,pos1);
noappointment++;
int aid=noappointment;
appointment z =new appointment (aid,startdate,enddate,satitle,sadescription,satype);
myappt.add(z);
makestore();
}catch(Exception e){
}
}
public static void savepart() {
String sfname;
String slname;
String sloc;
String sjtitle;
sfname = (String) pfname.getText();
slname = (String) plname.getText();
sloc = (String) ploc.getText();
sjtitle = (String) pjtitle.getText();
// noparticipant++;
// int pid=noparticipant;
int pid=1;
participant z =new participant (pid,sfname,slname,sloc,sjtitle);
mypart.add(z);
}
private static void populateplist() {
if(mypart!=null){
model.clear();
//creates an instance of the addressbook object to use to reflect a record in the vector.
participant thisparticipant;
//loop through each element of the vector - for loop blueprint taken from "Citation.java
for (int n = 0; n < mypart.size(); n++){
//equates thisaddress with an element in the vector
thisparticipant = (participant) mypart.elementAt(n);
// Prints out the entry number and details from the Vector
String pname = thisparticipant.firstname +thisparticipant.lastname;
model.addElement(pname);
}
}else{
model.addElement("No Participants Please Add One");
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Appointment System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new Menu(), BorderLayout.CENTER);
// Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
private static void makestore(){
appointment thisappointment; // Create an instance of citation to act as a temporary store
participant thisparticipant;
relationships thisrelationships;
try{
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("appointment.txt")));
//loop through each element of the vector - loop blueprint taken from "Citation.java"
for (int n=0; n<myappt.size(); n++)
{
//points thisaddress at the right element of the vector
thisappointment=(appointment)myappt.elementAt(n);
outFile.print(thisappointment.appointmentid);
outFile.print(";");
outFile.print(" ");
outFile.print(thisappointment.startdatetime);
outFile.print(";");
outFile.print(" ");
outFile.print(thisappointment.enddatetime);
outFile.print(";");
outFile.print(" ");
outFile.print(thisappointment.appttype);
outFile.print(";");
outFile.print(" ");
outFile.print(thisappointment.apptdescription);
outFile.print(";");
outFile.print(" ");
}
// closes the output file.
outFile.close();
//catches any input/output errors from "Simple File Writing"
}catch(IOException e){
System.out.println("An i/o error has occurred ["+e+"]");
}
try{
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("participant.txt")));
//loop through each element of the vector - loop blueprint taken from "Citation.java"
for (int n=0; n<mypart.size(); n++)
{
//points thisaddress at the right element of the vector
thisparticipant=(participant)mypart.elementAt(n);
outFile.print(thisparticipant.firstname);
outFile.print(";");
outFile.print(" ");
outFile.print(thisparticipant.lastname);
outFile.print(";");
outFile.print(" ");
outFile.print(thisparticipant.location);
outFile.print(";");
outFile.print(" ");
outFile.print(thisparticipant.jobtitle);
outFile.print(";");
outFile.print(" ");
}
// closes the output file.
outFile.close();
//catches any input/output errors from "Simple File Writing"
}catch(IOException e){
System.out.println("An i/o error has occurred ["+e+"]");
}
try{
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("relationships.txt")));
//loop through each element of the vector - loop blueprint taken from "Citation.java" by
for (int n=0; n<myrel.size(); n++)
{
//points thisaddress at the right element of the vector
thisrelationships=(relationships)myrel.elementAt(n);
outFile.print(thisrelationships.appointmentid);
outFile.print(";");
outFile.print(" ");
outFile.print(thisrelationships.participantid);
outFile.print(";");
outFile.print(" ");
}
// closes the output file.
outFile.close();
//catches any input/output errors from "Simple File Writing" by
}catch(IOException e){
System.out.println("An i/o error has occurred ["+e+"]");
}
}
//constructs the relationship object and way of creating a record
private static class relationships
{
int participantid;
int appointmentid;
static int norelationships;
relationships(int pid, int aid)
{
participantid =pid;
appointmentid =aid;
}
public static Vector<relationships> readFile(String fileName) {
boolean rexists = (new File(fileName)).exists();
if(rexists){
Vector<relationships> addid = new Vector<relationships>();
try {
Scanner scanner =
new Scanner(new File(fileName));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
addid.addElement(parseLine(scanner.next()));
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Please check your inported text file and change all delimiters to ;");
}
}
else{
try{
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("relationships.txt")));
//loop through each element of the vector - loop blueprint taken from "Citation.java"
// closes the output file.
outFile.close();
//catches any input/output errors from "Simple File Writing"
}catch(IOException e){
System.out.println("An i/o error has occurred ["+e+"]");
}
}
return myrel;
}
private static relationships parseLine(String line) {
Scanner lineScanner = new Scanner(line);
try{
lineScanner.useDelimiter("\\s*;\\s*");// Delimiter is 0 or more spaces then a semicolon then zero or more spaces
int pid = lineScanner.nextInt();
int aid = lineScanner.nextInt();
relationships c =new relationships (pid,aid);
return c;
}
catch(Exception e){
System.out.println("Please check your relationships text file and change all delimiters to ;");
}
return null;
}
}
private static class participant
{
int participantid;
String firstname;
String lastname;
String location;
String jobtitle;
;
static int noPrintappt;
participant(int pid,String fn, String ln, String loc, String jt) // The constructor
{
noparticipant++; // count total number of addresses
participantid=noparticipant;
participantid=pid;// set instance variables
firstname=fn;
lastname=ln;
location=loc;
jobtitle=jt;
}
int printappt() // NB printRef now returns an int
{
noPrintappt++; // count number of times printref has been called
System.out.println ("Participant ID:" + participantid);
System.out.println ("Name:" + firstname + lastname);
System.out.println ("Location:" + location);
System.out.println ("Job Title: " + jobtitle);
return noPrintappt; // set the return value for the method
// (must be an int, as declared above)
}
public static Vector<participant> readFile(String fileName) {
boolean pexists = (new File(fileName)).exists();
if(pexists){
Vector<participant> addid = new Vector<participant>();
try {
Scanner scanner =
new Scanner(new File(fileName));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
addid.addElement(parseLine(scanner.next()));
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Please check your inported text file and change all delimiters to ;");
}
}
else{
try{
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("participant.txt")));
//loop through each element of the vector - loop blueprint taken from "Citation.java"
// closes the output file.
outFile.close();
//catches any input/output errors from "Simple File Writing"
}catch(IOException e){
System.out.println("An i/o error has occurred ["+e+"]");
}
}
return mypart;
}
private static participant parseLine(String line) {
Scanner lineScanner = new Scanner(line);
try{
lineScanner.useDelimiter("\\s*;\\s*");// Delimiter is 0 or more spaces then a semicolon then zero or more spaces
int pid = lineScanner.nextInt();
String fn = lineScanner.next();
String ln = lineScanner.next();
String loc = lineScanner.next();
String jt = lineScanner.next();
participant c =new participant (pid,fn,ln,loc,jt);
return c;
}
catch(Exception e){
System.out.println("Please check your participant text file and change all delimiters to ;");
}
return null;
}
}
//constructs the appointment object and way of creating a record
private static class appointment
{
int appointmentid;
Date startdatetime;
Date enddatetime;
String appttitle;
String apptdescription;
String appttype;
static int noPrintappt;
appointment(int aid,Date sdt, Date edt,String atitle, String adesc, String atype) // The constructor
{
noappointment++; // count total number of addresses
appointmentid=noappointment;// set instance variables
aid= appointmentid;
startdatetime=sdt;
enddatetime=edt;
appttitle=atitle;
apptdescription=adesc;
appttype=atype;
}
int printappt() // NB printRef now returns an int
{
noPrintappt++; // count number of times printref has been called
System.out.println ("Start Date and Time :" + startdatetime);
System.out.println ("End Date and Time :" + enddatetime);
System.out.println ("Appointment Type:" + appttype);
System.out.println (appttitle +" - "+apptdescription);
return noPrintappt; // set the return value for the method
// (must be an int, as declared above)
}
public static Vector<appointment> readFile(String fileName) {
boolean aexists = (new File(fileName)).exists();
if(aexists){
Vector<appointment> addid = new Vector<appointment>();
try {
Scanner scanner =
new Scanner(new File(fileName));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
addid.addElement(parseLine(scanner.next()));
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Please check your inported text file and change all delimiters to ;");
}
}
else{
try{
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("appointment.txt")));
//loop through each element of the vector - loop blueprint taken from "Citation.java"
// closes the output file.
outFile.close();
//catches any input/output errors from "Simple File Writing"
}catch(IOException e){
System.out.println("An i/o error has occurred ["+e+"]");
}
}
return myappt;
}
private static appointment parseLine(String line) {
Scanner lineScanner = new Scanner(line);
try{
lineScanner.useDelimiter("\\s*;\\s*");// Delimiter is 0 or more spaces then a semicolon then zero or more spaces
int aid = lineScanner.nextInt();
String sdt = lineScanner.next();
String edt = lineScanner.next();
String atitle = lineScanner.next();
String adesc = lineScanner.next();
String atype = lineScanner.next();
Date startdate;
Date enddate;
ParsePosition pos;
pos = new ParsePosition(0);
SimpleDateFormat checker = new SimpleDateFormat ("dd/MM/yyyy,hh:mm");
startdate = checker.parse(sdt,pos);
ParsePosition pos1;
pos1 = new ParsePosition(0);
enddate = checker.parse(edt,pos1);
appointment c =new appointment (aid,startdate,enddate,atitle,adesc,atype);
return c;
}
catch(Exception e){
System.out.println("Please check your appointment text file for date format errors or where ; is not used as a delimiter");
}
return null;
}
}
}
2 个解决方案
#1
0
There is a problem with your participant.readFile
method. You should be returning the addid
vector, not mypart
, which is null.
你的参与者有一个问题。readFile方法。你应该返回addid向量,而不是mypart,它是空的。
public static Vector<participant> readFile(String fileName) {
boolean pexists = (new File(fileName)).exists();
if (pexists) {
Vector<participant> addid = new Vector<participant>();
try {
Scanner scanner = new Scanner(new File(fileName));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
addid.addElement(parseLine(scanner.next()));
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Please check your inported text file and change all delimiters to ;");
}
} else {
try {
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("participant.txt")));
// loop through each element of the vector - loop blueprint taken from "Citation.java" by Amadeo and
// Adam Moore.for G54PRG
// closes the output file.
outFile.close();
// catches any input/output errors from "Simple File Writing" by Amadeo and Adam Moore.for G54PRG
} catch (IOException e) {
System.out.println("An i/o error has occurred [" + e + "]");
}
}
return mypart; //<--- this is null!
}
#2
0
The problem is located in the readFile(String fileName)
method of your participant
class: you add the values from the file to the local variable addid
but return mypart
instead, which is null
.
问题位于您的participant类的readFile(字符串文件名)方法中:您将文件中的值添加到本地变量addid,但返回mypart,它是空的。
#1
0
There is a problem with your participant.readFile
method. You should be returning the addid
vector, not mypart
, which is null.
你的参与者有一个问题。readFile方法。你应该返回addid向量,而不是mypart,它是空的。
public static Vector<participant> readFile(String fileName) {
boolean pexists = (new File(fileName)).exists();
if (pexists) {
Vector<participant> addid = new Vector<participant>();
try {
Scanner scanner = new Scanner(new File(fileName));
scanner.useDelimiter(System.getProperty("line.separator"));
while (scanner.hasNext()) {
addid.addElement(parseLine(scanner.next()));
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Please check your inported text file and change all delimiters to ;");
}
} else {
try {
// creates a printwriter which outputs to address.txt
PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter("participant.txt")));
// loop through each element of the vector - loop blueprint taken from "Citation.java" by Amadeo and
// Adam Moore.for G54PRG
// closes the output file.
outFile.close();
// catches any input/output errors from "Simple File Writing" by Amadeo and Adam Moore.for G54PRG
} catch (IOException e) {
System.out.println("An i/o error has occurred [" + e + "]");
}
}
return mypart; //<--- this is null!
}
#2
0
The problem is located in the readFile(String fileName)
method of your participant
class: you add the values from the file to the local variable addid
but return mypart
instead, which is null
.
问题位于您的participant类的readFile(字符串文件名)方法中:您将文件中的值添加到本地变量addid,但返回mypart,它是空的。