我正在通過以下教程。我無法從數據庫調用下一個項目。java - homeandlearn數據庫教程 - rs.Next空指針異常
http://www.homeandlearn.co.uk/java/database_scrolling_buttons.html
砍下我的課儘量使問題更容易閱讀..
package Employees;
import java.sql.Connection;
imports ... etc
進口在頂部,然後調用構造函數
public class Workers extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
/**
* Creates new form Workers
*/
public Workers() {
initComponents();
DoConnect();
}
cont..
}
這裏是DoConnect方法 - 這可以正常工作,因爲它設置變量,並且您可以在窗體上看到它們。
public void DoConnect(){
try {
String host = "jdbc:derby://localhost:1527/Employees";
String uName = "robert";
String uPass = "robert";
Connection con = DriverManager.getConnection(host, uName, uPass);
String SQL = "SELECT * FROM APP.WORKERS";
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = stmt.executeQuery(SQL);
rs.next();
int id_col = rs.getInt("ID");
String first_name = rs.getString("First_Name");
String last_name = rs.getString("Last_Name");
String job = rs.getString("Job_Title");
// DISPLAY THE FIRST RECORD IN THE TEXT FIELDS
textID.setText(Integer.toString(id_col));
textFirstName.setText(first_name);
// etc... this works ok
}
catch(SQLException err){
JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}
}
但是,這是我得到我的錯誤的功能。當我點擊下一個按鈕時,它應該從數據庫中獲取下一個項目。不過,我不認爲我會用正確的方式 - 我跟着教程?我不認爲它喜歡rs.Next()方法?
這裏是下一個按鈕
private void buttonNextActionPerformed(java.awt.event.ActionEvent evt) {
try
{
if (rs.next()) { // this is the line where I get the error
}
else
{
rs.previous();
JOptionPane.showMessageDialog(Workers.this, "End of File");
}
}
catch (SQLException err) {
JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}
}
代碼當我按下旁邊我得到以下錯誤。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Employees.Workers.buttonNextActionPerformed(Workers.java:164)
at Employees.Workers.access$200(Workers.java:18)
..
不知道我需要做什麼來解決它。非常感謝。
下面是完整的代碼的要求...... Workers.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Employees;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
*
* @author davidsonr
*/
public class Workers extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
/**
* Creates new form Workers
*/
public Workers() {
initComponents();
DoConnect();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
textID = new javax.swing.JTextField();
textFirstName = new javax.swing.JTextField();
textLastName = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
textJobTitle = new javax.swing.JTextField();
buttonFirst = new javax.swing.JButton();
buttonPrevious = new javax.swing.JButton();
buttonNext = new javax.swing.JButton();
buttonLast = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
textID.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textIDActionPerformed(evt);
}
});
jLabel1.setText("Job Title");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(textID))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(textFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(textLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
.addComponent(textJobTitle))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(textJobTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addContainerGap(38, Short.MAX_VALUE))
);
buttonFirst.setText("First");
buttonFirst.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonFirstActionPerformed(evt);
}
});
buttonPrevious.setText("Previous");
buttonNext.setText("Next");
buttonNext.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonNextActionPerformed(evt);
}
});
buttonLast.setText("Last");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(buttonFirst)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonPrevious)
.addGap(104, 104, 104)
.addComponent(buttonNext)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(buttonLast)))
.addContainerGap(30, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(buttonFirst)
.addComponent(buttonPrevious)
.addComponent(buttonNext)
.addComponent(buttonLast))
.addContainerGap(160, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void textIDActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void buttonFirstActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void buttonNextActionPerformed(java.awt.event.ActionEvent evt) {
try
{
if (rs.next()) { // this is the line where I get the error
}
else
{
rs.previous();
JOptionPane.showMessageDialog(Workers.this, "End of File");
}
}
catch (SQLException err) {
JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}
}
public void DoConnect(){
try {
String host = "jdbc:derby://localhost:1527/Employees";
String uName = "robert";
String uPass = "robert";
Connection con = DriverManager.getConnection(host, uName, uPass);
String SQL = "SELECT * FROM APP.WORKERS";
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = stmt.executeQuery(SQL);
rs.next();
int id_col = rs.getInt("ID");
String first_name = rs.getString("First_Name");
String last_name = rs.getString("Last_Name");
String job = rs.getString("Job_Title");
// DISPLAY THE FIRST RECORD IN THE TEXT FIELDS
textID.setText(Integer.toString(id_col));
textFirstName.setText(first_name);
textLastName.setText(last_name);
textJobTitle.setText(job);
}
catch(SQLException err){
JOptionPane.showMessageDialog(Workers.this, err.getMessage());
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Workers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Workers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Workers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Workers.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Workers().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton buttonFirst;
private javax.swing.JButton buttonLast;
private javax.swing.JButton buttonNext;
private javax.swing.JButton buttonPrevious;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField textFirstName;
private javax.swing.JTextField textID;
private javax.swing.JTextField textJobTitle;
private javax.swing.JTextField textLastName;
// End of variables declaration
}
當你調用'buttonNextActionPerformed'方法時,是否已經初始化了'ResultSet'? –
不知道?我推測它在頂部宣佈? –
它被聲明瞭,但它被初始化了嗎?可能不會。考慮你的程序流程。 –