java - get Nullpointerexception while insert records -



java - get Nullpointerexception while insert records -

this question has reply here:

what null pointer exception, , how prepare it? 12 answers

i trying insert info database using prepare statements nullpointerexception

my database connection code

public class dbconnect { connection conn=null; public static connection connecrdb(){ try{ class.forname("org.sqlite.jdbc"); connection conn = drivermanager.getconnection("jdbc:sqlite:e:\\netbeansprojects\\abdo`s project\\project.sqlite"); system.out.println("connection success"); conn.setautocommit(false); homecoming conn; } catch(exception e){ joptionpane.showmessagedialog(null, e); homecoming null; } } }

and code in main class

public class items extends javax.swing.jframe { connection conn =null; resultset rs = null; preparedstatement pst=null; public items() { initcomponents(); dbconnect.connecrdb(); } private void savebtnactionperformed(java.awt.event.actionevent evt) { try{ string sql = "insert test (name) values(?)"; pst=conn.preparestatement(sql); pst.setstring(1,item_name.gettext() ); pst.executeupdate(); conn.commit(); } catch(exception e ){ system.out.println(e); system.out.println(e.getstacktrace()); } }

output is

java.lang.nullpointerexception [ljava.lang.stacktraceelement;@7da469fe

what wrong in code ?

conn null in savebuttonactionperformed. initialize in item class:

public class items extends javax.swing.jframe { connection conn = drivermanager.getconnection("jdbc:sqlite:e:\\netbeansprojects\\abdo`s project\\project.sqlite");

you can initialize within method, sure initialize before utilize it.

java sqlite jdbc

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -