gpt4 book ai didi

java - 我如何处理空的 jXDatePicker?

转载 作者:行者123 更新时间:2023-12-01 13:07:45 25 4
gpt4 key购买 nike

我有一个 swing 表单,其中包含 jXDatePicker 来捕获日期。如果未在 jXDatePicker 中选择日期,则尝试将日期插入数据库时​​会引发错误。以下是我在 Netbeans 中遇到的错误: 线程“AWT-EventQueue-0”中出现异常 java.lang.NullPointerException。下面是给我错误的代码:

    String dateOpened, dateOfLastUpdate, dateOf1stDelinquency, dateOfLastPayment,  dateClosed;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
dateOpened = format.format(jXDatePicker7.getDate());
dateOfLastUpdate = format.format(jXDatePicker2.getDate());
dateOf1stDelinquency = format.format(jXDatePicker4.getDate());
dateOfLastPayment = format.format(jXDatePicker5.getDate());
dateClosed = format.format(jXDatePicker6.getDate());

String query2 = "insert into ACCOUNT (ACCOUNT_NUMBER,DATE_CLOSED ,DELINQUENCY_DATE, UPDATE_DATE,AMOUNT_OWING,"
+ "BALANCE,PAYMENT_HISTORY,ACCOUNT_STATUS,MONTHLY_PAYMENT,TERMS_DURATION,PRINCIPAL,CREDIT_LIMIT,"
+ "DATE_OPENED,PORTIFOLIO_TYPE,ACCOUNT_TYPE,NATIONAL_ID,COSIGNER_NATIONAL_ID)"
+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement preparedStatement2 = con.prepareStatement(query2);
preparedStatement2.setString(1, acc_no);
preparedStatement2.setString(2, dateClosed);
preparedStatement2.setString(3, dateOf1stDelinquency);
preparedStatement2.setString(4, dateOfLastUpdate);
preparedStatement2.setDouble(5, amount_owing);
preparedStatement2.setDouble(6, current_balance);
preparedStatement2.setString(7, payment_history);
preparedStatement2.setString(8, account_status);
preparedStatement2.setDouble(9, monthly_payment);
preparedStatement2.setDouble(10, terms_duration);
preparedStatement2.setDouble(11, principal);
preparedStatement2.setDouble(12, credit_limit);
preparedStatement2.setString(13, dateOpened);
preparedStatement2.setString(14, portfolio_type);
preparedStatement2.setString(15, acc_type);
preparedStatement2.setString(16, national_id);
preparedStatement2.setString(17, cosigner_national_id);
preparedStatement2.executeUpdate();

某些日期不是必需的,并且在某些情况下适用,因此用户在这种情况下无法选择日期。

最佳答案

检查每个 JXDatePicker 的日期,例如 jXDatePicker2.getDate() 是否为 null。如果该日期恰好为空,则不要调用 format.format。如果日期为空并且按下 Enter 键,我会收到与 format.format 行中提到的完全相同的错误。相反,选择一些默认时间,例如 new Date(0); 含义:

format.format(new Date(0));

关于java - 我如何处理空的 jXDatePicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com