Uczę się pisać używająć JSF i napotkałem się na problem że gdy używam klasy Integer, wyrzuca mi NullPointerException a nie umiem dojść do tego w którym momencie następuje odwołanie do null'a. Co jest nie tak?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Beans;
import java.io.Serializable;
import java.util.Random;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
/**
*
* @author Shadist
*/
@ManagedBean(name = "mangedBean1")
@SessionScoped
public class MangedBean implements Serializable {
Integer randomInt;
Integer userNumber;
String response;
public String getResponse() {
if ((this.userNumber != null) && (this.userNumber.compareTo(this.randomInt)==0)){
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
session.invalidate();
return "Yay! You got it!";
} else {
return "<p>Sorry, " + userNumber + " isn't it.</p>"
+ "<p> Guess again...</p>";
}
}
public int getUserNumber() {
return userNumber;
}
public void setUserNumber(int userNumber) {
this.userNumber = userNumber;
}
public MangedBean() {
Random randomGR = new Random();
this.randomInt = new Integer(randomGR.nextInt(10));
System.out.println("Duke's number: " + randomInt);
}
}
A tutaj strona
<?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://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<body>
<ui:composition template="./../Templates/Template1.xhtml">
<ui:define name="top">
<h2>Guess a number</h2>
</ui:define>
<ui:define name="left">
<h5>Can you guees it?</h5>
<h:form>
<h:inputText id="userNumber" size="2" maxlength="2"
value="#{mangedBean1.userNumber}" />
<h:commandButton id="submit" value="submit" action="response"/>
</h:form>
</ui:define>
<ui:define name="content">
</ui:define>
</ui:composition>
</body>
</html>
Używam netbeans 8.1 oraz apache tomcat 8
Marszal