The Artima Developer Community
Sponsored Link

Java Answers Forum
Null pointer exception during executeUpdate(sql)

2 replies on 1 page. Most recent reply: Jun 9, 2008 5:23 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Tanvir Hossain

Posts: 53
Nickname: tanvirtonu
Registered: Feb, 2006

Null pointer exception during executeUpdate(sql) Posted: May 5, 2008 11:55 PM
Reply to this message Reply
Advertisement
Can anybody tell me what is the problem with the following code.Whenever I run this,it throws a null pointer exception.Can anybody tell me the reason why it throws null pointer exception.My database has seven fields of type "Text".
 
import java.sql.*;
public class DBCon
{      	Connection con=null;
	Statement stmt;
	public DBCon()
	{
	insert();
	}//End of Constructor
 public static void main(String[] args) {
    	 DBCon db = new DBCon();     		}
  		
public void insert(){
  		
try{	//----------------- Driver -------------------
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//----------------- URL WITHOUT DSN -------		
String conString = "jdbc:odbc:DRIVER={MicroSoft Access Driver (*.mdb)};" + 
         		"DBQ=./Data/MuktadirNiketon.mdb";
 
con = DriverManager.getConnection(conString,"","");
        				
String sql=" INSERT INTO Flat_Rent VALUES ('3-2008','ss','ss','ss','ss','ss','ss') " ; 
				System.out.println("SQL>>"+sql);	
stmt.executeUpdate(sql);
			    
}catch(SQLException sqle){
System.out.println("DSN Not Found !!!");		}
catch(ClassNotFoundException sqle){
System.out.println("Driver Not Found !!!");		}
						
}
}//End of Class


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Null pointer exception during executeUpdate(sql) Posted: May 6, 2008 1:59 AM
Reply to this message Reply
The variable stmt is not being initialised before it is used. The Statement object sends the SQL to the database but has not been given initialised with the database connection con. You need a line similar to:
stmt = con.createStatement();
in there somewhere (between making the connection and executing the statement).

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Null pointer exception during executeUpdate(sql) Posted: Jun 9, 2008 5:23 AM
Reply to this message Reply
Woah, JDBC + Microsoft Access as a Database? This surely has to be a small project? No persistence framework (less error prone) or something more capable than MS Access (PostgreSql - cheap and decent)?

Flat View: This topic has 2 replies on 1 page
Topic: SQL server connecting problem Previous Topic   Next Topic Topic: Problem during executing Command Prompt through java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use