求助,程序显示没有错误,但是不能运行。
package com.ruanko.view;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.JTextComponent;
import com.ruanko.business.FileOperation;
public class JMainFrame extends JFrame{
private JTextField pathTextField=null;
private JTextField numberTextField=null;
private JTextField nameTextFiel=null;
private JTextField phoneTextField=null;
private JTextField emailTextField=null;
private JTextField addressTextField=null;
private JButton addButton=null;
private JRadioButton maleButton=null;
private JRadioButton femaleButton=null;
private JComboBox relationshipBox=null;
public JMainFrame(){
this.setSize(620,500);
this.setTitle("通讯录");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label=new JLabel("欢迎进入通讯录系统");
this.getContentPane().add(label);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width-600)/2,(screenSize.height-500)/2);
ImageIcon imageIcon=new ImageIcon("image//Contact.png");
Image image=imageIcon.getImage();
this.setIconImage(image);
this.setContentPane(getContentPane());
}
public JPanel getContentPane(){
JPanel mainPanel=new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
mainPanel.add(getAddPanel1());
mainPanel.add(getAddPanel2());
mainPanel.add(getAddPanel3());
mainPanel.add(getAddPanel4());
return mainPanel;
}
private JPanel getAddPanel1(){
JPanel addPanel1=new JPanel();
addPanel1.add(new JLabel("联系人路径"));
JTextField pathTextField = new JTextField(43);
String defaultPath="F:/contacts.txt";
pathTextField.setText(defaultPath);
addPanel1.add(pathTextField);
return addPanel1;
}
private JPanel getAddPanel2(){
JPanel addPanel2=new JPanel();
addPanel2.add( new Label("编号:"));
JTextField numberTextField=new JTextField(12);
addPanel2.add(numberTextField);
addPanel2.add( new Label("姓名:"));
JTextField nameTextField=new JTextField(12);
addPanel2.add(nameTextField);
addPanel2.add( new Label("手机号:"));
JTextField phoneTextField=new JTextField(12);
addPanel2.add(phoneTextField);
return addPanel2;
}
private JPanel getAddPanel3(){
JPanel addPanel3=new JPanel();
addPanel3.add( new Label("email:"));
JTextField emailTextField=new JTextField(12);
addPanel3.add(emailTextField);
addPanel3.add( new Label("地址:"));
JTextField addressTextField=new JTextField(30);
addPanel3.add(addressTextField);
return addPanel3;
}
private JPanel getAddPanel4(){
JPanel addPanel4=new JPanel();
addPanel4.add(new JLabel("性别"));
maleButton=new JRadioButton("男");
femaleButton=new JRadioButton("女");
addPanel4.add(maleButton);
addPanel4.add(femaleButton);
ButtonGroup buttonGroup=new ButtonGroup();
buttonGroup.add(maleButton);
buttonGroup.add(femaleButton);
maleButton.setSelected(true);
addPanel4.add(new JLabel("关系:"));
String[] relationship={"同事","同学","亲戚","朋友"};
relationshipBox=new JComboBox(relationship);
addPanel4.add(relationshipBox);
addPanel4.add(addButton);
return addPanel4;
}
private JButton getAddButton(){
if(addButton==null){
addButton=new JButton("新增联系人");
addButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String number=numberTextField.getText();
JTextComponent nameTextField = null;
String name=nameTextField.getText();
String phone=phoneTextField.getText();
String email=emailTextField.getText();
String address=addressTextField.getText();
String gender=null;
if(maleButton.isSelected()){
gender="男";
}else if(femaleButton.isSelected()){
gender="女";
}
String relationship=(String)relationshipBox.getSelectedItem();
String contactInfor=number+"##"+name+"##"+phone+"##"+email+"##"+address+"##"+gender+"##"+relationship;// TODO Auto-generated method stub
String filePath=pathTextField.getText();
FileOperation fileoperation=new FileOperation();
if(fileoperation.saveContact(contactInfor,filePath)){
JOptionPane.showMessageDialog(null,"联系人保存成功");
}else{
JOptionPane.showMessageDialog(null,"联系人保存失败");
}
}
});
}
return addButton;
}
public static void main(String[] args){
JMainFrame frame=new JMainFrame();
frame.setVisible(true);
}
}
下面的显示这些:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1043)
at java.awt.Container.add(Container.java:363)
at com.ruanko.view.JMainFrame.getAddPanel4(JMainFrame.java:116)
at com.ruanko.view.JMainFrame.getContentPane(JMainFrame.java:48)
at com.ruanko.view.JMainFrame.<init>(JMainFrame.java:30)
at com.ruanko.view.JMainFrame.main(JMainFrame.java:162)