程序运行后单击button,数据库中内容返回不到JTextArea中,并出现以下错误,请高手指导.
java.lang.NullPointerException
at bus.test.actionPerformed(test.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
以下是程序代码:
public class Odbc
{
private Connection con = null;
public Connection getConnection()
{
String url = "jdbc:odbc:bond";//数据库URL
String userName = "sa";//登录数据库用户名
String password = "";//用户密码
try
{
//登录JDBC-ODBC驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, userName, password);
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
return con;
}}
public class test extends JFrame implements ActionListener
{
private JButton button;
private JScrollPane resultareaScrollPane;
private JSplitPane SplitPane;
private JTextArea resultarea;
private Odbc sql = new Odbc();
public test()
{
button = new JButton("button");
button.addActionListener(this);
JTextArea resultarea = new JTextArea();
resultarea.setLineWrap(true);
JScrollPane resultareaScrollPane = new JScrollPane(resultarea);
JSplitPane SplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,button,resultareaScrollPane);
this.getContentPane().add(SplitPane);
this.setSize(300,200);
this.setLocation(100,100);
this.setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button)
{
try
{
Connection con = sql.getConnection();
String command = "select * from student";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(command);
while(rs.next())
{
String reSname = rs.getString("Sname").trim();
resultarea.append(reSname);
}
stmt.close();
con.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
new test();
}
}
java.lang.NullPointerException
at bus.test.actionPerformed(test.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
以下是程序代码:
public class Odbc
{
private Connection con = null;
public Connection getConnection()
{
String url = "jdbc:odbc:bond";//数据库URL
String userName = "sa";//登录数据库用户名
String password = "";//用户密码
try
{
//登录JDBC-ODBC驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, userName, password);
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
return con;
}}
public class test extends JFrame implements ActionListener
{
private JButton button;
private JScrollPane resultareaScrollPane;
private JSplitPane SplitPane;
private JTextArea resultarea;
private Odbc sql = new Odbc();
public test()
{
button = new JButton("button");
button.addActionListener(this);
JTextArea resultarea = new JTextArea();
resultarea.setLineWrap(true);
JScrollPane resultareaScrollPane = new JScrollPane(resultarea);
JSplitPane SplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,button,resultareaScrollPane);
this.getContentPane().add(SplitPane);
this.setSize(300,200);
this.setLocation(100,100);
this.setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button)
{
try
{
Connection con = sql.getConnection();
String command = "select * from student";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(command);
while(rs.next())
{
String reSname = rs.getString("Sname").trim();
resultarea.append(reSname);
}
stmt.close();
con.close();
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args)
{
new test();
}
}
目测了一下,怀疑是这里出了问题,你没有取到Connection对象.而且初步怀疑是你的端口没有弄对, 1433什么的,应该被看到啊... ...
if (event.getSource() == button) { try {
System.out.println("Starting...");
Connection con = sql.getConnection(); System.out.println("Connection=" + con);加上2个输出,看一下运行结果,贴出来
错误还是一样,运行多了这2行结果
Starting...
Connection=sun.jdbc.odbc.JdbcOdbcConnection@1eec612
String reSname = rs.getString("Sname");
System.out.println("reSName="+reSname);
if(reSname!=null) { // 也许数据库里面是 null
reSname = reSname.trim();
}
resultarea.append(reSname);
}
at bus.test.actionPerformed(test.java:54) 请注意那个 :54 他代表错误出现在 test.java的第54行, 下次把那个错误行找到,直接调适就可以了。每次都可能不一样的,你根据那个数字找对应的行。
JTextArea resultarea = new JTextArea();改成
resultarea = new JTextArea();
空指针异常你跟踪一下,看看具体运行到哪句除了异常
就是这个可怜的家伙
private JTextArea resultarea;