0

我有一个类,它有一个 Editbutton 并将其设置为 actionListener。我写了我的全班代码。

我有 3 节课,

public class UserPage extends JFrame implements ActionListener {

private AllUser userModel;
private JTable uTable;
JButton editButton;

public UserPage() {
    setTitle("All User Information");
    userModel = new AllUser();
    uTable = new JTable(userModel);
    add(new JScrollPane(uTable), BorderLayout.CENTER);
    add(buttonPanels(), BorderLayout.PAGE_START);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800, 600);
    this.setLocation(300, 60);
}

public final JPanel buttonPanels() {
    JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    editButton=new JButton("Edit");

    editButton.addActionListener(this);

    buttonsPanel.add(editButton);

    return buttonsPanel;
}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()== editButton){
        int selectedRow=uTable.getSelectedRow();
        if(selectedRow>0){
            editUser(selectedRow);

        }
        else{
            JOptionPane.showMessageDialog(null, "Select a row");
        }

    }
}

public static void main(String[] args) {
    new UserPage().setVisible(true);
}

public void editUser(int row){
    UserInformation userInf=new UserInformation();
    userModel.getSelectedMember(row);
    NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf);
}
}

其他类: 其他类:

public class NewUserFrame_Edit extends javax.swing.JFrame {

public NewUserFrame_Edit( UserInformation userinf) {
    initComponents();
    setLocation(390, 20);

    jTextField1.setText(userinf.getFname());
    jTextField2.setText(userinf.getLname());

    if(userinf.getGender().equals("Male")){    
        jRadioButton1.setSelected(true);
    }
    else{
        jRadioButton2.setSelected(true);
    }

    jTextField3.setText(userinf.getDate());
    setVisible(true);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton2 = new javax.swing.JRadioButton();
    updateButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(51, 0, 0));
    jLabel1.setText("Edit User");

    jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel2.setText("First Name");

    jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel3.setText("Last Name");

    jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel4.setText("Date");

    jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
    jLabel5.setText("Gender");

    jRadioButton1.setText("Male");

    jRadioButton2.setText("Female");

    updateButton.setText("Update");
    updateButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            updateButtonActionPerformed(evt);
        }
    });

    cancelButton.setText("Cancel");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(106, 106, 106)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jLabel5)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jRadioButton2)
                                .addComponent(jRadioButton1)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(updateButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cancelButton))
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(292, 292, 292)
                    .addComponent(jLabel1)))
            .addContainerGap(258, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(21, 21, 21)
            .addComponent(jLabel1)
            .addGap(51, 51, 51)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(32, 32, 32)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel5)
                .addComponent(jRadioButton1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jRadioButton2)
            .addGap(35, 35, 35)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLabel4)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(updateButton)
                .addComponent(cancelButton))
            .addGap(52, 52, 52))
    );

    pack();
}// </editor-fold>                        

private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
   //Stil has not code, because first jtextfields should fill with old data

}                                            

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new NewUserFrame_Edit(new UserInformation());
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JButton cancelButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JButton updateButton;
// End of variables declaration                   
}

我喜欢当我按下按钮时,我的 NewUserFrame_Edit 类文本字段应该由旧数据填充。但是这个异常发生:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myproject.Library.NewUserFrame_Edit.<init>(NewUserFrame_Edit.java:13)
at myproject.Library.NewUserFrame_Edit$2.run(NewUserFrame_Edit.java:148)
...

我的用户信息类:

public class UserInformation {

private String Fname;
private String Lname;
private String ID;
private String Gender;
private String Date;

public String getFname() {
    return Fname;
}

public void setFname(String fname) {
    this.Fname = fname;
}

public String getLname() {
    return Lname;
}

public void setLname(String lname) {
    this.Lname = lname;
}

public String getID() {
    return ID;
}

public void setID(String i_d) {
    this.ID = i_d;
}

public String getGender() {
    return Gender;
}

public void setGender(String gndr) {
    this.Gender = gndr;
}

public String getDate() {
    return Date;
}

public void setDate(String date) {
    this.Date = date;
}

@Override
public String toString() {
    return ID + "     " + Fname + "     "
            + Lname + "     " + Gender + "     " + Date + "\n";
}
}
4

2 回答 2

5

没有类的整个代码很难说NewUserFrame_Edit,但堆栈跟踪清楚地表明第 13 行的内容是null.

我猜第 13 行是指

userinf.getGender().equals("Male")

Returns ,userinf.getGender()所以null你不能调用equals它。getGender返回null是因为您传递了一个未调用任何 setter 的对象:

public void editUser(int row){
  UserInformation userInf=new UserInformation();
  userModel.getSelectedMember(row);
  NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf);
}
于 2013-02-25T18:28:13.990 回答
3

As @Robin stated (+1 for him), the problem is within this method:

public void editUser(int row)
{
    UserInformation userInf = new UserInformation();
    userModel.getSelectedMember(row);
    NewUserFrame_Edit edit = new NewUserFrame_Edit(userInf);
}

You should replace:

UserInformation userInf = new UserInformation();

with:

UserInformation userInf = userModel.getSelectedMember(row);

which gets the data from the table model of the selected row.

于 2013-02-28T21:37:09.333 回答