Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerExeption when connecting to MS SQL  RSS feed

 
Anush Taranyan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my web.xml code

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:jtds:sqlserver://localhost;DatabaseName=Hospital;instance=SQLEXPRESS</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>taranyan</param-value>
</context-param>
<context-param>
<param-name>driver</param-name>
<param-value>net.sourceforge.jtds.jdbc.Driver</param-value>
</context-param>
<listener>
<listener-class>Hospital.listener.ContextListener</listener-class>
</listener>

and this is my java code

public class ContextListener implements ServletContextListener {

public DataProvider dp = null;

public void contextInitialized(ServletContextEvent arg0) {
ServletContext c = arg0.getServletContext();
String url = c.getInitParameter("url");
String username = c.getInitParameter("username");
String password = c.getInitParameter("password");
String driver = c.getInitParameter("driver");
dp = new DataProvider(url, username, password);
c.setAttribute("db", dp);

try { // Registering JTDS Driver
Class.forName(driver);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
dp = new DataProvider(url, username, password);
c.setAttribute("db", dp);
}

public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub

}
}

getInitParameter not working, throws EvaluationException and NullPointerExeption
 
Tim Holloway
Bartender
Posts: 18661
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Anush, but Please Don't Shout! .

There's a "Code" button on the message editor. You can use it to make your sample code and XML easier to read.

Your approach to working with a database is very odd. First of all, you're attempting to obtain a connection by brute force instead of getting it from a Connection Pool. Secondly, you're doing it in JSF-specific code, which normally isn't how people do this. Finally, you're attempting to pass this connection down via a modification to the Servlet Context.

That last quirk is the one that's killing you, I think. In many J2EE servers, the Servlet Context is a read-only object. Meaning that if you attempt to modify it in any way in application code, the modification attempts may silently fail and therefore the data you thought you had won't be there when you need it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!