Monday 15 September 2014

java - Unexpected exception -



java - Unexpected exception -

i'm facing problem want parse date(item) selected jcombobox become date type.

i know have converted date because can add together or subtract on year , day, below exception thrown when trigger cleartext() invocation (action clear selection jcombobox) on other text fields.

java.text.parseexception: unparseable date: "" @ java.text.dateformat.parse(dateformat.java:366) @ ui.addsupplier$1.itemstatechanged(addsupplier.java:86)

below code :

import control.suppliercontrol; import domain.supplierclass; import domain.dateitem; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.text.dateformat; import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.logging.level; import java.util.logging.logger; import javax.swing.*; import javax.swing.border.titledborder; public class addsupplier extends jframe{ private suppliercontrol supcontrol; private jpanel jpinput = new jpanel(new gridlayout(7,2)); private jlabel lbsupid = new jlabel("supplier id(spxxxx) : "); private jtextfield tfsupid = new jtextfield(); private jlabel lbsupname = new jlabel("supplier name : "); private jtextfield tfsupname = new jtextfield(); private jlabel lbmanufac = new jlabel("manufacturer : "); private jtextfield tfmanufac = new jtextfield(); private jlabel lbsuphp = new jlabel("supplier contact num. : "); private jtextfield tfsuphp = new jtextfield(); private jlabel lbsupaddr = new jlabel("supplier address : "); private jtextfield tfsupaddr = new jtextfield(); private jlabel lbactivedate = new jlabel("active date : "); private jlabel lbexpirydate = new jlabel("expiry date : "); private jtextfield tfexpirydate = new jtextfield(); private jcombobox cbacdate = new jcombobox(); private jpanel jpbutton = new jpanel(); private jbutton jbtadd = new jbutton("add"); private jbutton jbtclear = new jbutton("clear"); private jbutton jbtback = new jbutton("back"); private calendar calendar = calendar.getinstance(); private dateformat df = new simpledateformat("yyyy-mm-dd"); private date actdate=null; private date exp1; private date clrdate = null; public addsupplier(){ supcontrol = new suppliercontrol(); createdateitem(); jpinput.add(lbsupid); jpinput.add(tfsupid); jpinput.add(lbsupname); jpinput.add(tfsupname); jpinput.add(lbmanufac); jpinput.add(tfmanufac); jpinput.add(lbsuphp); jpinput.add(tfsuphp); jpinput.add(lbsupaddr); jpinput.add(tfsupaddr); jpinput.add(lbactivedate); jpinput.add(cbacdate); jpinput.add(lbexpirydate); tfexpirydate.seteditable(false); jpinput.add(tfexpirydate); titledborder suptb = new titledborder("add new supplier"); suptb.settitlecolor(color.red); suptb.settitlefont(new font("segoe script",font.bold+font.italic,30)); suptb.settitlejustification(2); jpinput.setborder(suptb); add(jpinput, borderlayout.center); jpbutton.add(jbtadd); jpbutton.add(jbtclear); jpbutton.add(jbtback); add(jpbutton, borderlayout.south); cbacdate.additemlistener(new itemlistener() { public void itemstatechanged(itemevent e) { seek { exp1 = (date)df.parse(cbacdate.getselecteditem().tostring()); exp1.setyear(exp1.getyear()+1); exp1.setdate(exp1.getdate()-1); tfexpirydate.settext(df.format(exp1)); } grab (parseexception ex) { logger.getlogger(addsupplier.class.getname()).log(level.severe, null, ex); } } }); jbtadd.addactionlistener(new addlistener()); jbtclear.addactionlistener(new clearlistener()); jbtback.addactionlistener(new backlistener()); settitle("abc halth supplement system"); setsize(450, 300); setlocationrelativeto(null); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); } public static void main (string args[]){ new addsupplier(); } public void createdateitem(){ cbacdate.additem(""); (int = 0; < 30; ++i) { cbacdate.additem(new dateitem( calendar.gettime())); calendar.add(calendar.date, 1); } } public void cleartext(){ tfsupid.settext(""); tfsupname.settext(""); tfmanufac.settext(""); tfsuphp.settext(""); tfsupaddr.settext(""); tfexpirydate.settext(""); } private class addlistener implements actionlistener{ public void actionperformed(actionevent e){ string supplierid = tfsupid.gettext(); string name = tfsupname.gettext(); string manufacturer = tfmanufac.gettext(); string contactnum = tfsuphp.gettext(); string addr = tfsupaddr.gettext(); seek { actdate = (date) df.parseobject(cbacdate.getselecteditem().tostring()); } grab (parseexception ex) { logger.getlogger(addsupplier.class.getname()).log(level.severe, null, ex); } date expdate = exp1; supplierclass supplierclass = supcontrol.selectrecord(supplierid); if(supplierclass != null){ joptionpane.showmessagedialog(null, "data has alredy exsists!!!", "existing record", joptionpane.error_message); }else{ supplierclass sc = new supplierclass(supplierid,name,manufacturer,contactnum,addr,actdate,expdate); supcontrol.addrec(sc); joptionpane.showmessagedialog(null,"a new supplier has added database ..."); cleartext(); } } } private class clearlistener implements actionlistener{ public void actionperformed(actionevent e){ cleartext(); cbacdate.setselectedindex(0); } } private class backlistener implements actionlistener{ public void actionperformed(actionevent e){ dispose(); } } }

if clear text, "date" parsed empty string.

i guess try-catch-block should this:

try { string datestring = cbacdate.getselecteditem().tostring().trim(); if(!"".equals(datestring) { exp1 = (date)df.parse(datestring); exp1.setyear(exp1.getyear()+1); exp1.setdate(exp1.getdate()-1); tfexpirydate.settext(df.format(exp1)); } else { <put default handling here> } } grab (parseexception ex) { logger.getlogger(addsupplier.class.getname()).log(level.severe, null, ex); }

java exception-handling

No comments:

Post a Comment