以我平凡的天分和不平凡的毅力,可完成所有事!
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class readquestion extends JFrame implements ActionListener
{
databasemanager mb=new databasemanager();
JTextArea test=new JTextArea(100,100);
JButton read;
//JCheckBox box[];
Container c;
ResultSet rs;
public readquestion()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,500);
read=new JButton("读取题目");
read.addActionListener(this);
c=getContentPane();
c.setLayout(new BorderLayout());
JPanel pane=new JPanel();
//pane.setLayout(new GridLayout());
pane.add(test);
JPanel pane2=new JPanel();
//pane.setLayout(new GridLayout());
pane2.add(read);
c.add(pane,BorderLayout.CENTER);
c.add(pane2,BorderLayout.SOUTH);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="读取题目");//我最近改进的程序,发现了问题主要出现在这个“读取题目”这个按钮上,但找不出解决方法。
{
String str,s;
int x;
x=(int)Math.round(Math.random()*10);
str="select question from question1 where question_id='"+x+"'";
rs=mb.getResult(str);
StringBuffer buf=new StringBuffer();
try{
while(rs.next())
{
s=rs.getString(3);
//s=rs.getString(2);
buf.append(s);
test.setText(buf.toString());
mb.closeConnection();
this.dispose();
}
}catch(SQLException sql){
System.out.println(sql.toString());
}
}
}
public static void main(String args[])
{
new readquestion();
}
}
java.sql.SQLException: Invalid handle
java.lang.NullPointerException
at readquestion.actionPerformed(readquestion.java:48)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:18
34)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2152)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5463)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3052)
at java.awt.Component.processEvent(Component.java:5228)
at java.awt.Container.processEvent(Container.java:1961)
at java.awt.Component.dispatchEventImpl(Component.java:3931)
at java.awt.Container.dispatchEventImpl(Container.java:2019)
at java.awt.Component.dispatchEvent(Component.java:3779)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4203
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3883)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3813)
at java.awt.Container.dispatchEventImpl(Container.java:2005)
at java.awt.Window.dispatchEventImpl(Window.java:1757)
at java.awt.Component.dispatchEvent(Component.java:3779)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:234)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
这些是运行捕捉的异常,望各位高手能够提出你们的解决建议,先谢了!