“线程异常”的原因AWT-EventQueue-0“java.lang.NullPointerException”

时间:2017-05-30 18:33:59

标签: java

我正在创建一个酒店预订系统,但evey时间我运行我的项目我收到“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException” 我一直无法找到原因。 这是我收到的错误报告

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at hotelreservation.Reservation.fill(Reservation.java:389)  at hotelreservation.Reservation.fill(Reservation.java:389)
at hotelreservation.Reservation.<init>(Reservation.java:31)
at hotelreservation.main.jMenuItem1ActionPerformed(main.java:502)
at hotelreservation.main.access$600(main.java:29)
at hotelreservation.main$6.actionPerformed(main.java:277)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是班级

package hotelreservation;

import java.awt.event.KeyEvent;
//import java.sql.Date;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/ **

*
 * @author TOSHIBA
 */
public class Reservation extends javax.swing.JInternalFrame {

    /**
     * Creates new form Reservation
     */
public Reservation() {
    initComponents();
  fill();
    //idgenerate();
    username();
    Date date = new Date();
    txt_date.setDate(date);
}

public void username() {
    dbconnection db = new dbconnection();
    try {
        db.connect();
        db.stm = db.con.createStatement();
        db.rs = db.stm.executeQuery("select User_name from Users where Status='Active'");
        while (db.rs.next()) {
            txt_user.setText(db.rs.getString(1));
        }
    } catch (SQLException e) {
        JOptionPane.showMessageDialog(this, e.toString());
    }
}

public void idgenerate() {

    dbconnection db = new dbconnection();
    int IDincrement = 0;
    try {
        db.connect();
        db.stm = db.con.createStatement();

        db.rs = db.stm.executeQuery("select MAX(Res_ID)from Reservation");

        while(db.rs.next()) {
            if (db.rs.first() == false) {
                txt_id.setText("Res000001");
            } else {
                db.rs.last();
                int autoID = db.rs.getInt(1) + 1;
                String no = String.valueOf(autoID);
                int nolong = no.length();
                for (int a = 0; a < 3 - nolong; a++) {
                    no = "0" + no;
                }
                txt_id.setText("Res" + no);

            }
          /* db.rs.next();
            if (db.rs != null) {
                IDincrement = db.rs.getInt(1) + 1;
                IDincrement++;
            } else {
                IDincrement = 1;
            }
            txt_id.setText("CS" + IDincrement);*/
        }

    } catch (SQLException e) {

        JOptionPane.showMessageDialog(this,"Error \n"+ e.getMessage(),"warning",JOptionPane.WARNING_MESSAGE);
    }

}

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

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jPanel3 = new javax.swing.JPanel();
    jButton4 = new javax.swing.JButton();
    jButton1 = new javax.swing.JButton();
    txt_user = new javax.swing.JTextField();
    txt_date = new com.toedter.calendar.JDateChooser();
    jLabel6 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    txt_rrate = new javax.swing.JTextField();
    txt_rid = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    txt_id = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    txtlname = new javax.swing.JTextField();
    txt_fname = new javax.swing.JTextField();
    txt_tel = new javax.swing.JTextField();
    filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0));
    jPanel2 = new javax.swing.JPanel();
    jLabel8 = new javax.swing.JLabel();

    setBackground(new java.awt.Color(204, 204, 204));
    setClosable(true);
    setTitle("Reservation Form");
    setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
    setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Icon-Edit-Reservations1.png"))); // NOI18N
    setName(""); // NOI18N
    setVisible(false);

    jPanel1.setBackground(new java.awt.Color(255, 204, 51));
    jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED, java.awt.Color.black, new java.awt.Color(204, 255, 0), java.awt.Color.red, java.awt.Color.black));

    jScrollPane1.setBackground(new java.awt.Color(255, 204, 51));
    jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Available Rooms", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18), new java.awt.Color(204, 0, 204))); // NOI18N
    jScrollPane1.setViewportBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, null, java.awt.Color.lightGray));
    jScrollPane1.setAutoscrolls(true);

    jTable1.setBackground(new java.awt.Color(153, 0, 153));
    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "Room_ID", "Room_Type", "Room_rate", "Status"
        }
    ));
    jTable1.setToolTipText("click on the row you want ");
    jTable1.setGridColor(new java.awt.Color(255, 255, 255));
    jTable1.setSelectionBackground(new java.awt.Color(102, 255, 0));
    jTable1.setSelectionForeground(new java.awt.Color(255, 0, 51));
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jTable1MouseClicked(evt);
        }
    });
    jScrollPane1.setViewportView(jTable1);

    jPanel3.setBackground(new java.awt.Color(255, 204, 51));
    jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED, new java.awt.Color(204, 0, 204), new java.awt.Color(204, 0, 204), new java.awt.Color(204, 0, 204), new java.awt.Color(204, 0, 204)));

    jButton4.setBackground(new java.awt.Color(204, 0, 204));
    jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/sav.png"))); // NOI18N
    jButton4.setText("Reserve");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    jButton1.setBackground(new java.awt.Color(204, 0, 204));
    jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Clear-icon.png"))); // NOI18N
    jButton1.setText("Clear");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    txt_user.setEditable(false);

    txt_date.setDateFormatString("yyyy MMM d");
    txt_date.setEnabled(false);

    jLabel6.setText("Date_In");

    jLabel9.setText("User_name");

    jLabel5.setText("Room_Rate:");

    txt_rrate.setEditable(false);
    txt_rrate.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            txt_rrateActionPerformed(evt);
        }
    });
    txt_rrate.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyPressed(java.awt.event.KeyEvent evt) {
            txt_rrateKeyPressed(evt);
        }
    });

    txt_rid.setEditable(false);

    jLabel4.setText("Room_ID:");

    jLabel1.setText("Res_ID:");

    jLabel2.setText("Firstname:");

    jLabel7.setText("Lastname");

    jLabel3.setText("Telephone:");

    txtlname.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            txtlnameKeyTyped(evt);
        }
    });

    txt_fname.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            txt_fnameKeyTyped(evt);
        }
    });

    txt_tel.setToolTipText(" valid number format +252615178484");
    txt_tel.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent evt) {
            txt_telKeyTyped(evt);
        }
    });

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addGap(18, 18, 18)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(175, 175, 175))
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel7)
                                .addComponent(jLabel3))
                            .addGap(24, 24, 24)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtlname, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                                .addComponent(txt_id)
                                .addComponent(txt_fname)
                                .addComponent(txt_tel)))
                        .addComponent(jLabel1))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(jLabel6)
                        .addComponent(jLabel9))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txt_rid)
                        .addComponent(txt_date, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addComponent(txt_rrate)
                        .addComponent(txt_user))))
            .addContainerGap(43, Short.MAX_VALUE))
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
            .addContainerGap(40, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1)
                .addComponent(txt_rid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel4))
            .addGap(18, 18, 18)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txt_fname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2)
                .addComponent(txt_rrate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel5))
            .addGap(18, 18, 18)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtlname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6))
                .addComponent(txt_date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(18, 18, 18)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel9)
                    .addComponent(txt_tel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(30, 30, 30)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(20, 20, 20)
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(24, Short.MAX_VALUE))
    );

    jPanel2.setBackground(new java.awt.Color(153, 0, 153));

    jLabel8.setFont(new java.awt.Font("Tw Cen MT", 1, 48)); // NOI18N
    jLabel8.setForeground(new java.awt.Color(255, 255, 255));
    jLabel8.setText("Guest Reservation");

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(283, 283, 283)
            .addComponent(jLabel8)
            .addContainerGap(305, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel8)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(161, 161, 161))
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

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

private void txt_rrateActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

public void fill() {

    dbconnection db = new dbconnection();
    try {
        db.connect();
        db.stm = db.con.createStatement();
        db.rs = db.stm.executeQuery("select Room_ID,Room_type,Room_Rate,Status from Rooms where Status='Available'ORDER By Room_ID");
        ResultSetMetaData rmetadata = db.rs.getMetaData();
        int columns = rmetadata.getColumnCount();
        DefaultTableModel dtm = new DefaultTableModel();
        Vector columns_name = new Vector();
        Vector data_rows = new Vector();
        for (int i = 1; i <= columns; i++) {
            columns_name.addElement(rmetadata.getColumnName(i));
        }
        dtm.setColumnIdentifiers(columns_name);
        // dtm.setColumnIdentifiers(Columns_name);
        while (db.rs.next()) {
            data_rows = new Vector();
            for (int j = 1; j <= columns; j++) {
                data_rows.addElement(db.rs.getString(j));
            }
            dtm.addRow(data_rows);
        }
        jTable1.setModel(dtm);

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}

2 个答案:

答案 0 :(得分:0)

您的space方法中的whereStatus='Available'之后,您的SQL查询中似乎错过了fill()

select Room_ID,Room_type,Room_Rate,Status from Rooms whereStatus='Available'ORDER By Room_ID

将其更改为:

select Room_ID,Room_type,Room_Rate,Status from Rooms whereStatus='Available' ORDER By Room_ID

由于SQL语句无效,db.rs为空,导致执行NullPointerException语句时出现db.rs.next()

答案 1 :(得分:0)

这一行

 at hotelreservation.Reservation.fill(Reservation.java:389)
堆栈跟踪的

告诉异常发生在第389行的方法fill()中。空指针异常基本上意味着你在空对象上调用某些内容。

我认为这一行

jTable1.setModel(dtm); 

这意味着jTable1为null。通过查看代码,您在方法initComponents()中先前设置的jTable1变量对于该特定方法是本地的,这就是为什么它在方法fill()上为null的原因

您可以通过创建类变量jTable1并使用该

来解决此问题