线程中的异常" AWT-EventQueue-0"使用java,awt和mysql的java.lang.NullPointerException

时间:2015-01-28 19:13:42

标签: java mysql awt

我正在开发一个java测验游戏,它涉及从数据库中获取一系列问题。游戏编译并运行,但是当我单击“测试”按钮时,我得到上述错误。 这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

public class PhysicsQuiz extends Frame implements WindowListener, ActionListener{

    public Connection getConnection() throws SQLException{
        String user  = "root";
        String pass = "usbw";
        Connection c = null;
        c = DriverManager.getConnection("jdbc:mysql://localhost:3307/pastpapers", user, pass);
        System.out.println("Connected to database");
        return c;
    }
    private Random rand = new Random();
    private Label menuheader;
    private Choice topiclist;
    private Button test;
    private Button scores;
    private Button quit;
    private Label questionheader;
    private Choice multiquestion;
    private TextField calculation;
    private TextField equation;
    public PhysicsQuiz(){
        setLayout(new FlowLayout());
        menuheader = new Label("Choose the topic you wish to revise.");
        add(menuheader);
        topiclist = new Choice();
        topiclist.add("Mechanics");
        topiclist.add("Materials");
        topiclist.add("Waves");
        topiclist.add("DC Electricity");
        topiclist.add("Nature of Light");
        add(topiclist);
        test = new Button("Test!");
        add(test);
        test.addActionListener(this);
        scores = new Button("View previous scores on this topic");
        add(scores);
        scores.addActionListener(this);
        quit = new Button("Quit");
        add(quit);
        quit.addActionListener(this);
        addWindowListener(this);
        setTitle("Physics Quiz");
        setSize(250, 300);
        setVisible(true);
    }
    public void Test() throws SQLException{
        String topic = topiclist.getSelectedItem();
        remove(topiclist);
        remove(test);
        remove(scores);
        remove(quit);

        question[] questions = new question[10];

        for(int j=0;j<questions.length;j++){
            try{
                Connection c = getConnection();
                Statement st = c.createStatement();
                int random = rand.nextInt(33);
                System.out.println(random);
                String sqlcontent = "SELECT QuestionContent FROM question WHERE QuestionTopic = '" + topic + "' AND QuestionID = " + random;
                ResultSet rs1 = st.executeQuery(sqlcontent);
                try{
                    while (rs1.next()){
                        questions[j].content = rs1.getString(1);
                        System.out.println(questions[j].content);
                    }
                } finally {
                    rs1.close();
                }
                String sqltype = "SELECT QuestionType FROM question WHERE QuestionTopic = '" + topic + "' AND QuestionID = " + random;
                ResultSet rs2 = st.executeQuery(sqltype);
                try{
                    while (rs2.next()){
                        questions[j].type = rs2.getString(1);   
                        System.out.println(questions[j].type);
                    }
                } finally {
                    rs2.close();
                }
                st.close();
                c.close();
            }catch(SQLException ex){
                System.out.println(ex);
            }
        }

        for(int i = 0;i<questions.length;i++){
            if(questions[i].type.equals("Multiple Choice")){
                questionheader = new Label("Question " + i + " : " + questions[i].content);
                multiquestion = new Choice();
            }
            if(questions[i].type.equals("Calculation")){
                questionheader = new Label("Question " + i + " : " + questions[i].content);
                calculation = new TextField();
                equation = new TextField();
            }
            else{
                System.out.println("Error. Cannot get results from query");
            }
        }

    }
    public static void main(String[] args){
        PhysicsQuiz app = new PhysicsQuiz();
    }
    public void actionPerformed(ActionEvent e){
        String action = e.getActionCommand();
        if(action.equals("Test!")){
            try{
                Test();
            }
            catch(SQLException ex){
                System.out.println(ex);
            }
        }
        if(action.equals("View previous scores on this topic")){

        }
        if(action.equals("Quit")){
            System.exit(0);
        }
    }
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
    public void windowOpened(WindowEvent e) { }
    public void windowClosed(WindowEvent e) { }
    public void windowIconified(WindowEvent e) { }
    public void windowDeiconified(WindowEvent e) { }
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) { }
}

这是我的完整错误消息:

E:\Documents\GitHub\COMP4-Project>java PhysicsQuiz
Connected to database
2
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at PhysicsQuiz.Test(PhysicsQuiz.java:70)
        at PhysicsQuiz.actionPerformed(PhysicsQuiz.java:116)
        at java.awt.Button.processActionEvent(Button.java:409)
        at java.awt.Button.processEvent(Button.java:377)
        at java.awt.Component.dispatchEventImpl(Component.java:4881)
        at java.awt.Component.dispatchEvent(Component.java:4703)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:702)
        at java.awt.EventQueue$3.run(EventQueue.java:696)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:75)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:86)
        at java.awt.EventQueue$4.run(EventQueue.java:724)
        at java.awt.EventQueue$4.run(EventQueue.java:722)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:75)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

我将非常感谢所有帮助,因为这是一个截止日期的项目。谢谢!

0 个答案:

没有答案