-1

So I have this very weird issue... I have tried searching all over StackOverflow and tried everything I could find, but I simply cannot fix this error from happening. The error is:

/index.xhtml @15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null

It's a school project and the project works for some of my classmates, but it also won't work for some. Could it be NetBeans screwing up when we open them? So far at least 2 people got it working, but at least 2 people also got this error.

index.xhtml

<?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:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Welcome</title>
    </h:head>
    <h:body>
        <h:form>
            <h3>Please enter your name and password.</h3>   
            <table>
                <tr>
                    <td>Name:</td>
                    <td><h:inputText value="#{user.name}"/></td>
                    <td>.</td>
                    <td><h:inputText value="#{user.userId}"/></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><h:inputSecret value="#{user.password}"/></td>
                </tr>
            </table>
            <p><h:commandButton value="Login" action="welcome"/></p>
        </h:form>
    </h:body>
</html>

welcome.xhtml

<?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:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Welcome</title>
    </h:head>
    <h:body>
        <h3>Welcome to JavaServer Faces, #{user.name}.#{user.userId}!</h3>
        <h3>your password is #{user.password}</h3>
    </h:body>
    <h:form>
        <h:commandButton value="Logout" action="index"/>
    </h:form>
</html>

UserBean.java

package com.corejsf;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("user")
@SessionScoped
public class UserBean implements Serializable {

    private String name;
    private String password;
    private Integer userId;

    public String getName() {
        return name;
    }

    public void setName(String newValue) {
        name = newValue;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String newValue) {
        password = newValue;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer newValue) {
        userId = newValue;
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index";
    }
}

What it basically does, is take any username, id, and password on index.xhtml. Then you press login, which sends you over to welcome.xhtml (because of action="welcome"). welcome.xhtml should then post your username, id, and password on the page. However, when I press the login button, I get this error:

/index.xhtml @15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null

I did try switching to ManagedBean (@ManagedBean(name="user")) but that resulted in the same thing.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • http://stackoverflow.com/q/30128395/1391249 – Tiny Jan 10 '16 at 17:28
  • @Tiny The odd thing about this is, if I send the project to another classmate, he can easily open it in NetBeans and it works fine. Some of us have this same problem, but it also works for some. Could it be NetBeans screwing up? – MortenMoulder Jan 10 '16 at 17:55
  • No. NetBeans has got nothing to do with that problem. The server is in charge of running the application which knows nothing about NetBeans or any other IDE being used. It could be related to a bean management framework being used like CDI, JSF, Spring. Try to properly clean & build / deploy the application. An appropriate match should be found in the linked question. – Tiny Jan 10 '16 at 18:11
  • @Tiny NetBeans does, however, determine which JSF version it should run, right? What I do not understand, is why it works for some and it doesn't work for the others. I did check everything through on that linked question before I asked here myself, unfortunately. – MortenMoulder Jan 10 '16 at 18:17
  • Does it work, if attempted in a new test application having nothing other than one XHTML file and a single managed bean with no extra dependencies? If I were to create an application having the same code as XHTML / managed bean, it would work as this kind of things already works without any flaw in my environment. Therefore, it is difficult to get hold of the actual cause as it is not reproducible in the very same environment. – Tiny Jan 10 '16 at 18:26
  • @Tiny If I create a new project, create the appropriate files (as listed in this thread) it will work yes. However, that code is exactly identical to the projects we open (the one we share around). The project includes nothing but these three files and the configuration files (beans.xml, glassfish-web.xml, and web.xml). When I create a new project and create my classes and files, glassfish-web.xml and web.xml are the only two configuration files listed. Even if I remove beans.xml from the shared project, we still get the same error. – MortenMoulder Jan 10 '16 at 18:29

1 Answers1

0

I found a fix for this. Apparently, for some of us.., the imported project name was not matching the <context-root> element's name. All we have to do, is go into WEB-INF and find glassfish-web.xml. In that file, check what is inside <context-root> and either replace that with your project name or simply rename your project to whatever is inside that element.

Took a few hours to figure that out, but hopefully others will find this and appreciate my answer.

MortenMoulder
  • 6,138
  • 11
  • 60
  • 116