Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException on getGreeting()  RSS feed

 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the Core JSF book, Chapter 1, I'm getting a strange NullPointerException on the getGreeting() logic in the UserBean class.

The issue seems to be where the book tests for the user.length() == 0. When I comment out the IF statement, then the simple Ajax example works. With the if statement, the NullPointerException. It seems that the name property is being evaluated even before I can enter the name (and password) into the form.

Here's the index.html code:



And, here's the UserBean.java


Just wondering what might be wrong.

Suggestions?

Thanks,

mike
 
Akaine Harga
Ranch Hand
Posts: 99
Java MyEclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is simple. When you bind a property to view its getter is invoked every time you load the view. In this case JSF sees greeting as a property of the user object, so it invokes the method as soon as you load the view. And since name is null at this point you get the nullpointer exception. Recommended approach: in the getGreeting() method check name for null before checking its length. Alternative approach: initialize name with an empty string.
 
Mike London
Ranch Hand
Posts: 1476
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akaine Harga wrote:The answer is simple. When you bind a property to view its getter is invoked every time you load the view. In this case JSF sees greeting as a property of the user object, so it invokes the method as soon as you load the view. And since name is null at this point you get the nullpointer exception. Recommended approach: in the getGreeting() method check name for null before checking its length. Alternative approach: initialize name with an empty string.


Appreciate your reply That's what I ended up doing until I went back to the code and saw that the UserBean definition changed between the first and second version in chapter 1. In the second version, for the Ajax example, they added the = "" to the variable definition.

private String name = ""

I missed that.

Thanks again!

-mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!