swing - Populate jTable in JAVA -
why have these error? mean? want display data on sql db.
run: exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ javaapplication25.searchpatient.<init>(searchpatient.java:26) @ javaapplication25.searchpatient$2.run(searchpatient.java:146) @ java.awt.event.invocationevent.dispatch(invocationevent.java:251) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:705) @ java.awt.eventqueue.access$000(eventqueue.java:101) @ java.awt.eventqueue$3.run(eventqueue.java:666) @ java.awt.eventqueue$3.run(eventqueue.java:664) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:675) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:211) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:128) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:117) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:113) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:90)
here codes used, don't know how fix because i'm not enough in programming java apps. , can me here? :
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 * dbo.patients"); resultsetmetadata rsmetadata = rs.getmetadata(); int columns = rsmetadata.getcolumncount(); defaulttablemodel dtm = new defaulttablemodel(); vector columns_vector = new vector(); vector rows_vector = new vector(); (int i=1; i< columns; i++){ columns_vector.addelement(rsmetadata.getcolumnname(i)); } dtm.setcolumnidentifiers(columns_vector); while (rs.next()){ rows_vector = new vector(); (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 handling code here: connection = main.connectdatabase(); }
you have these 2 lines, 1 after other
statement stmt = null; resultset rs = stmt.executequery("select * dbo.patients");
you've not initialised stmt
object, null
, nothing, natta
basically, it's trying turn on tv before you've plugged in, it's not work
take @ jdbc data access more details, essentially, need establish connection database , use connection create statement object, can query it.
Comments
Post a Comment