DAO Setter shows NullPointerException error when setting the BigDecimal value - Java Swing -
i getting following error when set decimal values. please ignore naming convention, sorry being copied legacy application.
error
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ com.shams.mainwindow.btnsaveactionperformed(mainwindow.java:161) @ com.shams.mainwindow.access$200(mainwindow.java:25) @ com.shams.mainwindow$3.actionperformed(mainwindow.java:85) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2346) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.awteventmulticaster.mousereleased(awteventmulticaster.java:289) @ java.awt.component.processmouseevent(component.java:6525) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321) @ java.awt.component.processevent(component.java:6290) @ java.awt.container.processevent(container.java:2234) @ java.awt.component.dispatcheventimpl(component.java:4881) @ java.awt.container.dispatcheventimpl(container.java:2292) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4898) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4533) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4462) @ java.awt.container.dispatcheventimpl(container.java:2278) @ java.awt.window.dispatcheventimpl(window.java:2739) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:746) @ java.awt.eventqueue.access$400(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:697) @ java.awt.eventqueue$3.run(eventqueue.java:691) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:719) @ java.awt.eventqueue$4.run(eventqueue.java:717) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.awt.eventqueue.dispatchevent(eventqueue.java:716) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82)
expensedto
import java.math.bigdecimal; import java.util.date; // public class expense { bigdecimal fltamt=bigdecimal.zero; date dtdate; category category; long intexpenseid; public category getcategory() { return category; } public void setcategory(category category) { this.category = category; } public long getintexpenseid() { return intexpenseid; } public void setintexpenseid(long intexpenseid) { this.intexpenseid = intexpenseid; } public bigdecimal getfltamt() { return fltamt; } public void setfltamt(bigdecimal fltamt) { this.fltamt = fltamt; } public date getdtdate() { return dtdate; } public void setdtdate(date dtdate) { this.dtdate = dtdate; } }
mainwindow
.......
private void btnsaveactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: date selecteddate = (date) datepicker.getmodel().getvalue(); simpledateformat dateformat = new simpledateformat("yyyy-mm-dd"); cbitem selected_item = (cbitem) txtcategory.getselecteditem(); system.out.println(selected_item.getid()); system.out.println(selected_item.getname()); lbl.settext(txtamt.gettext() + "," + txtcategory.getselectedindex() + ","+ dateformat.format(selecteddate)); /* addnewpanel.add(lbl); addnewpanel.revalidate(); addnewpanel.repaint();*/ bigdecimal vl=new bigdecimal(txtamt.gettext()); system.out.print(bigdecimal.one); expense.setfltamt(bigdecimal.one); /*expense.setdtdate(new date(dateformat.format(selecteddate))); category.setintcatid(selected_item.getid()); expense.setcategory(category);*/ }
i understand error line of setting bigdecimal, not determine causing issue. when comment line works. copied required(i think) code here. wants full code, can copy too. great...!!
thank you...
edited- mainwindow code fully...
/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ package com.shams; import com.shams.dto.category; import com.shams.dto.expense; import com.shams.utils.dbconnection; import java.math.bigdecimal; import java.sql.connection; import java.sql.resultset; import java.sql.statement; import java.text.simpledateformat; import java.util.date; import javax.swing.jlabel; import net.sourceforge.jdatepicker.impl.jdatepanelimpl; import net.sourceforge.jdatepicker.impl.jdatepickerimpl; import net.sourceforge.jdatepicker.impl.utildatemodel; /** * * @author shamseer */ public class mainwindow extends javax.swing.jframe { expense expense; category category; /** * creates new form mainwindow */ public mainwindow() { initcomponents(); initmycomponents(); loaddata(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { mainpanel = new javax.swing.jpanel(); addnewpanel = new javax.swing.jpanel(); txtcategory = new javax.swing.jcombobox(); txtamt = new javax.swing.jtextfield(); btnsave = new javax.swing.jbutton(); viewpanel = new javax.swing.jpanel(); txtreporttype = new javax.swing.jcombobox(); menubaretapp = new javax.swing.jmenubar(); filemenu = new javax.swing.jmenu(); menuaddnew = new javax.swing.jmenuitem(); viewmenu = new javax.swing.jmenu(); jmenuitem1 = new javax.swing.jmenuitem(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); mainpanel.setlayout(new java.awt.cardlayout()); txtcategory.seteditable(true); txtcategory.setautoscrolls(true); txtcategory.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { txtcategoryactionperformed(evt); } }); txtcategory.addkeylistener(new java.awt.event.keyadapter() { public void keytyped(java.awt.event.keyevent evt) { txtcategorykeytyped(evt); } }); addnewpanel.add(txtcategory); txtamt.setpreferredsize(new java.awt.dimension(50, 25)); addnewpanel.add(txtamt); btnsave.settext("save"); btnsave.settooltiptext(""); btnsave.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { btnsaveactionperformed(evt); } }); addnewpanel.add(btnsave); mainpanel.add(addnewpanel, "cardaddnewpanel"); txtreporttype.setmodel(new javax.swing.defaultcomboboxmodel(new string[] { "item 1", "item 2", "item 3", "item 4" })); javax.swing.grouplayout viewpanellayout = new javax.swing.grouplayout(viewpanel); viewpanel.setlayout(viewpanellayout); viewpanellayout.sethorizontalgroup( viewpanellayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(viewpanellayout.createsequentialgroup() .addgap(20, 20, 20) .addcomponent(txtreporttype, javax.swing.grouplayout.preferred_size, 83, javax.swing.grouplayout.preferred_size) .addcontainergap(516, short.max_value)) ); viewpanellayout.setverticalgroup( viewpanellayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(viewpanellayout.createsequentialgroup() .addgap(21, 21, 21) .addcomponent(txtreporttype, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap(414, short.max_value)) ); mainpanel.add(viewpanel, "cardviewpanel"); getcontentpane().add(mainpanel, java.awt.borderlayout.center); filemenu.settext("file"); menuaddnew.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_n, 0)); menuaddnew.settext("add new"); filemenu.add(menuaddnew); menubaretapp.add(filemenu); viewmenu.settext("view"); jmenuitem1.setaccelerator(javax.swing.keystroke.getkeystroke(java.awt.event.keyevent.vk_v, 0)); jmenuitem1.settext("view transactions"); jmenuitem1.settooltiptext(""); viewmenu.add(jmenuitem1); menubaretapp.add(viewmenu); setjmenubar(menubaretapp); pack(); }// </editor-fold> private void txtcategoryactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: } private void txtcategorykeytyped(java.awt.event.keyevent evt) { // todo add handling code here: } private void btnsaveactionperformed(java.awt.event.actionevent evt) { // todo add handling code here: date selecteddate = (date) datepicker.getmodel().getvalue(); simpledateformat dateformat = new simpledateformat("yyyy-mm-dd"); cbitem selected_item = (cbitem) txtcategory.getselecteditem(); system.out.println(selected_item.getid()); system.out.println(selected_item.getname()); lbl.settext(txtamt.gettext() + "," + txtcategory.getselectedindex() + ","+ dateformat.format(selecteddate)); /* addnewpanel.add(lbl); addnewpanel.revalidate(); addnewpanel.repaint();*/ bigdecimal vl=new bigdecimal(txtamt.gettext()); system.out.print(bigdecimal.one); expense.setfltamt(bigdecimal.one); /*expense.setdtdate(new date(dateformat.format(selecteddate))); category.setintcatid(selected_item.getid()); expense.setcategory(category);*/ } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(mainwindow.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(mainwindow.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(mainwindow.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(mainwindow.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new mainwindow().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jpanel addnewpanel; private javax.swing.jpanel viewpanel; private javax.swing.jbutton btnsave; private javax.swing.jmenu filemenu; private javax.swing.jmenuitem jmenuitem1; private javax.swing.jpanel mainpanel; private javax.swing.jmenuitem menuaddnew; private javax.swing.jmenubar menubaretapp; private javax.swing.jtextfield txtamt; private javax.swing.jcombobox txtcategory; private javax.swing.jcombobox txtreporttype; private javax.swing.jmenu viewmenu; // end of variables declaration utildatemodel model = new utildatemodel(); jdatepanelimpl datepanel = new jdatepanelimpl(model); jdatepickerimpl datepicker = new jdatepickerimpl(datepanel); jlabel lbl=new jlabel(""); @suppresswarnings("unchecked") private void loaddata() { //jdatepickerutil datpick = new jdatepickerutil(); //cbitem array=new cbitem[]{"test",9}; //vector vctitem = new vector(); //cbitem[] cbit = null;//=new arraylist<>(); try{ connection conn = new dbconnection().createconnection(); statement stmt = conn.createstatement(); string sql = "select * category"; //list<string> catnames= new arraylist<>(); resultset rs = stmt.executequery(sql); //int size=0; while(rs.next()){ //catnames.add(rs.getstring("strcatname")); //cbit=new cbitem[size+1]; //cbit[size]=new cbitem(rs.getint("intcatid"), rs.getstring("strcatname"));//.addelement( new cbitem(1, "car" ) ); txtcategory.additem(new cbitem(rs.getint("intcatid"), rs.getstring("strcatname"))); //size++; } //txtcategory.additem(cbit); } catch(exception e){ e.printstacktrace(); } } private void initmycomponents() { addnewpanel.add(datepicker); addnewpanel.revalidate(); addnewpanel.repaint(); } } class cbitem{ int id; string name; cbitem(int i, string car) { this.id=i; this.name=car; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } @override public string tostring(){ return name+" ("+id+")"; } }
you have that:
expense expense;
variable defined, never set. npe. should not @ least initialize it?
expense expense = new expense();
Comments
Post a Comment