-3

我的问题只是一个空指针异常,但是,我不明白我是如何弄清楚的。这是代码。

我正在尝试运行 primefaces 的聊天实用程序。我正在使用 tomcat 7 .. 就我可以到达这里但现在我遇到了这个问题。

public class ChatView {

private final PushContext pushContext = PushContextFactory.getDefault().getPushContext();

private ChatUsers users;

    private String privateMessage;

private String globalMessage;

    private String username;

    private boolean loggedIn;

private String privateUser;

private final static String CHANNEL = "/chat/";

public void setUsers(ChatUsers users) {
    this.users = users;
}

public String getPrivateUser() {
    return privateUser;
}

public void setPrivateUser(String privateUser) {
    this.privateUser = privateUser;
}

public String getGlobalMessage() {
    return globalMessage;
}

public void setGlobalMessage(String globalMessage) {
    this.globalMessage = globalMessage;
}

public String getPrivateMessage() {
    return privateMessage;
}

public void setPrivateMessage(String privateMessage) {
    this.privateMessage = privateMessage;
}

    public String getUsername() {
            return username;
    }
    public void setUsername(String username) {
            this.username = username;
    }

    public boolean isLoggedIn() {
            return loggedIn;
    }
    public void setLoggedIn(boolean loggedIn) {
            this.loggedIn = loggedIn;
    }

    public void sendGlobal() {
    pushContext.push(CHANNEL + "*", username + ": " + globalMessage);

            globalMessage = null;
    }

public void sendPrivate() {
    pushContext.push(CHANNEL + privateUser, "[PM] " + username + ": " + privateMessage);

    privateMessage = null;
}

    public void login() {
    RequestContext requestContext = RequestContext.getCurrentInstance();

            if(users.contains(username)) {
        loggedIn = false;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username taken", "Try with another username."));

        requestContext.update("growl");
    }
    else {
        users.addUser(username);
        pushContext.push(CHANNEL, username + " joined the channel.");
        requestContext.execute("subscriber.connect('/" + username + "')");
        loggedIn = true;
    }
    }

public void disconnect() {
    //remove user and update ui
    users.removeUser(username);
    RequestContext.getCurrentInstance().update("form:users");

    //push leave information
    pushContext.push(CHANNEL, username + " left the channel.");

    //reset state
    loggedIn = false;
    username = null;
}
}

……

public class User implements Serializable {


private static final long serialVersionUID = 1L;
    private String firstname;
    private String lastname;
    private Integer age;
    private String street;
    private String city;
    private String postalCode;
    private String info;
    private String email;
    private String phone;

    public String getFirstname() {
            return firstname;
    }

    public void setFirstname(String firstname) {
            this.firstname = firstname;
    }

    public String getLastname() {
            return lastname;
    }

    public void setLastname(String lastname) {
            this.lastname = lastname;
    }

    public Integer getAge() {
            return age;
    }

    public void setAge(Integer age) {
            this.age = age;
    }

    public String getStreet() {
            return street;
    }

    public void setStreet(String street) {
            this.street = street;
    }

    public String getCity() {
            return city;
    }

    public void setCity(String city) {
            this.city = city;
    }

    public String getPostalCode() {
            return postalCode;
    }

    public void setPostalCode(String postalCode) {
            this.postalCode = postalCode;
    }

    public String getInfo() {
            return info;
    }

    public void setInfo(String info) {
            this.info = info;
    }

    public String getEmail() {
            return email;
    }

    public void setEmail(String email) {
            this.email = email;
    }

    public String getPhone() {
            return phone;
    }

    public void setPhone(String phone) {
            this.phone = phone;
    }
}

……

public class ChatUsers {

private List<String> users = Arrays.asList("fatih");


@PostConstruct
public void init() {
    this.users = new ArrayList<String>();
}

public List<String> getUsers() {
    return users;
}

public void setUsers(List<String> users) {
    this.users = users;
}

public void addUser(String user) {
    this.users.add(user);
}

public void removeUser(String user) {
    this.users.remove(user);
}

public boolean contains(String user) {
    return this.users.contains(user);
}
}

我得到了 ChatView 类和这条线的例外if(users.contains(username))

任何建议都会非常有帮助。谢谢..

编辑

'SEVERE: Received 'java.lang.UnsupportedOperationException' when invoking action  listener '#{chatView.login}' for component 'j_idt21'
Dec 25, 2012 10:01:18 PM javax.faces.event.MethodExpressionActionListener processAction
SEVERE: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at org.primefaces.examples.view.ChatUsers.addUser(ChatUsers.java:43)
at org.primefaces.examples.view.ChatView.login(ChatView.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Dec 25, 2012 10:01:18 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of INVOKE_APPLICATION 5 : UIComponent-ClientId=form:j_idt21, Message=java.lang.UnsupportedOperationException
Dec 25, 2012 10:01:18 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: java.lang.UnsupportedOperationException
javax.faces.event.AbortProcessingException: java.lang.UnsupportedOperationException
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
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.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at org.primefaces.examples.view.ChatUsers.addUser(ChatUsers.java:43)
at org.primefaces.examples.view.ChatView.login(ChatView.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at   javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListen    er.java:153)
... 25 more

'

4

2 回答 2

9

你没有在你的类中初始化ChatUsers引用。所以,当你使用它时CharView,你会得到一个。NPE或者你可能没有在setUsers方法之前调用方法login,所以你users仍然没有初始化。

您的声明:-

private ChatUsers users;

应改为: -

private ChatUsers users = new ChatUsers();

更新 : -

在您的CharView班级中,当您创建如下列表时: -

private List<String> users = Arrays.asList("fatih");

你得到一个固定大小的列表,你不能修改它。所以,在类的addUsers方法中,当你尝试向它添加用户时ChatView,你会得到。UnsupportedOperationException

您应该像这样创建您的列表:-

private List<String> users = new ArrayList<String>();

如果您想在声明时初始化该列表,您可以执行以下操作: -

private List<String> users = new ArrayList<String>() {{
    add("fatih");
}};

这称为双括号初始化

于 2012-12-25T19:54:08.420 回答
0

请记下

private ChatUsers users;

private ChatUsers users = null;

类变量相同。

所以你必须在使用它之前将它初始化为非空对象。

于 2012-12-25T20:00:09.603 回答