java.lang.NullPointerException jdbc大错误

时间:2015-07-02 10:24:04

标签: java jdbc

我是java编程的新手,所以我有一个我无法解决的大错误:

这是异常的堆栈跟踪:

java.lang.NullPointerException
    at com.smy.gome.MySQL.checkUser(MySQL.java:56)
    at com.smy.gome.LogReg$1.actionPerformed(LogReg.java:43)
    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.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(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)

我的代码在MySQL.java:56

这是Java代码:

ResultSet user = statement.executeQuery("SELECT * FROM tadzio WHERE nick='benas12' AND pass='benas'");

MySQL.java:

package com.smy.gome;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQL {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/root";

    static final String USER = "root";
    static final String PASS = "benux1";

    public static Connection conn = null;
    public static Statement statement = null;
    public static ResultSet results = null;

    public synchronized static void Connection(){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost/root", "root", "benux1");
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static void Destroy(){
        try {
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static ResultSet query(String s) throws SQLException {
        try {
            if (s.toLowerCase().startsWith("select")) {
                ResultSet rs = statement.executeQuery(s);
                return rs;
            } else {
                statement.executeUpdate(s);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int checkUser() {
        try {
            conn.createStatement();
            ResultSet user = statement.executeQuery("SELECT * FROM tadzio WHERE nick='benas12' AND pass='benas'");
            user.next();
            int count = user.getRow();
            return count;
        } catch(Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static String getVersion(){

        return "tadis";
    }

}

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您没有分配statement。将代码更改为以下内容:

public static int checkUser() {
    try {
        statement = conn.createStatement();
        ResultSet user = statement.executeQuery("SELECT * FROM tadzio WHERE nick='benas12' AND pass='benas'");
        user.next();
        int count = user.getRow();
        return count;
    } catch(Exception e) {
        e.printStackTrace();
        return -1;
    }
}

答案 1 :(得分:0)

statement null

生成

要使整个代码按预期工作,请使用以下代码,因为statement正在各个地方使用

package com.smy.gome;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class MySQL {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/root";

    static final String USER = "root";
    static final String PASS = "benux1";

    public static Connection conn = null;
    public static Statement statement = null;
    public static ResultSet results = null;

    public synchronized static void Connection(){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection("jdbc:mysql://localhost/root", "root", "benux1");
            statement = conn.createStatement();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static void Destroy(){
        try {
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized static ResultSet query(String s) throws SQLException {
        try {
            if (s.toLowerCase().startsWith("select")) {
                ResultSet rs = statement.executeQuery(s);
                return rs;
            } else {
                statement.executeUpdate(s);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int checkUser() {
        try {
            ResultSet user = statement.executeQuery("SELECT * FROM tadzio WHERE nick='benas12' AND pass='benas'");
            user.next();
            int count = user.getRow();
            return count;
        } catch(Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static String getVersion(){

        return "tadis";
    }

}