-3

私の問題は単にnullpointerexceptionですが、それを理解する方法がわかりません。コードは次のとおりです。

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メソッドでは、ユーザーを追加しようとすると取得されます。ChatViewUnsupportedOperationException

次のようにリストを作成する必要があります: -

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;

クラス変数と同じです。

そのため、使用する前に null 以外のオブジェクトに初期化する必要があります。

于 2012-12-25T20:00:09.603 に答える