wood burning stoves 2.0*
The moose likes Servlets and the fly likes getting null values, while fetching data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting null values, while fetching data" Watch "getting null values, while fetching data" New topic
Author

getting null values, while fetching data

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i have a simple itembean class and using that i am inserting and fetching records from database, values are inserting into database, but when i am fetching values, it showing me null.
here is my code:






after inserting, it redirecting to success.jsp, and from success.jsp i am redirecting to ViewData.jsp and there i am print the values from getters.
do i need to also bring those values in success.jsp??
or what i am doing wrong??
i have two more class, one is simple html form and second is bean class, which has setters and getters.
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
A basic mistake you have done.
form component names must not be given with spaces. check your form jsp code..(item name) is wrong. You can have itemName or item_name like..please follow coding conventions.

You are inserting null values into database. so when you retrieve you get null values.

This is due to the naming.give itemName or item_name like...Do not give spaces kind..


Thanks
BVR


Thanks
Vipul Kumar
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
thank you for reply..
and it's not space, it's a underscore, i don't know why it's appearing here like space.
and in datatbase proper values are inserted the only problem is while fetching.
i also tried by setting and getting data to request but still it's not working.
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Punit Jain wrote:

after inserting, it redirecting to success.jsp, and from success.jsp i am redirecting to ViewData.jsp and there i am print the values from getters.
do i need to also bring those values in success.jsp??
or what i am doing wrong??
i have two more class, one is simple html form and second is bean class, which has setters and getters.




You created Item Bean object. i mean a new Item bean object which does have all default values.
You did not set any property of the Item bean object. so when retrieving you will get null values. as you did not set item bean object..

check in while loop. You are getting the property without being set..so first set the propety in the while loop.then call the getters..

Thanks
BVR.
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Punit, firstly I should mention that you should avoid scriptlets and not run SQL queries in a JSP page (the view and business logic/processing code/data access should be separated). However, in answer to your question as to why you are getting null values, I will expand on Vipul's answer. In the while loop that is testing if rs.next() is true, you should use this ResultSet to get the values from the database.

e.g. String query = "SELECT name,quantity,brand,price FROM items";
........................
while(rs.next())
{
out.println(rs.getString(1));//print name
out.println(rs.getInt(2)); //print quantity
out.println(rs.getString(3));//print brand
out.println(rs.getInt(4));//print price
}

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

You created Item Bean object. i mean a new Item bean object which does have all default values.
You did not set any property of the Item bean object. so when retrieving you will get null values. as you did not set item bean object..

check in while loop. You are getting the property without being set..so first set the propety in the while loop.then call the getters..


but how do i do this??
i mean how i pass all the field values from servlet (Connections.java) to my jsp page where i am displaying the values???
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
as you said Vanessa Danin

e.g. String query = "SELECT name,quantity,brand,price FROM items";
........................
while(rs.next())
{
out.println(rs.getString(1));//print name
out.println(rs.getInt(2)); //print quantity
out.println(rs.getString(3));//print brand
out.println(rs.getInt(4));//print price
}


but i want to get value from my getters, not directly from my database.
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
The process flow should be like this:
In a servlet or utility Java class, select all items from the database.
Set the properties of an Item javabean.
Add the individual Item javabeans to a collection (e.g. list of Items).
Set the collection (e.g. list) of Items as an attribute to a request (or a session if you need access to this data when the request is over).
Pass on the request from the servlet to the relevant jsp page.
In your jsp get the request (or session) attribute and loop through the collection (e.g. using the JSTL forEach tag) and use EL to access the Item's getter methods.
Does this point you in the right direction Punit?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
thank you for your good explanation, but you mean that i need to fetch the data from database, i can use getters directly (if i want fetch only the lat values which i entered)?
and i fetch data from database than i can directly display that why to these all??

Set the properties of an Item javabean.
Add the individual Item javabeans to a collection (e.g. list of Items).
Set the collection (e.g. list) of Items as an attribute to a request (or a session if you need access to this data when the request is over).
Pass on the request from the servlet to the relevant jsp page.
In your jsp get the request (or session) attribute and loop through the collection (e.g. using the JSTL forEach tag) and use EL to access the Item's getter methods.


Thank you Vanessa Danin.
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Punit, I am not at all sure if I understand what you are asking.

I think you are saying that you only want to display the values of a single Item (the Item that has just been inserted into your database and not all the items in your Item table). Is that correct? I originally thought you wanted to display all the Items in your Item table since your sql query was "SELECT * FROM items;" (without any limiting clause such as a WHERE clause).

If you only want to display the values of the Item that has just been set by the user, then the process flow would be:
Set the Item object's properties (e.g. name, quantity, brand and price) and insert it into the database. (you are already doing this)
Set this Item object as an attribute to the request (or a session) (e.g. request.setAttribute("item",ii);)
Forward the request from the servlet to the relevant jsp page.
In your jsp, using JSTL/EL get the request (or session) attribute (i.e. the Item object) and then get that Item's properties using the Item's getter methods (e.g. <c:out value="${requestScope.item.itemName}"/> )

Just as an aside, I am not sure why you have called the properties in the Item class itemName, itemQuantity etc instead of simply using name, quantity etc.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

I think you are saying that you only want to display the values of a single Item (the Item that has just been inserted into your database and not all the items in your Item table). Is that correct?

yes i m saying the same thing.

Set this Item object as an attribute to the request (or a session) (e.g. request.setAttribute("item",ii);)

i tried it but it's not worked for me. i tried like this:

and i m getting the values using request.getAttributed().
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Punit Jain wrote:


I didn't read all the previous posts, but why are you creating an object with various attributes, if you then proceed to put all the attributes individually as attributes? Why not put ii as attribute, and then use EL to access the attributes in the JSP?
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Tim, that is exactly what I recommended to Punit (as per my posts above). Punit, as I said previously (which is what Tim is also saying now), you must set the Item object as the attribute i.e. request.setAttribute("item",ii). Also, as I showed you previously, you should be using JSTL/EL and not java code like request.getAttribute("item") in your jsp to get the attribute.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Ah, I see. Am I glad I mentioned that I didn't read the previous replies :-)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
thanks for replies...
as you said,
for setting my attributes i used

and for getting values, i used like this, as you said, (sorry but, i don't know how to use EL)

but using this i am getting exception,

The function getItemName must be used with a prefix when a default namespace is not specified


and i also tried using scriptlets, like this:


but by using this i am getting null value.
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Since you are not familiar with JSTL/EL I suggest that you work through a tutorial before trying to use it. (Please google "JSTL tutorial" and pick a tutorial that you find easiest to learn from). The exception referring to the namespace is occuring because you are not using a JSTL taglib directive at the top of your jsp page e.g. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> .
Please see: http://www.coderanch.com/how-to/java/SetupJstlForJsp2 for what is required to be able to use JSTL in your application.

The reason your scriptlet didn't work is because the request attribute is an Item object (not an Item object's name, quantity, brand, price or any other item object property) and the request attribute was set with the attribute name "item" not "item.getItemName()" .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting null values, while fetching data