File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes nullpointerexception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "nullpointerexception" Watch "nullpointerexception" New topic
Author

nullpointerexception

nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Hi guys,
Im using the following above statement but its giving me an error java.lang.NullPointer Exception.Can nyone point out me why its giving the error.Is it bcoz my connection object is a null object or something wrong with the prepared statement
nitin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

The only way that statement could cause an NPE is if the con variable is null.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Hi bibebault,
What could be the reasons that my connection object is null.Im using driver sun.jdbc.JdbcOdbcDriver does this support the ResultSet I mentioned.Or is it my datasource is locked
bye
nitin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

How could anyone possibly tell without seeing the code used to create the connection?
nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Hi bear,
I used 3 classes namely Connectionmanager,PoolManager,ConnectionPool,Im pasting the 3 source files.Please have a look.All the username, password,
datasourcename are taken from a confg file.
//CONNECTION MANAGER
package dao;
import java.sql.*;
// Referenced classes of package klse.dao:
// PoolManager
public class ConnectionManager
{
public ConnectionManager()
{
}
public void init()
{
poolMgr = PoolManager.getInstance();
connection = poolMgr.getConnection("db");
}
public void close()
throws SQLException
{
poolMgr.freeConnection("db", connection);
}
public ResultSet getResultSet()
{
return rs;
}
public PreparedStatement getStmt()
{
return stmt;
}
public Connection getConn()
{
return connection;
}
protected Connection connection;
protected PreparedStatement stmt;
protected ResultSet rs;
protected String query;
protected PoolManager poolMgr;
}
//////////////////////////////////////////////////

//POOL MANAGER
package dao;
import java.sql.*;
import java.util.*;
// Referenced classes of package klse.dao:
// ConnectionPool
public class PoolManager
{
private PoolManager()
{
drivers = new Vector();
pools = new Hashtable();
init();
}
public static synchronized PoolManager getInstance()
{
if(instance == null)
instance = new PoolManager();
clients++;
return instance;
}
private void init()
{
java.io.InputStream is = getClass().getResourceAsStream("/klse.properties");
Properties props = new Properties();
try
{
props.load(is);
}
catch(Exception exception)
{
return;
}
loadDrivers(props);
createPools(props);
}
private void loadDrivers(Properties props)
{
String driverClasses = props.getProperty("db.driver");
for(StringTokenizer st = new StringTokenizer(driverClasses); st.hasMoreElements()
{
String driverClassName = st.nextToken().trim();
try
{
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
}
catch(Exception exception) { }
}
}
private void createPools(Properties props)
{
for(Enumeration propNames = props.propertyNames(); propNames.hasMoreElements()
{
String name = (String)propNames.nextElement();
if(name.endsWith(".url"))
{
String poolName = name.substring(0, name.lastIndexOf("."));
String url = props.getProperty(poolName + ".url");
if(url != null)
{
String user = props.getProperty("db.user");
String password = props.getProperty("db.pwd");
String maxConns = props.getProperty("db.maxconns");
int max;
try
{
max = Integer.valueOf(maxConns).intValue();
}
catch(NumberFormatException numberformatexception)
{
max = 0;
}
String initConns = props.getProperty("db.initconns");
int init;
try
{
init = Integer.valueOf(initConns).intValue();
}
catch(NumberFormatException numberformatexception1)
{
init = 0;
}
String loginTimeOut = props.getProperty("db.logintimeout");
int timeOut;
try
{
timeOut = Integer.valueOf(loginTimeOut).intValue();
}
catch(NumberFormatException numberformatexception2)
{
timeOut = 5;
}
ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max, init, timeOut);
pools.put(poolName, pool);
}
}
}
}
public Connection getConnection(String name)
{
Connection conn = null;
ConnectionPool pool = (ConnectionPool)pools.get(name);
if(pool != null)
try
{
conn = pool.getConnection();
}
catch(SQLException sqlexception) { }
return conn;
}
public void freeConnection(String name, Connection conn)
{
ConnectionPool pool = (ConnectionPool)pools.get(name);
if(pool != null)
pool.freeConnection(conn);
}
public synchronized void release()
{
if(--clients != 0)
return;
ConnectionPool pool;
for(Enumeration allPools = pools.elements(); allPools.hasMoreElements(); pool.release())
pool = (ConnectionPool)allPools.nextElement();
for(Enumeration allDrivers = drivers.elements(); allDrivers.hasMoreElements()
{
Driver driver = (Driver)allDrivers.nextElement();
try
{
DriverManager.deregisterDriver(driver);
}
catch(SQLException sqlexception) { }
}
}
private static PoolManager instance;
private static int clients;
private Vector drivers;
private Hashtable pools;
}
//////////////////////////////////////////////////
//CONNECTION POOL
package dao;
import java.sql.*;
import java.util.Enumeration;
import java.util.Vector;
public class ConnectionPool
{
public ConnectionPool(String name, String URL, String user, String password, int maxConns, int initConns, int timeOut)
{
this.name = "";
this.URL = "";
this.user = "";
this.password = "";
this.maxConns = 0;
this.initConns = 0;
this.timeOut = 0;
freeConnections = new Vector();
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConns = maxConns;
this.timeOut = timeOut <= 0 ? 5 : timeOut;
initPool(initConns);
}
private void initPool(int initConns)
{
for(int i = 0; i < initConns; i++)
{
Connection pc;
try
{
pc = new_Connection();
}
catch(SQLException sqlexception) { }
}
}
private Connection new_Connection()
throws SQLException
{
Connection conn = null;
if(user == null)
conn = DriverManager.getConnection(URL);
else
conn = DriverManager.getConnection(URL, user, password);
return conn;
}
public Connection getConnection()
throws SQLException
{
return getConnection(timeOut * 1000);
}
private synchronized Connection getConnection(long timeout)
throws SQLException
{
long startTime = System.currentTimeMillis();
long remaining = timeout;
Connection conn;
for(conn = null; (conn = getPooledConnection()) == null
{
try
{
wait(remaining);
}
catch(InterruptedException interruptedexception) { }
remaining = timeout - (System.currentTimeMillis() - startTime);
if(remaining <= 0L)
throw new SQLException("getConnection() timed-out");
}
if(!isConnectionOK(conn))
{
return getConnection(remaining);
} else
{
checkedOut++;
return conn;
}
}
private boolean isConnectionOK(Connection conn)
{
Statement stmt = null;
try
{
if(!conn.isClosed())
{
stmt = conn.createStatement();
stmt.close();
} else
{
return false;
}
}
catch(SQLException sqlexception)
{
if(stmt != null)
try
{
stmt.close();
}
catch(SQLException sqlexception1) { }
return false;
}
return true;
}
private Connection getPooledConnection()
throws SQLException
{
Connection conn = null;
if(freeConnections.size() > 0)
{
conn = (Connection)freeConnections.firstElement();
freeConnections.removeElementAt(0);
} else
if(maxConns == 0 || checkedOut < maxConns)
conn = new_Connection();
return conn;
}
public synchronized void freeConnection(Connection conn)
{
freeConnections.addElement(conn);
checkedOut--;
notifyAll();
}
public synchronized void release()
{
for(Enumeration allConnections = freeConnections.elements(); allConnections.hasMoreElements()
{
Connection conn = (Connection)allConnections.nextElement();
try
{
conn.close();
}
catch(SQLException sqlexception) { }
}
freeConnections.removeAllElements();
}
private String name;
private String URL;
private String user;
private String password;
private int maxConns;
private int initConns;
private int timeOut;
private int checkedOut;
private Vector freeConnections;
}
[ February 11, 2004: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

How about paring it down to the section of code where you obtain the connection for using with your createStatement call? And please use the UBB code tags to keep your code readable and formatted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nullpointerexception
 
Similar Threads
Regarding bean references
couldn't send request to the server.404 error recieving
String reference problem
generics...
Batch Update using spring and ibatis