java - Exception Adding elements to a JList -
i'm writing code retrieve data mysql database.
want add 1 column of query jlist.
this code:
try { arraylist<string> names = new arraylist(); db = new database(); db.connect(); statement st = db.getconnection().createstatement(); resultset rs = st.executequery("select nombre alumnos"); while (rs.next()) { names.add(rs.getstring("nombre")); } defaultlistmodel listmodel = new defaultlistmodel(); for(string nms : names) { listmodel.addelement(nms); } //this line 140 // , lstinicio jlist lstinicio.setmodel(listmodel); } catch (sqlexception sql) { system.err.println(sql.getmessage()); }
i think ok, when reach part of code exception:
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ proyecto.ausentismo.mainwindow.loadlist(mainwindow.java:140) @ proyecto.ausentismo.mainwindow.<init>(mainwindow.java:21) @ proyecto.ausentismo.login.btnloginactionperformed(login.java:121) @ proyecto.ausentismo.login.access$000(login.java:11) @ proyecto.ausentismo.login$1.actionperformed(login.java:49) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener$actions.actionperformed(basicbuttonlistener.java:303) @ javax.swing.swingutilities.notifyaction(swingutilities.java:1664) @ javax.swing.jcomponent.processkeybinding(jcomponent.java:2879) @ javax.swing.jcomponent.processkeybindings(jcomponent.java:2926) @ javax.swing.jcomponent.processkeyevent(jcomponent.java:2842) @ java.awt.component.processevent(component.java:6282) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4861) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.keyboardfocusmanager.redispatchevent(keyboardfocusmanager.java:1895) @ java.awt.defaultkeyboardfocusmanager.dispatchkeyevent(defaultkeyboardfocusmanager.java:762) @ java.awt.defaultkeyboardfocusmanager.predispatchkeyevent(defaultkeyboardfocusmanager.java:1027) @ java.awt.defaultkeyboardfocusmanager.typeaheadassertions(defaultkeyboardfocusmanager.java:899) @ java.awt.defaultkeyboardfocusmanager.dispatchevent(defaultkeyboardfocusmanager.java:727) @ java.awt.component.dispatcheventimpl(component.java:4731) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.window.dispatcheventimpl(window.java:2719) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:729) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:688) @ java.awt.eventqueue$3.run(eventqueue.java:686) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:702) @ java.awt.eventqueue$4.run(eventqueue.java:700) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:699) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)
does know causing exception? how find out? edit: commented line of error. , stil exception
as suspected, lstinicio
variable null. solution: code find out think initialized it, , see why didn't initialized. that's can based on you've posted far.
edit: @jesuscc29: glad you've got working. key lesson here not answer specific problem you're having, rather general process of debugging nullpointerexception (npe), since can guarantee you'll run these buggers time , time again. first find out line, , variable on line causing npe occur, , try trace code see why variable null. this, , 90% of time you'll find problem , it's solution.
Comments
Post a Comment