MySQL PreparedStatement NullPointerException

时间:2015-06-27 14:11:10

标签: java mysql nullpointerexception

我在尝试运行Java应用程序时不断收到NullPointerException。似乎问题出在PreparedStatement代码上。

这是我的代码

    Connection mc = null;
    /**
     * Create the application.
     */
    public GUI() {
        mc = DB.getConnection();
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 795, 486);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("Forecast Data");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                try {
                    String query = "Select * from db";
                    PreparedStatement pst = mc.prepareStatement (query);
                    ResultSet rs = pst.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

这是堆栈跟踪

java.lang.NullPointerException
    at GUI$2.actionPerformed(GUI.java:63)
    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$400(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)

这是我的DB类

public class DB {

    public static void main (String[]args) {
            getConnection();
        }

        public static Connection getConnection(){
            String uRl = "jdbc:mysql://localhost:3306/weather";
            String user = "root";
            String password = "0888150";
            try {
                String driver ="com.mysql.jdbc.Driver";
                Class.forName(driver);

                Connection mycon = DriverManager.getConnection(uRl,user,password);
                Statement st = mycon.createStatement();
                ResultSet rs = st.executeQuery("SELECT * FROM db");
                while(rs.next()){
                    System.out.println(rs.getString("City")+", "+rs.getString("date")+ ", "+rs.getString("Low")+ ", "+
                rs.getString("High")+", " +rs.getString("status"));
                }

            }catch(Exception ex){
                ex.printStackTrace();
            }
            return null;

        }
        }

1 个答案:

答案 0 :(得分:1)

看起来DB.getConnection();正在返回null

您可以在课程getConnection中分享DB()方法的代码,并使用mc打印System.out.println(mc)的值吗?

确定。您总是返回null,将DB.java更改为以下课程。

public class DB {

public static void main (String[]args) {
        getConnection();
    }

    public static Connection getConnection(){
        String uRl = "jdbc:mysql://localhost:3306/weather";
        String user = "root";
        String password = "0888150";
        Connection mycon = null;
        try {
            String driver ="com.mysql.jdbc.Driver";
            Class.forName(driver);

            mycon = DriverManager.getConnection(uRl,user,password);
            Statement st = mycon.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM db");
            while(rs.next()){
                System.out.println(rs.getString("City")+", "+rs.getString("date")+ ", "+rs.getString("Low")+ ", "+
            rs.getString("High")+", " +rs.getString("status"));
            }

        }catch(Exception ex){
            ex.printStackTrace();
        }
        return mycon;

    }
    }