JCalander组合框中的空针指针异常 [英] Null Pointer Exception from JCalander Combobox

查看:129
本文介绍了JCalander组合框中的空针指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java应用程序从JCalander Combobox生成空指针异常。我试图抓住错误。但是没有工作。有人可以帮我解决这个问题。请。

 线程中的异常AWT-EventQueue-0java.lang.NullPointerException java.util中的
。 Calendar.setTime(Calendar.java:1106)
在java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
在java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
在java.text.DateFormat.format(DateFormat.java:336)
在org.freixas.jcalendar.JCalendarCombo.paramString(JCalendarCombo.java:780)
在java.awt.Component.toString (Component.java:8095)


tbmodel =(DefaultTableModel)tblItmQty.getModel();
System.out.println(calRecvDate.getDate());
try {
if(calRecvDate.getDate()== null){//错误
JOptionPane.showMessageDialog(null,Please Select Shippment Received Date);
calRecvDate.requestFocus();

} else if(txtShipSs.getText()。isEmpty()){

//////////////////////////////////////////// //////////////

  if(inputValidate()== true){ 

try {
String shipId = txtShipId.getText();
String invID = txtInvoice.getText();
String shipSs = txtShipSs.getText();
String address = txtNtfAddress.getText();
String sipper = txtAShipper.getText();
String vesselsal = txtVessal.getText();
日期rcvDate = calRecvDate.getDate(); // Jcalander
String consignee = txtConsigne.getText();


ArrayList< ShippmentItems> shipItems = new ArrayList< ShippmentItems>();
tbmodel =(DefaultTableModel)tblItmQty.getModel(); (int i = 0; i< tbmodel.getRowCount(); i ++){
String itmcode =(String)tbmodel.getValueAt(i,0);


String itmName =(String)tbmodel.getValueAt(i,1);
int qty =(int)tbmodel.getValueAt(i,2);
ShippmentItems shpItems = new ShippmentItems(shipId,itmcode,itmName,qty);
shipItems.add(shpItems);
}


解决方案

由于这会抛出NPE:

  calRecvDate.getDate()== null 

calRecvDate 变量为null,您将需要检查它是否为空,然后再使用它,或确保它是通过将代码追溯到您认为已初始化的位置,并解决问题(因为它未被初始化),它将变为空。



要检查是否为空,您可以执行以下操作:

  if (calRecvDate!= null){
//在这里使用calRecvDate变量
} else {
//初始化calRecvDate变量

//或者更好的是,显示JOptionPane错误消息给用户
//表示该日期尚未被选中,并通过调用return退出此方法:

return;
}

再次,不要使用try / catch块来处理NullPointerExceptions。 p>

My Java Application produces Null Pointer Exception from JCalander Combobox. I tried to catch the error. But that didnt work. Can someone assist me to fix this. Please.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)
at org.freixas.jcalendar.JCalendarCombo.paramString(JCalendarCombo.java:780)
at java.awt.Component.toString(Component.java:8095)


 tbmodel = (DefaultTableModel)tblItmQty.getModel();
        System.out.println(calRecvDate.getDate());
        try{
        if(calRecvDate.getDate()==null){ // Error
            JOptionPane.showMessageDialog(null, "Please Select Shippment Received Date");  
            calRecvDate.requestFocus();

        }else if(txtShipSs.getText().isEmpty()){

////////////////////////////////////////////////////////////////

  if (inputValidate() == true) {

              try {
                    String shipId = txtShipId.getText();
                    String invID = txtInvoice.getText();
                    String shipSs = txtShipSs.getText();
                    String address = txtNtfAddress.getText();
                    String sipper = txtAShipper.getText();
                    String vessal = txtVessal.getText();
                    Date rcvDate = calRecvDate.getDate(); // Jcalander
                    String consignee = txtConsigne.getText();


                    ArrayList<ShippmentItems> shipItems = new ArrayList<ShippmentItems>();
                    tbmodel = (DefaultTableModel) tblItmQty.getModel();

                    for (int i = 0; i < tbmodel.getRowCount(); i++) {
                          String itmcode = (String) tbmodel.getValueAt(i, 0);
                          String itmName = (String) tbmodel.getValueAt(i, 1);
                          int qty = (int) tbmodel.getValueAt(i, 2);
                          ShippmentItems shpItems = new ShippmentItems(shipId, itmcode, itmName, qty);
                          shipItems.add(shpItems);
                    }

解决方案

Since this throws the NPE:

calRecvDate.getDate()==null

The calRecvDate variable is null, and you will either need to check if it's null before using it, or make sure that it isn't null by tracing back in your code to where you think you've initialized it and fix the problem (since it isn't initialized).

To check if it's null, you could do:

if (calRecvDate != null) {
  // use the calRecvDate variable here
} else {
  // initialize the calRecvDate variable here

  // or perhaps better, display a JOptionPane error message to the user
  // that the date hasn't been selected, and exit this method by calling return:

  return;
}

Again, don't use try/catch blocks to handle NullPointerExceptions.

这篇关于JCalander组合框中的空针指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆