I'm trying to implement H2 database auto mixed mode using JSF 2.1 with
Primefaces 4.0 and Primefaces Mobile 0.9.5. I have a session scoped managed
bean UserBean.java which has a managed property of application scoped
managed bean with lazy initialization(eager = true) Database.java. I'm
trying to get a connection pool with 100 connections up and running. I
tried checking the object instance of Database class and I get the
reference of object correctly but the instance variable poolMgr is returned
null. can some one help me in resolving this issue?
**Error :**
java.lang.NullPointerException
javax.faces.FacesException: #{userBean.validateUser}:
java.lang.NullPointerException
at
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at
org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:681)
at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:452)
at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:138)
at
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:564)
at
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:213)
at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1083)
at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:379)
at
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:175)
at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1017)
at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:445)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:260)
at
org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:225)
at
org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:358)
at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
at java.lang.Thread.run(Thread.java:722)
Caused by: javax.faces.el.EvaluationException:
java.lang.NullPointerException
at
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 25 more
Caused by: java.lang.NullPointerException
at com.atr.media.db.Database.getDbConnection(Database.java:40)
at com.atr.web.login.UserBean.validateUser(UserBean.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 26 more
**faces-config.xml**
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"
version="2.1">
<managed-bean>
<description>holds user credentials for session</description>
<managed-bean-name>userBean</managed-bean-name>
<managed-bean-class>com.atr.web.login.UserBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>database</property-name>
<property-class>com.atr.media.db.Database</property-class>
<value>#{database}</value>
</managed-property>
</managed-bean>
<managed-bean>
<description>view scoped managed bean for user home page</description>
<managed-bean-name>analyzerFilterInput</managed-bean-name>
<managed-bean-class>com.atr.web.userhome.AnalyzerFilterInput</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<managed-bean eager="true">
<description>database connection</description>
<managed-bean-name>database</managed-bean-name>
<managed-bean-class>com.atr.media.db.Database</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<navigation-rule>
<display-name>login.xhtml</display-name>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>SUCCESS</from-outcome>
<to-view-id>/userhome.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
<navigation-rule>
<display-name>input_table.xhtml</display-name>
<from-view-id>/input_table.xhtml</from-view-id>
<navigation-case>
<from-outcome>SUCCESS</from-outcome>
<to-view-id>/tableplot.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
</faces-config>
**Database.java (managed property, which is application scoped)**
package com.atr.media.db;
import java.sql.Connection;
import java.sql.SQLException;
import org.h2.jdbcx.JdbcConnectionPool;
public class Database
{
public JdbcConnectionPool poolMgr;
private final String autoMixedMode = ";AUTO_SERVER=TRUE"; // Enables
// Automatic
// Mixed Mode
// for Database
public Database() throws SQLException
{
new Database("/data/test", 100);
System.out.println("object >>> " + Database.this);
}
public Database(String path, int noOfCon) throws SQLException
{
poolMgr = JdbcConnectionPool.create("jdbc:h2:" + path + autoMixedMode,
"sa", ""); // Creates
// Connection
// Pool
System.out.println("In constructor");
poolMgr.setMaxConnections(noOfCon);
System.out.println("poolMgr ::: " + poolMgr);
}
public Connection getDbConnection() throws SQLException
{
return poolMgr.getConnection();
}
}
**UserBean.java (backing bean)**
package com.atr.web.login;
import java.sql.Connection;
import java.sql.SQLException;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import com.atr.media.db.Database;
public class UserBean
{
private String name;
private String password;
@ManagedProperty("#{database}")
private Database database;
public UserBean()
{
// TODO Auto-generated constructor stub
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public Database getDatabase()
{
return database;
}
public void setDatabase(Database database)
{
this.database = database;
}
public String validateUser()
{
FacesContext fc = FacesContext.getCurrentInstance();
try
{
System.out.println("database object >>> "+database.poolMgr);
Connection con = database.getDbConnection();
//User Login Validation to be handled
}
catch (SQLException e)
{
//DB Connection Issue - Handle Message
e.printStackTrace();
}
return "SUCCESS";
}
}
**login.xhtml**
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:pm="http://primefaces.org/mobile">
<f:view contentType="text/html" renderKitId="PRIMEFACES_MOBILE">
<h:head>
<style type="text/css">
#logo {
height: 33px;
}
#formheader {
background-color: #000000;
-webkit-border-radius: 20px 20px 0 0;
-moz-border-radius: 20px 20px 0 0;
border-radius: 20px 20px 0 0;
color: #fff;
font-size: 28px;
padding: 20px 26px;
}
.formatpanel tr,.formatpanel td {
border: none;
margin: 50px auto;
width: 500px;
}
</style>
</h:head>
<h:body>
<pm:page id="page" title="Heading">
<pm:view id="loginview">
<pm:header title="Heading">
<f:facet name="left">
<a href="signup.xhtml">New User? Create Account</a>
<img id="logo" src="resources/images/msi.png"
style="float: left; display: inline" class="ui-btn-right" />
</f:facet>
</pm:header>
<pm:content>
<h:form id="loginform" style="margin: 50px auto;
width: 500px;">
<h2 id="formheader">Login</h2>
<p:messages />
<p:panelGrid columns="2" styleClass="formatpanel">
<p:outputLabel for="username"
style="font-weight:bold">Username:</p:outputLabel>
<p:inputText id="username" value="#{userBean.name}"
required="true" requiredMessage="Enter your username"
tabindex="1"></p:inputText>
<p:outputLabel for="password"
style="font-weight:bold">Password:</p:outputLabel>
<p:password id="password" value="#{userBean.password}"
required="true" requiredMessage="Enter your password"
tabindex="2">
<f:validateLength minimum="8" maximum="16"></f:validateLength>
</p:password>
</p:panelGrid>
<h:commandButton value="Login" action="#{userBean.validateUser}"
tabindex="3"></h:commandButton>
<h:outputLink value="loginissues.xhtml" tabindex="4">Can't access
account?</h:outputLink>
</h:form>
</pm:content>
</pm:view>
</pm:page>
</h:body>
</f:view>
</html>
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.