aspose file tools*
The moose likes Struts and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Null Pointer Exception" Watch "Null Pointer Exception" New topic
Author

Null Pointer Exception

Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Hi buddies!

At first of all, sorry for my english, I'm not english speaker but I have problems using Struts and I would like who someone help me to solve it!

Basically I'm doing a very simple web application using Spring, Hibernate and Struts. I think that Spring + Hibernate are working nice because I have done JUNIT test and they are working ok but I don't know how to configure struts2 and struts.xml

First of all... when I invoke an action by a JSP file I get this error and I don't know if the problem is in the struts.xml, jsp file or another place...

The error is the following:



and here are the struts file configuration and my jsp:

struts.xml



lista.jsp



Note: I call to lista.jsp by this way:

http://localhost:8080/SpringHibernateStruts2/jsp/alumno/lista.action

Greetings from Spain!


I edit my post.

I have been thinking about the problem and I think that the problem is in JSP file (lista.jsp) but I don't know how I can do a JSP file where it shows me a list of students that I have saved into the database before!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

Your English is better than my Spanish

Samuel Castillo Romero wrote:



This tells us the exception happened at line 38 in the class AlumnoAction, not in your JSP. You didn't share that code with us, so I can't help you more.

Samuel Castillo Romero wrote:
I have been thinking about the problem and I think that the problem is in JSP file (lista.jsp) but I don't know how I can do a JSP file where it shows me a list of students that I have saved into the database before!


The way you have your struts.xml set up, the method lista is invoked on the action AlumnoAction before the JSP is displayed. In the lista method, you would retrieve the list from the database and put it somewhere that is visible to the JSP. In your JSP, you have specified the iterator to look for a value named alumnos, so should be a getAlumnos method in your Action.

There are many tutorials on the web. Start with the examples in the Struts documentation. A little reading early will save you many headaches later!


[How To Ask Questions On JavaRanch]
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Ops, I forgot attach to you the action class

Here we go!




As you can see, there is a method called "lista" and there is where is the error, particularly, this is the error: alumnos = alumnoDao.lista();

The question is that I'm not really sure that this is the error because I have some JUNIT test wich use this function and it works nice.

Thank you for your help. I'm trying to fix this error since two weeks and I am so overwhelmed

Greetings!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

Samuel Castillo Romero wrote:
As you can see, there is a method called "lista" and there is where is the error, particularly, this is the error: alumnos = alumnoDao.lista();
The question is that I'm not really sure that this is the error because I have some JUNIT test wich use this function and it works nice.


It looks like alumnoDao is not getting initialised. I take it initialisation delegated to Spring?
When you test with JUnit, is the application deployed on the server? Are you testing the Action or just the Spring and Hibernate component?

Samuel Castillo Romero wrote:
Thank you for your help. I'm trying to fix this error since two weeks and I am so overwhelmed


Let's see how much help I can offer. I've never used Struts2 and Spring together
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Joe is correct ... In reality all you should need to do is just



unless like he asked if your delegating to Spring to initialize.
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Lucas Mireles wrote:Joe is correct ... In reality all you should need to do is just



unless like he asked if your delegating to Spring to initialize.


Really I'm new using this technologies and I'm not sure if I am delegating the initialization of the variable or not. How can I know it?

If you don't mind I send you the code of AlumnoDao and AlumnoDaoHibernate, maybe it could be useful.






Thank you all! :)
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Your probably not .... You wouldve had to configure it and the code you just posted is a Data access object not the action controller.

Just initialize the array and you'll be ok
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
[quote=Lucas Mireles]Your probably not .... You wouldve had to configure it and the code you just posted is a Data access object not the action controller.

Just initialize the array and you'll be ok[/quote]

I have already initialized the array but I get the same error.

I put this one:

private List<Alumno> alumnos = new ArrayList<Alumno>();

Any idea?

Sorry for being so annoying :(
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Its probably because how your accessing the list.

In your JSP you say value is alumnos ... but you dont have a getter for alumnos you have a getter for lista.

So either change your jsp or change the action and provide a getAlumnos

-Lucas
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Lucas Mireles wrote:Its probably because how your accessing the list.

In your JSP you say value is alumnos ... but you dont have a getter for alumnos you have a getter for lista.

So either change your jsp or change the action and provide a getAlumnos

-Lucas


I think that it's okay. I'm going to try to explain it... (my english is not good...hehehe)


In struts.xml I have this line:

<action name="lista" class="escuela.web.AlumnoAction" method="lista">

That means that I have an action name called "lista" in class AlumnoAction and when this class is executed, I execute lista() method.



On the other hand I have:

<s:iterator value="alumnos" status="status"> in my JSP file so I need a getter method in AlumnoAction.



This getter method is this one:



So, when I call to lista(), alumnos variable is loaded with all the students and later, I get the information by getAlumnos().

Then.... Where is the problem? Why it does not work?


Samuel
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

Lucas Mireles wrote:
In your JSP you say value is alumnos ... but you dont have a getter for alumnos you have a getter for lista.


I think you are looking at his DAO class, which has a getter for lista. His action class does have a getAlumnos method.
The original error was caused by the alumnoDao instance on line 38 of AlumnoAction being null. Initialising alumnos does not fix this problem.

@Samuel: Since you are new at this, how about learning one technology at a time? Start with a basic Struts (or Spring or Hibernate) app, get proficient with it, then add another piece to the puzzle. If you expect to throw everything together without understanding how each piece works, you are in for a long, painful process.
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Joe Ess wrote:
Lucas Mireles wrote:
In your JSP you say value is alumnos ... but you dont have a getter for alumnos you have a getter for lista.


I think you are looking at his DAO class, which has a getter for lista. His action class does have a getAlumnos method.
The original error was caused by the alumnoDao instance on line 38 of AlumnoAction being null. Initialising alumnos does not fix this problem.

@Samuel: Since you are new at this, how about learning one technology at a time? Start with a basic Struts (or Spring or Hibernate) app, get proficient with it, then add another piece to the puzzle. If you expect to throw everything together without understanding how each piece works, you are in for a long, painful process.



Hi Joe Ess, I understand more or less how works Hibernate + Spring in a simple way. I have done very simple classes and I check that they work nice using test. So I would like only create a very simple JSP page showing all the students. But I think that the error is in struts and not in Hibernate - Spring.

After have done this example I would like to learn more about Spring or Hibernate but I think that it should not be very difficult start doing an aplication like this one.

Sorry for the inconveniences.
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

Your dao is null.

alumnoDao.lista()

So you probably need to initialize it
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Lucas Mireles wrote:Your dao is null.

alumnoDao.lista()

So you probably need to initialize it

in this line: alumnos = alumnaDo.lista();
I have initialized the variable called alumnos, using an ArrayList<Alumno> but I have the same error.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

I will say it again (I think this is #3): alumnoDao is null. You need to initialise it.
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Joe Ess wrote:I will say it again (I think this is #3): alumnoDao is null. You need to initialise it.



I'm stupid but how can I initialise it?

I have AlumnoDao alumnoDao but AlumnoDao is an interface. I don't know how initialise it...

I have put this new line to initialise it (private AlumnoDao alumnoDao = new AlumnoDaoHibernate();) but it does not work, I have this error:



Line number 39 is this one:

return hibernateTemplate.loadAll(Alumno.class);



Sorry for the inconveniences again...
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

I have put this new line to initialise it (private AlumnoDao alumnoDao = new AlumnoDaoHibernate();) but it does not work, I have the same error.


That should fix the problem I'm thinking of. Are you sure it is the same error? Check the stack trace.
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Joe Ess wrote:
I have put this new line to initialise it (private AlumnoDao alumnoDao = new AlumnoDaoHibernate();) but it does not work, I have the same error.


That should fix the problem I'm thinking of. Are you sure it is the same error? Check the stack trace.



Sorry I was wrong. The new error is in AlumnoDaoHibernate class and the line of the error is:

return hibernateTemplate.loadAll(Alumno.class);

Maybe the problem is in the entity?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

Do you have much experience with Java?
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Joe Ess wrote:Do you have much experience with Java?



emmmm I am studying at university. Anyway, I understand that you prefer not help me because I don't know much about java, is normal.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8968
    
    9

It's not that I don't want to help you. If you don't know Java well, trying to use 3 frameworks that extend Java in three different ways is going to be very difficult.
Again, my advice is to start small and build upon a strong foundation.
Jesus Mireles
Ranch Hand

Joined: Mar 10, 2010
Posts: 122

I think your struts question is now answered ... it looks like you need help with Hibernate and how to use Hibernate ... I agree with Joe, its not that we dont want to help, but at this point your asking us to do your work and not much of questions now ... I think you need to do more search on hooking up with Hibernate and Spring ...

-Lucas
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Sorry I didn't express myself much well.. I know you want to help me and not do my work!! it's obvious! I thank you for your help, really!


Anyway I think that the problem is with hibernate because if I create manually an Alumno object and I return a list with this object, I can see a JSP web with the student so I'm going to work in this way ;)

Thank you for all. I will follow your advice!
Greetings!
 
Consider Paul's rocket mass heater.
 
subject: Null Pointer Exception