JSF的ValueChangeListener出现空指针异常
编了一个很简单的jsf例子,就是选择一个下拉框中的选项,然后当前页面同时显示出新值和旧值。新值没问题,但是把旧值那一行代码打上去就出现空指针异常,请问是怎么回事?ValueChangeListenerTest.java:
package src;
import javax.faces.model.*;
import javax.faces.event.*;
public class ValueChangeListenerTest {
private SelectItem[] races = {new SelectItem("HUM"), new SelectItem("ORC"), new SelectItem("UD"), new SelectItem("NE")};
private String selectedRace;
private String oldRace;
public String getOldRace() {
return oldRace;
}
public void setOldRace(String oldRace) {
this.oldRace = oldRace;
}
public SelectItem[] getRaces() {
return races;
}
public void setRaces(SelectItem[] races) {
this.races = races;
}
public String getSelectedRace() {
return selectedRace;
}
public void setSelectedRace(String selectedRace) {
this.selectedRace = selectedRace;
}
public void selectRace(ValueChangeEvent e) {
selectedRace = e.getNewValue().toString();
oldRace = e.getOldValue().toString();
}
}
页面显示文件:valueChangeListener.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java. prefix="f" %>
<%@taglib uri="http://java. prefix="h" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Value Change Listener Test</title>
</head>
<body>
<f:view>
<h:form>
Select a race:
<h:selectOneMenu immediate="true" valueChangeListener="#{vclt.selectRace}" onchange="submit();">
<f:selectItems value="#{vclt.races}"/>
</h:selectOneMenu><br/>
This is the race you selected:
<h:outputText value="#{vclt.selectedRace}"></h:outputText><br/>
This is your last race:
<h:outputText value="#{vclt.oldRace}"></h:outputText>
</h:form>
</f:view>
</body>
</html>
红色那行提示空指针。我试过在ValueChangeListenerTest.java中设置一个初始值,不行;在faces-config.xml也设置过初始值,还是不行,应该怎么解决?