mysql - Pushing sql statments from java to database, executeUpdate() returns NullPointerException -


so trying store information database java application, getting error: ""awt-eventqueue-0" java.lang.nullpointerexception" , can life of me not find reason. jave tried changing names of variables , have tried using "preparedstatment" aswell. yet nothing has worked far.

public class db {  public static final string driver ="com.mysql.jbdc.driver"; public static final string url = "jdbc:mysql://localhost:3306/oving8"; public static final string user = "root"; public static final string pw = ""; private static connection kobling;  private static statement settoppstatment(){     try {         class.forname(driver);         connection tilkobling = drivermanager.getconnection(url, user, pw);         return tilkobling.createstatement();     } catch (classnotfoundexception e) {         system.out.println("can not find class in");     } catch (sqlexception e) {         system.out.println("can not connect database");         }     return null; } public static void skrivenhettildb(enhet enhet){ statement st = settoppstatment();  string id = enhet.getid(); string navn = enhet.getnavn(); string sqlenhet = null;   if (enhet != null){     sqlenhet = "insert enhet values('"+ id + "','" +  navn + "');"; }; system.out.println(sqlenhet);  try {     st.executeupdate(sqlenhet);  <---- line 123 } catch (sqlexception e) {     system.out.println("this unit registerd"); } try {     st.getconnection().close(); } catch (sqlexception e) {     e.printstacktrace(); } 

};

and stacktrace:

exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ fil.db.skrivenhettildb(db.java:123) @ main.jframe$9.actionperformed(jframe.java:385) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2348) @ 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:6535) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3324) @ java.awt.component.processevent(component.java:6300) @ java.awt.container.processevent(container.java:2236) @ java.awt.component.dispatcheventimpl(component.java:4891) @ java.awt.container.dispatcheventimpl(container.java:2294) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4888) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4525) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4466) @ java.awt.container.dispatcheventimpl(container.java:2280) @ java.awt.window.dispatcheventimpl(window.java:2750) @ java.awt.component.dispatchevent(component.java:4713) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:758) @ java.awt.eventqueue.access$500(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:709) @ java.awt.eventqueue$3.run(eventqueue.java:703) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:731) @ java.awt.eventqueue$4.run(eventqueue.java:729) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:728) @ 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) 

the following code return null if getting of exceptions. in case since have sysout should see messages in console or log file.

you use debugger check line line happening.

private static statement settoppstatment(){     try {         class.forname(driver);         connection tilkobling = drivermanager.getconnection(url, user, pw);         return tilkobling.createstatement();     } catch (classnotfoundexception e) {         system.out.println("can not find class in");     } catch (sqlexception e) {         system.out.println("can not connect database");         }     return null; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -