请问以下的代码一运行就会报错,是为什么呢?
报错信息是:java.lang.NullPointerException: Cannot read field "parent" because "comp" is null;
这个怎么解决呢?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SignIn extends JFrame {
private JPanel contentPane;
private JLabel label;
private JLabel label_1;
private JLabel label_2;
private JTextField usernameField;
private JLabel label_3;
private JTextField nameField;
private JLabel label_4;
private JLabel label_5;
private JTextField idNoField;
private JRadioButton idCardButton;
private JRadioButton passportButton;
private JRadioButton twCardButton;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SignIn frame = new SignIn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SignIn() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
label = new JLabel("用户名:");
label.setBounds(110, 52, 54, 15);
contentPane.add(label);
usernameField = new JTextField();
usernameField.setBounds(174, 49, 134, 21);
contentPane.add(usernameField);
usernameField.setColumns(10);
label_1 = new JLabel("密码:");
label_1.setBounds(110, 83, 54, 15);
contentPane.add(label_1);
passwordField1 = new JPasswordField();
passwordField1.setBounds(174, 80, 134, 21);
contentPane.add(passwordField1);
label_2 = new JLabel("确认密码:");
label_2.setBounds(110, 113, 60, 15);
contentPane.add(label_2);
passwordField2 = new JPasswordField();
passwordField2.setBounds(174, 110, 134, 21);
contentPane.add(passwordField2);
label_3 = new JLabel("姓名:");
label_3.setBounds(112, 143, 52, 15);
contentPane.add(label_3);
nameField = new JTextField();
nameField.setBounds(174, 140, 134, 21);
contentPane.add(nameField);
nameField.setColumns(20);
label_4 = new JLabel("证件类型:");
label_4.setBounds(110, 174, 60, 15);
contentPane.add(label_4);
label_5 = new JLabel("证件号码");
label_5.setBounds(110, 206, 60, 15);
contentPane.add(label_5);
idNoField = new JTextField();
idNoField.setBounds(174, 202, 200, 21);
contentPane.add(idCardButton);
idNoField.setColumns(50);
idCardButton = new JRadioButton("身份证");
idCardButton.setBounds(174, 170, 66, 23);
contentPane.add(idCardButton);
passportButton = new JRadioButton("护照");
passportButton.setBounds(242, 170, 52, 23);
contentPane.add(passportButton);
twCardButton = new JRadioButton("台胞证");
twCardButton.setBounds(296, 170, 66, 23);
contentPane.add(twCardButton);
ButtonGroup idTypeGroup = new ButtonGroup();
idTypeGroup.add(idCardButton);
idTypeGroup.add(passportButton);
idTypeGroup.add(twCardButton);
idCardButton.setSelected(true);
}
//获取输入
public String getUsername(){
String username = usernameField.getText();
return username;
}
public String getPassword1(){
String password1 = String.valueOf(passwordField1.getPassword());
return password1;
}
public String getPassword2(){
String password2 = String.valueOf(passwordField2.getPassword());
return password2;
}
public String getTrueName(){
String name = nameField.getText();
return name;
}
public String getIdType(){
boolean idcard = false,passport = false,twcard = false;
idcard = idCardButton.isSelected();
passport = passportButton.isSelected();
twcard = twCardButton.isSelected();
String idType = null;
if (idcard){
idType = "身份证";
}else if (passport){
idType = "护照";
}else if (twcard){
idType = "台胞证";
}
return idType;
}
public String getIdNo(){
String idNo = idNoField.getText();
return idNo;
}
}
删除88行的代码,还没有实现不能添加,修改后的代码如下。
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class SignIn extends JFrame {
private JPanel contentPane;
private JLabel label;
private JLabel label_1;
private JLabel label_2;
private JTextField usernameField;
private JLabel label_3;
private JTextField nameField;
private JLabel label_4;
private JLabel label_5;
private JTextField idNoField;
private JRadioButton idCardButton;
private JRadioButton passportButton;
private JRadioButton twCardButton;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SignIn frame = new SignIn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SignIn() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
label = new JLabel("用户名:");
label.setBounds(110, 52, 54, 15);
contentPane.add(label);
usernameField = new JTextField();
usernameField.setBounds(174, 49, 134, 21);
contentPane.add(usernameField);
usernameField.setColumns(10);
label_1 = new JLabel("密码:");
label_1.setBounds(110, 83, 54, 15);
contentPane.add(label_1);
passwordField1 = new JPasswordField();
passwordField1.setBounds(174, 80, 134, 21);
contentPane.add(passwordField1);
label_2 = new JLabel("确认密码:");
label_2.setBounds(110, 113, 60, 15);
contentPane.add(label_2);
passwordField2 = new JPasswordField();
passwordField2.setBounds(174, 110, 134, 21);
contentPane.add(passwordField2);
label_3 = new JLabel("姓名:");
label_3.setBounds(112, 143, 52, 15);
contentPane.add(label_3);
nameField = new JTextField();
nameField.setBounds(174, 140, 134, 21);
contentPane.add(nameField);
nameField.setColumns(20);
label_4 = new JLabel("证件类型:");
label_4.setBounds(110, 174, 60, 15);
contentPane.add(label_4);
label_5 = new JLabel("证件号码");
label_5.setBounds(110, 206, 60, 15);
contentPane.add(label_5);
idNoField = new JTextField();
idNoField.setBounds(174, 202, 200, 21);
idNoField.setColumns(50);
idCardButton = new JRadioButton("身份证");
idCardButton.setBounds(174, 170, 66, 23);
contentPane.add(idCardButton);
passportButton = new JRadioButton("护照");
passportButton.setBounds(242, 170, 52, 23);
contentPane.add(passportButton);
twCardButton = new JRadioButton("台胞证");
twCardButton.setBounds(296, 170, 66, 23);
contentPane.add(twCardButton);
ButtonGroup idTypeGroup = new ButtonGroup();
idTypeGroup.add(idCardButton);
idTypeGroup.add(passportButton);
idTypeGroup.add(twCardButton);
idCardButton.setSelected(true);
}
//获取输入
public String getUsername(){
String username = usernameField.getText();
return username;
}
public String getPassword1(){
String password1 = String.valueOf(passwordField1.getPassword());
return password1;
}
public String getPassword2(){
String password2 = String.valueOf(passwordField2.getPassword());
return password2;
}
public String getTrueName(){
String name = nameField.getText();
return name;
}
public String getIdType(){
boolean idcard = false,passport = false,twcard = false;
idcard = idCardButton.isSelected();
passport = passportButton.isSelected();
twcard = twCardButton.isSelected();
String idType = null;
if (idcard){
idType = "身份证";
}else if (passport){
idType = "护照";
}else if (twcard){
idType = "台胞证";
}
return idType;
}
public String getIdNo(){
String idNo = idNoField.getText();
return idNo;
}
}