【问题标题】:Having Trouble filling a LocalDateTime array with data from MySQL database使用 MySQL 数据库中的数据填充 LocalDateTime 数组时遇到问题
【发布时间】:2020-08-22 01:12:02
【问题描述】:

我正在使用 MySQL 数据库来保存我的 Java 提醒应用程序的信息。我正在尝试提取信息并将其存储在一个数组中,并将数组的每个元素与更新的当前时间戳进行比较。问题是我的代码给出了一个空指针异常,我不知道为什么。它在 LocalDateTime 不是数组时起作用,但是当我将它变成数组时它会抛出错误。它还要求我将其初始化为空值。

关于如何解决此问题的想法?任何帮助表示赞赏。

这是有问题的方法。

public static LocalDateTime[] getReminderTime()
{
    String SQL = "SELECT r_dateTime FROM reminder_database.reminder;";
    LocalDateTime reminderTime[] = null;
    try 
    {
        Connection conn = main.getConnection();

        java.sql.Statement stmt;
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);

        if(rs.isBeforeFirst())
        {
            for(int i = 0; rs.next(); i++)
            {
                reminderTime[i] = rs.getTimestamp(1).toLocalDateTime();
            }
        }

        rs.close();
        stmt.close();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println("Exception thrown with getReminderTime --> " + e + e.getStackTrace());
    }
    return reminderTime;
}

抛出异常

Exception thrown with getReminderTime --> java.lang.NullPointerException[Ljava.lang.StackTraceElement;@6dfc1e5f
Exception thrown with getReminderTime --> java.lang.NullPointerException[Ljava.lang.StackTraceElement;@3b2da18f

【问题讨论】:

  • 请添加完整的错误代码/回溯。
  • 添加了堆栈跟踪。

标签: java mysql arrays localdatetime


【解决方案1】:

找到了解决办法。

我需要初始化数组的大小以填充它。所以我创建了另一种遍历所有元素并获取大小的方法。

这是代码。

public static LocalDateTime[] getReminderTime()
{
    String SQL = "SELECT r_dateTime FROM reminder_database.reminder;";
    LocalDateTime reminderTime[] = null;
    reminderTime = new LocalDateTime[getSizeOfRs()];
    try 
    {
        Connection conn = main.getConnection();

        java.sql.Statement stmt;
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);


        if(rs.isBeforeFirst())
        {

            for(int i = 0; rs.next(); i++)
            {
                reminderTime[i] = rs.getTimestamp(1).toLocalDateTime();

            }
        }

        rs.close();
        stmt.close();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println("Exception thrown with getReminderTime --> " + e +  e.getStackTrace());
    }
    return reminderTime;
}

以及获取 rs 大小

public static int getSizeOfRs()
{

    String SQL = "SELECT * FROM reminder_database.reminder;";
    int size = 0;
    try {
        Connection conn = main.getConnection();

        java.sql.Statement stmt;
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);


        for(int i = 0; rs.next(); i++)
            size++;
    }
    catch(Exception e)
    {
        System.out.println("Exception thrown with getSizeofRS --> " + e +  e.getStackTrace());
    }

    return size;
}

【讨论】:

    猜你喜欢
    • 2011-10-30
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多