sqlite - insert blob into sqlitedatabase java -
i have created data base , gui collects data user, want add collected data database. connection db good, function called on button click failing, returning message "null pointer exception: null"
system output:
opened database add database java.lang.nullpointerexception: null
addtobase:
private void addtobase(string name, int row) throws exception, ioexception, sqlexception { preparedstatement st = null; fileinputstream fis = null; connection con = null; try { class.forname("org.sqlite.jdbc").newinstance(); con = drivermanager.getconnection("jdbc:sqlite:heores.sqlite"); con.setautocommit(false); system.out.println("opened database successfully"); fileinputstream inputstream= new fileinputstream(selectedfile.getabsolutepath()); string sql = "insert hero (name, row, img) values (?, ?, ?)"; st = con.preparestatement(sql); st.setstring(1, name); st.setint(2, row); st.setblob(3,inputstream); st.executeupdate(); con.commit(); system.out.println("add database successfully"); } catch(exception e) { system.err.println( e.getclass().getname() + ": " + e.getmessage() ); system.exit(0); } { st.close(); fis.close(); //<--- null here? } }
button action listener:
private void jbutton2actionperformed(java.awt.event.actionevent evt) { string name; int row; switch(row.getselecteditem().tostring()){ case "front":{ row =1; } case "middle":{ row =2; } case "back":{ row =3; } default:{ row=0; } } name=name1.gettext(); //here catches null pointer exeption, addtobase functions seems work fine outputs printed try { addtobase(name, row); } catch ( exception e ) { system.err.println( e.getclass().getname() + ": " + e.getmessage() ); system.exit(0); } }
any ideas whats wrong? after opening database seems columns filled except img column (blob), file i'm inserting declared in main:
public class main extends javax.swing.jframe { image image; file selectedfile = new file("avatar.png"); //...
i have checked
if( input!= null){ function(input); }
but variables , inputs seems initialized
update: ok i've got stack trace
run: opened database add database java.lang.nullpointerexception: null java.lang.nullpointerexception @ classes.main.addtobase(main.java:82) @ classes.main.jbutton2actionperformed(main.java:304) @ classes.main.access$200(main.java:23) @ classes.main$3.actionperformed(main.java:169) @ 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.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)
fileinputstream fis = null;
not set anywhere in code , then:
finally { st.close(); fis.close(); //<--- null here? }
so fis == null , trying close , exception. pointed there in code comment.
Comments
Post a Comment