*
The moose likes JSF and the fly likes newbie null reference exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "newbie null reference exception" Watch "newbie null reference exception" New topic
Author

newbie null reference exception

ian mcdavid
Greenhorn

Joined: Aug 24, 2010
Posts: 5
faces configuration:

web.xml file:

Login.jsp/jsf whatever:

Login class definition:

error:

javax.faces.FacesException: #{Login.ValidateLogin}: java.lang.NullPointerException

I am a Java web development scrub / newbie. I am assuming this is because i did not have an instance of the Login class. However, tutorials that i have seen have done similar tasks without creating an instance (and they are not static classes) so i am confused. Please help me understand why i am getting this error and how to make this simple web application work. Thanks in advance.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Welcome to the JavaRanch, Ian!

We have a "code" button in the message editor. I've used it to make your samples more readable.

Standard disclaimer: I don't care how many code examples start off with a login example, user-designed security systems are a menace to the Internet.

Standards notes: You shouldn't start package names with a capital letter. That's against the language conventions, even though it's technically legal. IDEs will yell at you. More specifically, class names should start with a capital letter. Most other things shouldn't. It provides a visual distinction between what is a class and what is an object, method or property. It also works better with tools that expect you to be conforming to standards. This also applies to bean names, by the way.

You did not initialize the username and password properties and you did not mark their corresponding View components with the "required" attribute. That means that it's possible to submit a form with null username and/or password and that will throw a NullPointerException.


Customer surveys are for companies who didn't pay proper attention to begin with.
ian mcdavid
Greenhorn

Joined: Aug 24, 2010
Posts: 5
Thanks for the help so far. Another problem with this is that the fields are never getting set through the form. The navigation works and the validation function gets called, but the username and password fields are never getting set. Something behavior that deviates from the tutorials that i have seen is that the textInput components literally have the string #{Login.username} and #{Login.password} in them. Any ideas on how to make this work properly?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

"#{Login.username}" isn't really a String, it's an EL Expression. That means that the framework is going to compile that expression to produce get/set property calls.

I don't see anything wrong with your field definitions, even though "Login" as a bean name is (as mentioned) not recommended capitalization (and interferes witjh your ability to use annotations in JSF2).

It's always a good idea to put a <h:messages/> element on the page, though. If you're having a problem that results in an error message, that element will display the message(s).
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

Sorry. I misread. Again.

If your EL is being displayed instead of compiled, one of 2 things is probably to blame.

1. You're referencing the page via its resource path (.jsp) instead of its URL (.jsf). If you don't use the .jsf extension, then the request won't be processed by the FacesServlet and none of the JSF functions will work right.

2. You're missing TLD info that defines the "h" (and maybe "f") tag namespaces. I normally use xhtml (Facelets) with xml fomat, so I have supply the namespace info as part of my XML. If you're using JSP resources, check the book. There should be some additional text that goes above what you've shown, although for brevity's sake, they may omit it in the examples after the first 1 or 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newbie null reference exception