Guten Mittag miteinander,
Bin soeben wieder auf ein Problem gestossen mit meiner Registrierungs Seite.
Mir wird eine NullPointerException auf meine validator Methode ausgeworfen:
register.xhtml
registerBean.java
web.xml
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PrimesTest</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/XML]
faces-config.xml
[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_0.xsd"
version="2.0">
<!-- Settings for application -->
<application>
<locale-config>
<default-locale>de</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
<message-bundle>beans.messages</message-bundle>
<resource-bundle>
<base-name>beans.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
[/XML]
Ich hoffe ich bin nicht blind und habe etwas falsch geschrieben.
Vielen Dank schonmal für die Hilfe
Bin soeben wieder auf ein Problem gestossen mit meiner Registrierungs Seite.
Mir wird eine NullPointerException auf meine validator Methode ausgeworfen:
Code:
/register.xhtml @39,69 validator="#{registerBean.validateUser}": java.lang.NullPointerException
register.xhtml
HTML:
<!--
This is the register screen. Welcome to Noten Verwalter.
-->
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<title>Registrierung für Notenverwalter</title>
<link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" />
</h:head>
<h:body>
<center>
<p:panel header="Register Form" style="width: 350;">
<h:form>
<h:panelGrid columns="2" cellpadding="2">
<h:outputLabel for="LoginName" value="#{msgs.accountname}" />
<h:panelGroup>
<h:inputText id="LoginName" value="# {registerBean.userName}"
required="true" size="15"
binding="#{registerBean.register}" />
</h:panelGroup>
<h:outputLabel for="Password" value="#{msgs.password}" />
<h:panelGroup>
<p:password id="Password" value="#{registerBean.password}"
required="true" size="15" feedback="true" />
</h:panelGroup>
<h:outputLabel for="repeatPassword" value="#{msgs.repeatPassword}" />
<h:panelGroup>
<p:password id="repeatPassword" value="#{registerBean.repeatPassword}"
required="true" size="15"
validator="#{registerBean.validateUser}" feedback="false" />
</h:panelGroup>
<h:outputLabel for="Vorname" value="#{msgs.vorname}" />
<h:panelGroup>
<h:inputText id="Vorname" value="#{registerBean.vorname}"
required="true" size="15" />
</h:panelGroup>
<h:outputLabel for="Nachname" value="#{msgs.nachname}" />
<h:panelGroup>
<h:inputText id="Nachname" value="#{registerBean.nachname}"
required="true" size="15" />
</h:panelGroup>
<h:outputLabel for="Klasse" value="#{msgs.klasse}" />
<h:panelGroup>
<h:selectOneRadio id="Klasse" value="#{registerBean.klasse}" required="true">
<f:selectItem itemValue="1" itemLabel="5IA09" />
<f:selectItem itemValue="2" itemLabel="5IS09" />
</h:selectOneRadio>
</h:panelGroup>
</h:panelGrid>
<p:commandButton id="registerButton" ajax="false" type="submit" value="#{msgs.registerb}" action="#{registerBean.register}" />
<p:commandButton value="#{msgs.cancel}" type="reset"/>
</h:form>
</p:panel>
<div><p:messages ></p:messages></div>
</center>
</h:body>
</html>
registerBean.java
Java:
package beans;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.naming.NamingException;
@ManagedBean
@RequestScoped
public class RegisterBean {
private UIInput UIregister;
public String userName;
public String vorname;
public String nachname;
public String password;
public String repeatPassword;
public String klasse;
public RegisterBean() {}
// Validator_Methode: Überprüft ob in DB User mit diesem Passwort existiert
public void validateUser(FacesContext context, UIComponent component, Object value) throws SQLException, NamingException {
userName = (String) UIregister.getLocalValue();
try {
String sql = "SELECT username FROM user WHERE username = '" + userName + "'";
// String sql = "SELECT name, vorname FROM user WHERE username = '" + userName + "' AND password = '" + value.toString() + "'";
ResultSet rs = dataBaseBeans.DatabaseManager.getDatebaseManager().sendQuery(sql);
if (!rs.next()) {
System.out.println("User existiert NICHT");
}
else {
FacesMessage message = new FacesMessage("User existiert schon!");
context.addMessage(component.getClientId(context),message);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void register() throws SQLException, NamingException {
boolean isValidPw = validatePassword();
if(isValidPw) {
System.out.println("Password Is Valid");
int role_fk = 1;
//String sql = "insert into user values ("+ Integer.valueOf(klasse)+ ", 1, "+ userName +", "+ password +", "+ nachname +", "+ vorname +");";
String sql = "INSERT INTO user "
+ "(klasse_fk, role_fk, username, password, name, vorname) "
+ "VALUES "
+ "('" + Integer.valueOf(klasse) + "',"
+ "('" + role_fk + "',"
+ "('" + userName + "',"
+ "('" + password + "',"
+ "('" + nachname + "',"
+ "('" + vorname + "')";
try {
dataBaseBeans.DatabaseManager.getDatebaseManager().sendUpdate(sql);
System.out.println("Erfolgreich Inserted");
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
}
}
public boolean validatePassword() {
boolean isValid = false;
if((password.equals(repeatPassword) && password !=null)) {
isValid = true;
}
return isValid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getNachname() {
return nachname;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepeatPassword() {
return repeatPassword;
}
public void setRepeatPassword(String repeatPassword) {
this.repeatPassword = repeatPassword;
}
public String getKlasse() {
return klasse;
}
public void setKlasse(String klasse) {
this.klasse = klasse;
}
// Methode setzt die loginID welche zur Validation gebraucht wird. Wird an "userid" aus login.xhtml gebunden
public void setRegister(UIInput UIregister) {
this.UIregister = UIregister;
System.out.println("Setted the register UIInput");
}
public UIInput getRegister() {
return UIregister;
}
}
web.xml
[XML]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>PrimesTest</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
[/XML]
faces-config.xml
[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_0.xsd"
version="2.0">
<!-- Settings for application -->
<application>
<locale-config>
<default-locale>de</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
<message-bundle>beans.messages</message-bundle>
<resource-bundle>
<base-name>beans.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
[/XML]
Ich hoffe ich bin nicht blind und habe etwas falsch geschrieben.
Vielen Dank schonmal für die Hilfe