爲什麼我有這些錯誤?這是什麼意思?我只想顯示我的SQL DB上的所有數據。在JAVA中填充jTable
run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication25.SearchPatient.<init>(SearchPatient.java:26)
at javaapplication25.SearchPatient$2.run(SearchPatient.java:146)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
下面是我使用的代碼,不知道如何解決它,因爲我沒有足夠的編程Java應用程序。 , 有人能幫我一下嗎? :
package javaapplication25;
/**
*
* @author cleanfuel
*/
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class SearchPatient extends javax.swing.JFrame {
/**
* Creates new form SearchPatient
*/
public SearchPatient() throws SQLException {
initComponents();
Statement stmt = null;
ResultSet rs = stmt.executeQuery("SELECT * FROM dbo.Patients");
ResultSetMetaData rsmetadata = rs.getMetaData();
int columns = rsmetadata.getColumnCount();
DefaultTableModel dtm = new DefaultTableModel();
Vector columns_vector = new Vector();
Vector rows_vector = new Vector();
for (int i=1; i< columns; i++){
columns_vector.addElement(rsmetadata.getColumnName(i));
}
dtm.setColumnIdentifiers(columns_vector);
while (rs.next()){
rows_vector = new Vector();
for (int j=1; j<columns; j++){
rows_vector.addElement(rs.getString(j));
}
dtm.addRow(rows_vector);
}
tblPatients.setModel(dtm);
}
@SuppressWarnings("unchecked")
Connection connection;
private void formWindowOpened(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
connection = Main.ConnectDatabase();
}
'不知道如何解決它,因爲我在編寫Java應用程序時不夠好。所以,每次遇到小問題時,您都會來這裏請求我們爲您解決它?對不起,這不是它的工作方式。你需要學習如何自己解決問題。錯誤在第26行,一個變量爲空。你應該能夠找出哪一行是空的。你所要做的就是爲該行上使用的每個變量添加一個System.out.println(...)。其中一個將是空的。 – camickr 2013-04-06 02:13:02
對不起,先生camickr,如果我總是問這裏的問題... – 2013-04-06 02:15:53
@CrystalMaiden任何優秀的開發人員的實力是有能力研究自己的問題。分配問題沒有任何問題,但這不應該是你開始的第一個地方,這就是全部 – MadProgrammer 2013-04-06 02:18:13