0

データベースからレコードを挿入または取得しようとすると、nullpointerexception が発生します。これは、Maintain Member Control クラスです。

    package control;
    import da.MemberDa;
    import domain.Member;

    public class MaintainMemberControl {
    private MemberDa memberDa;


    public MaintainMemberControl(){
    memberDa = new MemberDa();
    }

    public void InsertRecord(String studentID, String name, String membership, String      danceStyle, String gender, String faculty, String programme, String yearOfStudy, String dateJoined, String contactNumber,String email){
       memberDa.InsertRecord(studentID, name, membership, danceStyle, gender, faculty, programme, yearOfStudy, dateJoined, contactNumber,email);
     }
    public Member selectRecord(String studentID) {
    return memberDa.getRecord(studentID);
     }

     }

MemberDa.java これは私の memberDa Java クラスです

    package da;

    import domain.Member;
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;

    public class MemberDa {

    private String HOST = "jdbc:derby://localhost:1527/GoogleDinosaur";
    private String USERNAME = "GoogleDinosaur";
    private String PASSWORD = "123456";
    private String TABLENAME = "members";

    private Connection conn;
    private PreparedStatement stmt;




    public MemberDa(){
        createConnection();
    }

    private void createConnection(){
        try{
        conn = DriverManager.getConnection(HOST, USERNAME, PASSWORD);

    } catch(SQLException ex){
        JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    }

  public Member getRecord(String studentID) {
    String queryStr = "SELECT * FROM " + TABLENAME + " WHERE studentID = ?";
    Member member = null;
    try {
        stmt = conn.prepareStatement(queryStr);
        stmt.setString(1, studentID);
        ResultSet rs = stmt.executeQuery();

        if (rs.next()) {
            member = new Member(studentID, rs.getString("Name"), rs.getString("membership"),rs.getString("dancestyle"),rs.getString("gender"),rs.getString("faculty"),rs.getString("programme"),rs.getString("yearofstudy"),rs.getString("datajoined"),rs.getString("contactnumber"),rs.getString("email"));
        }
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    return member;
}

public void InsertRecord(String studentID, String name, String membership, String danceStyle, String gender, String faculty, String programme, String yearOfStudy, String dateJoined, String contactNumber,String email) {
    String queryStr = "INSERT INTO " + TABLENAME + " VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    Member member = null;
    try {

    stmt = conn.prepareStatement(queryStr);
    stmt.setString(1,studentID);
    stmt.setString(2,name);
    stmt.setString(3,membership);
    stmt.setString(4,danceStyle);
    stmt.setString(5,gender);
    stmt.setString(6,faculty);
    stmt.setString(7,programme);
    stmt.setString(8,yearOfStudy);
    stmt.setString(9,dateJoined);
    stmt.setString(10,contactNumber);
    stmt.setString(11,email);

    stmt.executeUpdate();
    conn.close();
    } catch (SQLException | NumberFormatException ex) {

    }




}

 public static void main(String[] args) {

    MemberDa memberDa = new MemberDa();
}
}

MemberTab_redo.java

jbSubmit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String rbGender= null;

            int prompt = JOptionPane.showConfirmDialog(null, "Insert new member record? ", "Warning", JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);

            if (e.getSource() == jbSubmit) {
                if (jrbMale.isSelected()) {
                    rbGender = "Male";
                } else if (jrbFemale.isSelected()) {
                    rbGender = "Female";
                }
            }
            if(prompt == JOptionPane.YES_OPTION){
            memberContr.InsertRecord(jtfStudentID.getText(),jtfName.getText(), (String)jcbMembership.getSelectedItem(), (String)jcbDanceStyle.getSelectedItem(),rbGender,(String) jcbFaculty.getSelectedItem(), jtfProgramme.getText(),(String)jcbYearOfStudy.getSelectedItem(),jtfDateJoined.getText(),jtfContactNumber.getText(),jtfEmail.getText());

            } 
            }
    });

送信ボタンをクリックすると、毎回ヌルポインタ例外エラーが発生します。

OPコメントから:

以下、エラー、

line 313 is InsertRecord statement Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.MemberTab_redo$2.actionPerformed(MemberTab_redo.java:313) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.jav‌​a:252) 

この行:

memberContr.InsertRecord(jtfStudentID.getText(),jtfName.getText(), (String)jcbMembership.getSelectedItem(), (String)jcbDanceStyle.getSelectedItem(),rbGender,(String) jcbFaculty.getSelectedItem(), jtfProgramme.getText(),(String)jcbYearOfStudy.getSelectedItem(),jtfDateJoined.ge‌​tText(),jtfContactNumber.getText(),jtfEmail.getText());
4

1 に答える 1

0

InsertRecordNPEは、メソッドに渡される値の 1 つ (または複数) が であることを意味する行を指しますNULL。コードを確認するか、デバッグを実行してどちらを確認する必要があります。jtfStudentIDjcbMembership、など

于 2013-11-10T02:07:19.037 に答える