1

I tested this source code:

Bean:

private NewAccountObj na;

public class NewAccountObj {

    private int userid;
        ............

    public NewAccountObj(int userid.............) {

        this.userid = userid;
            ............
        }

        public int getUserid() {
            return userid;
        }

        ...............

    }

    // Getters 
    public NewAccountObj getDataList() {        
        return na;
    }

JSF Page:

<h:panelGrid columns="2">
    <h:panelGroup>User ID</h:panelGroup>
    <h:panelGroup>
        <h:inputText id="userid" value="#{bean.dataList['userid']}">                    
        </h:inputText>
    </h:panelGroup>
......................
</h:panelGrid> 

When I submit the form I get Target Unreachable, 'null' returned null. Can you help me to find the problem? Maye this is not the proper way to access Java object in h:panelGrid?

PS:

I get this error message in Glassfish log:

javax.el.PropertyNotFoundException: /NewAccount.xhtml @38,126 value="#{NewAccountController.dataList['userid']}": Target Unreachable, 'null' returned null
Wojciech Wirzbicki
  • 2,990
  • 4
  • 27
  • 47
  • The exception basically tells that `#{bean}` is `null`. Show how you configured it. – BalusC Dec 13 '12 at 12:10
  • I found the problem! I need to initialize the Java object and then to use it into the JSF page. I added this: `na = new NewAccountObj(0, 0, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null);` into the constructor. By the way this is very ugly solution. Is there any other elegant solution to initialize empty object in Java? –  Dec 13 '12 at 13:23
  • 2
    Erm, create a default c'tor? – BalusC Dec 13 '12 at 13:25
  • Something like this `NewAccountObj na = new NewAccountObj();`? Netbeans gives me error that I need to pass arguments. –  Dec 13 '12 at 13:27
  • As said.. create a default c'tor. – BalusC Dec 13 '12 at 13:28
  • 1
    @BalusC, how he can call bean getter method in inputText like this #{bean.dataList['userid']} ? – Narayan Subedi Dec 13 '12 at 16:06
  • you should set the userId to a property in managed bean #{bean.userId} – Swarne27 Dec 14 '12 at 03:26

2 Answers2

4

With the above code, the NewAccountObj is null. So when the getDataList() is called, it returns null. Then it will to call null.getUserId().

na needs to be initialized. I see in your comment that you have a very long constructor, you should create another one with no arguments (or the minimum required for the object to work).

private NewAccountObj na;

    public class NewAccountObj {

        private int userid;
        ............

        public NewAccountObj() {
            new New AccountObj(0,0,...........);
        }

        public NewAccountObj(int userid.............) {

            this.userid = userid;
            ............

        }

        public int getUserid() {
            return userid;
        }

        ...............

    }

    // Getters 
    public NewAccountObj getDataList() {        
        return na;
    }

And change the datalist getter like this:

public NewAccountObj getDataList() {
    if(na == null){
        na = new NewAccountObj();
    }
    return na;
}
grekier
  • 969
  • 6
  • 16
-1
ADD set property method

public int setUserid(int userid) {
           this.userid =userid ;
        }
Mohammod Hossain
  • 4,024
  • 2
  • 23
  • 34
  • 6
    A missing setter wouldn't result in this kind of exception. It's totally beyond me why this answer is upvoted. – BalusC Dec 13 '12 at 12:06