struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tommy Pham <tommy...@yahoo.com>
Subject Re: Hibernate3+Spring2.5+Struts2
Date Wed, 19 Aug 2009 16:34:48 GMT





----- Original Message ----
> From: ZiedBejaoui <bejaouizied@hotmail.com>
> To: Struts Users Mailing List <user@struts.apache.org>
> Sent: Wednesday, August 19, 2009 9:23:35 AM
> Subject: Re: Hibernate3+Spring2.5+Struts2
> 
> Thanks for your reply however I didn't get you point, I have created a 
> userserviceImp instance in my loginaction.
> could you please tell what you mean?
> 
> >> >>
> >> >> public class LoginAction extends ActionSupport {
> >> >>
> >> >>    private UserServiceImp userService;
> >> >>    private String message;
> >> >>    private String username;
> >> >>    private String password;
> >> >>    private ResourceBundleMessageSource messageSource;
> >> >>
> >> >>    public LoginAction() {
> >> >>    }
> >> >>
> >> >>    @Override
> >> >>    public String execute() throws Exception {
> >> >>        User usr = userService.getUser(username);
> >> >>        if(usr != null){
> >> >>            if(usr.getPassword().equals(password)){
> >> >>                message = messageSource.getMessage("loginSuccess",
> >> > null,Locale.CHINA);
> >> >>            }else{
> >> >>                message = messageSource.getMessage("pswError",
> >> > null,Locale.CHINA);
> >> >>            }
> >> >>        }else{
> >> >>            message = messageSource.getMessage("usrError",
> >> > null,Locale.CHINA);
> >> >>        }
> >> >>        return SUCCESS;
> >> >>    }
> >> >>
> >> >>    public void setUserService(UserServiceImp userService) {
> >> >>        this.userService = userService;
> >> >>    }
> >> >>
> >> >>    public String getMessage() {
> >> >>        return message;
> >> >>    }
> >> >>
> >> >>    public void setMessage(String message) {
> >> >>        this.message = message;
> >> >>    }
> >> >>
> >> >>    public String getUsername() {
> >> >>        return username;
> >> >>    }
> >> >>
> >> >>    public void setUsername(String username) {
> >> >>        this.username = username;
> >> >>    }
> >> >>
> >> >>    public String getPassword() {
> >> >>        return password;
> >> >>    }
> >> >>
> >> >>    public void setPassword(String password) {
> >> >>        this.password = password;
> >> >>    }
> >> >>
> >> >>    public void setMessageSource(ResourceBundleMessageSource 
> >> >> messageSource)
> >> > {
> >> >>        this.messageSource = messageSource;
> >> >>    }
> >> >>
> >> >> }
> >> >>
> >
> > 
> >
> > Your UserServiceImp userService was instantiated but never initialized in 
> > your LoginAction class.  Hence the NPE.  When you to execute a method that 
> > does not a reference to real object, you'll get NPE.  You might want to 
> > review your java :)
> >
> > Best regards,
> > Tommy
> >

In many programming languages, all variables (fields) have to be initialized before use with
exception of primitive types (byte, short, etc - NOTE: String is null by default- they will
have a default value).  Your custom class UserServiceImp was instantiated with userService
but never initialized.  So when you try to do userService.getUser() in the execute() method
of your LoginAction, you'll get the NPE as seen. I suggest you spend some time and go through
this http://java.sun.com/docs/books/tutorial/ .  Your current problem is covered here: http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html

Best regards,
Tommy


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message