NullPointerException还有Linkedlist sort的问题
这个程序要做一个学生信息的GUI程序,可以update的。要做两种sort,ID和name的。
用Collection.sort(myList);做sort会报错,不知道什么原因【囧……
随便填一点进sortByID和sortByName想看看能不能运行结果是NullPointerException
求牛人debug啊~~~
这是主程序
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class Sherlock extends JFrame
{
private LinkedList<Student> myList = new LinkedList<Student>();
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JTextArea FArea;
private JTextArea SArea;
private JLabel IDLabel;
private JLabel nameLabel;
private JLabel teleLabel;
private JLabel emailLabel;
private JLabel titleLabel;
private JTextField IDField;
private JTextField nameField;
private JTextField teleField;
private JTextField emailField;
private JPanel FPanel;
private JPanel SPanel;
public Sherlock()
{
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
titleLabel= new JLabel("Student Contact Information");
titleLabel.setForeground(Color.BLUE);
titleLabel.setFont((new Font("",1,30)));
JPanel titlePanel = new JPanel();
JPanel FPanel = createFPanel();
FPanel.setLayout(new BorderLayout());
JPanel SPanel = createSPanel();
SPanel.setLayout(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
titlePanel.add(titleLabel);
tabbedPane.addTab("Query Records",FPanel);
tabbedPane.addTab("Update Records",SPanel);
mainPanel.add(titlePanel,BorderLayout.NORTH);
mainPanel.add(tabbedPane,BorderLayout.SOUTH);
add(mainPanel);
firstLoad();
}
public JPanel createFPanel()
{
JPanel FTPanel = new JPanel();
FTPanel.setLayout(new GridLayout(1,5));
FArea = new JTextArea(10,20);
b1 = new JButton("Load File");
b2 = new JButton("Sort by ID");
b3 = new JButton("Sort by Name");
b4 = new JButton("Clear");
b5 = new JButton("Exit");
class DoListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionString = e.getActionCommand();
if (actionString.equals("Load File"))
{
loadFile();
}
if (actionString.equals("Sort by ID"))
{
sortByID();
}
if (actionString.equals("Sort by Name"))
{
sortByName();
}
if (actionString.equals("Clear"))
{
clear();
}
if (actionString.equals("Exit"))
{
System.exit(0);
}
}
}
ActionListener listener = new DoListener();
b1.addActionListener(listener);
b2.addActionListener(listener);
b3.addActionListener(listener);
b4.addActionListener(listener);
b5.addActionListener(listener);
FTPanel.add(b1);
FTPanel.add(b2);
FTPanel.add(b3);
FTPanel.add(b4);
FTPanel.add(b5);
FPanel.add(FTPanel,BorderLayout.NORTH);
FPanel.add(FArea,BorderLayout.SOUTH);
return FPanel;
}
public JPanel createSPanel()
{
JPanel STPanel = new JPanel();
STPanel.setLayout(new GridLayout(2,4));
JPanel SBPanel = new JPanel();
SBPanel.setLayout(new GridLayout(1,4));
SArea = new JTextArea(10,20);
IDLabel = new JLabel("Student ID: ");
nameLabel = new JLabel("Student Name: ");
teleLabel = new JLabel("Telephone: ");
emailLabel = new JLabel("Email Address: ");
IDField = new JTextField();
nameField = new JTextField();
teleField = new JTextField();
emailField = new JTextField();
STPanel.add(IDLabel);
STPanel.add(IDField);
STPanel.add(nameLabel);
STPanel.add(nameField);
STPanel.add(teleLabel);
STPanel.add(teleField);
STPanel.add(emailLabel);
STPanel.add(emailField);
b6 = new JButton("Update Recond");
b7 = new JButton("Save File");
b8 = new JButton("Clear");
b9 = new JButton("Exit");
class DoListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionString = e.getActionCommand();
if (actionString.equals("Update Recond"))
{
updateRecond();
}
if (actionString.equals("Save File"))
{
saveFile();
}
if (actionString.equals("Clear"))
{
clear();
}
if (actionString.equals("Exit"))
{
System.exit(0);
}
}
}
ActionListener listener = new DoListener();
b6.addActionListener(listener);
b7.addActionListener(listener);
b8.addActionListener(listener);
b9.addActionListener(listener);
SBPanel.add(b6);
SBPanel.add(b7);
SBPanel.add(b8);
SBPanel.add(b9);
SPanel.add(STPanel,BorderLayout.NORTH);
SPanel.add(SArea,BorderLayout.CENTER);
SPanel.add(SBPanel,BorderLayout.SOUTH);
return SPanel;
}
public void firstLoad()
{
try
{
Scanner input = new Scanner(new FileReader("E:\\StudentContacts.csv"));
String myEntry = "" ;
int ID =0;
String name ="";
String phone="";
String email ="";
while(input.hasNextLine())
{
myEntry = input.nextLine();
StringTokenizer st = new StringTokenizer(myEntry,",");
while(st.hasMoreTokens())
{
ID = Integer.parseInt(st.nextToken());
name = st.nextToken();
phone = st.nextToken();
email = st.nextToken();
myList.addLast(new Student (ID,name,phone,email));
}
}// end of while loop
input.close();
}
catch(IOException ex)
{
System.out.println("file loading failed.");
}
}
public void loadFile()
{
String outputMsg = "Student ID\t Student Name\t\t Phone \t Email\t\n+\n\n";
for (int i=0; i<myList.size();i++)
{
outputMsg = outputMsg + myList.get(i).toString();
}
FArea.setText(outputMsg);
SArea.setText(outputMsg);
}
public void sortByID()
{
Collections.sort(myList);
}
public void sortByName()
{
FArea.setText("");
SArea.setText("");
}
public void updateRecond()
{
int newID = 0;
String newName ="";
String newPhone ="";
String newEmail ="";
String tempID ="";
boolean inputNumber = false;
while (inputNumber == false)
{
try
{
tempID = IDField.getText();
newID = Integer.parseInt(tempID);
if (!isExistID(newID))
{
inputNumber = true;
newName = nameField.getText();
newPhone = teleField.getText();
newEmail = emailField.getText();
}
else
{
inputNumber = false;
newPhone = teleField.getText();
newEmail = emailField.getText();
}
}
catch (NumberFormatException e)
{
inputNumber = false;
JOptionPane.showMessageDialog(null,"You must enter an integer!","",
JOptionPane.INFORMATION_MESSAGE);
}
}
Student newStudent = new Student (newID,newName,newPhone,newEmail);
myList.add(newStudent);
loadFile();
}
public boolean isExistID(int s)
{
for (int i=0; i<myList.size();i++)
{
if (s == myList.get(i).getId())
{
return true;
}
}
return false;
}
public void saveFile()
{
try
{
PrintWriter out = new PrintWriter(new FileWriter("E:\\StudentContacts.csv"));
int ID = 0;
String name ="";
String phone ="";
String email="";
for (int i = 0; i < myList.size(); i++)
{
Student s = myList.get(i);
ID = s.getId();
name = s.getName();
phone = s.getPhone();
email = s.getEmail();
out.println(ID+ "," + name + "," + phone +","+ email);
}
out.close();
JOptionPane.showMessageDialog(null,"Data saved successfully!","",
JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception ex)
{
System.out.println("save file fail");
}
}
public void clear()
{
FArea.setText("");
SArea.setText("");
}
public static void main(String[] args)
{
JFrame myApp = new Sherlock();
myApp.setVisible(true);
myApp.setResizable(false);
myApp.setSize(700, 400);
myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
这是Student.java的程序
public class Student {
private int ID;
private String name;
private String phone;
private String email;
public Student(int i, String n, String p, String e)
{
ID = i;
name = n;
phone = p;
email = e;
}
public void setId(int i)
{
ID = i;
}
public void setName(String n)
{
name = n;
}
public void setPhone(String p)
{
phone = p;
}
public void setEmail(String e)
{
email = e;
}
public int getId()
{
return ID;
}
public String getName()
{
return name;
}
public String getPhone()
{
return phone;
}
public String getEmail()
{
return email;
}
public String toString()
{
return " " + ID + "\t" + name+ "\t\t" + phone + "\t" + email + "\n";
}
}
[解决办法]
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
}
}
这个是Connections sort方法的源码 可以看到List中的T是要实现Comparable接口的,,也就是说你的Student类要实现Comparable借口才能用此方法。。。
[解决办法]
楼上说的对.
我改了一下,在Student类里加上了 compareTo方法。
另外按照name排序的话,编了一个实现了java.util.Comparator的类,Compare2,实现按name比较。
程序做了部分修改,我调试能运行,楼主参考一下。
Student类:
public class Student implements Comparable //要实现Comparable接口。{ private int ID; private String name; private String phone; private String email; public Student(int i, String n, String p, String e) { ID = i; name = n; phone = p; email = e; } public void setId(int i) { ID = i; } public void setName(String n) { name = n; } public void setPhone(String p) { phone = p; } public void setEmail(String e) { email = e; } public int getId() { return ID; } public String getName() { return name; } public String getPhone() { return phone; } public String getEmail() { return email; } public int compareTo(Object s) //实现接口Comparable里的compareTo方法。 { int result=(this.getId()>((Student)s).getId())?1:-1; //ID大,返回1,否则返回-1. return result; } public String toString() {return " " + ID + "\t" + name+ "\t\t" + phone + "\t" + email + "\n"; }}