简体   繁体   English

“AWT-EventQueue-0”java.lang.NullPointerException 在 mysql.connector.java@5.1.38-bin/com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.8821324:94518)

[英]"AWT-EventQueue-0" java.lang.NullPointerException at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2015)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.swing.JOptionPane;
    


public class DBManager {
    private String connectionString="jdbc:mysql://localhost/xx?autoReconnect=true&useSSL=false";
    private Connection conn=null;
    private Statement st=null;
    private ResultSet rs=null;
    private String out;
    private ArrayList<String> arr;
    public DBManager() {
        
    }
    private void createConnection() {
        try {
            
            conn=DriverManager.getConnection(connectionString,"root","");
            st=conn.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public ArrayList<String> recordFill(int num) {
        createConnection();
        String query="Select * from project_details where po="+num+"";      
        try {
            rs = st.executeQuery(query);
            arr=new ArrayList<String>();
            java.sql.Date date = rs.getDate(2);
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String dateStr = dateFormat.format(date);
            arr.add(dateStr);
            arr.add(rs.getString(3));
            arr.add(rs.getString(4));
            arr.add(rs.getString(5));
            arr.add(rs.getString(6));
            java.sql.Date date2 = rs.getDate(7);
            DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
            String dateStr2 = dateFormat.format(date2);
            arr.add(dateStr2);
            arr.add(rs.getString(8));
            rs.close();
            st.close();
            conn.close();
        }catch(SQLException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return arr;
    }
    public boolean checkpo(int num) {
        createConnection();
        String query="Select PO from project_details";
        try {
            rs = st.executeQuery(query);
            while(rs.next()) {
                if(Integer.parseInt(rs.getString(1))==num) {
                    JOptionPane.showMessageDialog(null, "Already on database");
                    return true;
                }
            }
            
            rs.close();
            st.close();
            conn.close();
            
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

public static void main(String args[]) {
    DBManager test=new DBManager();
}
}

I want to solve this error please help: ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////我想解决这个错误请帮忙:////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// ///////////////////////////

Already in database

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2015)
    at mysql.connector.java@5.1.38-bin/com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:1978)
    at DBManager.recordFill(DBManager.java:60)
    at ProjectDetails$3.actionPerformed(ProjectDetails.java:221)
    at java.desktop/javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
    at java.desktop/javax.swing.JTextField.postActionEvent(JTextField.java:723)
    at java.desktop/javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:839)
    at java.desktop/javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1810)
    at java.desktop/javax.swing.JComponent.processKeyBinding(JComponent.java:2900)
    at java.desktop/javax.swing.JComponent.processKeyBindings(JComponent.java:2948)
    at java.desktop/javax.swing.JComponent.processKeyEvent(JComponent.java:2862)
    at java.desktop/java.awt.Component.processEvent(Component.java:6413)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950)
    at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:870)
    at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1139)
    at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1009)
    at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:835)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4893)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

数据库

In recordFill you get a Resultset rs .recordFill ,您会得到一个结果集Resultset rs Then, you call rs.getDate(2);然后,您调用rs.getDate(2); without having checked if there is a record in your result set.没有检查结果集中是否有记录。 A call to rs.next() should fix the issue.调用rs.next()应该可以解决这个问题。

You have to handle the case that your query did not return any rows, too.您还必须处理您的查询也没有返回任何行的情况。 So there should be something like所以应该有类似的东西

if (rs.next()) {
    // row found, handle row
} else {
    // row not found, maybe throw exception
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL JDBC java项目中的“AWT-EventQueue-0”java.lang.NullPointerException错误 - “AWT-EventQueue-0” java.lang.NullPointerException error on MySQL JDBC java project BlueJ Java MySQL CRUD应用程序| 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - BlueJ Java MySQL CRUD application | Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException java中的“AWT-EventQueue-0”java.lang.NullPointerException - “AWT-EventQueue-0” java.lang.NullPointerException in java 线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java 线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException? Java的 - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException? Java MySQL 线程“AWT-EventQueue-0”中的错误异常 java.lang.NullPointerException - MySQL error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Java:线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java: Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Java-线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException - Java - Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException Getter和Setter Java AWT-EventQueue-0 java.lang.NullPointerException - Getter and Setter Java AWT-EventQueue-0 java.lang.NullPointerException 线程““ AWT-EventQueue-0””中的Java异常java.lang.NullPointerException - Java Exception in thread '“AWT-EventQueue-0”' java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM