• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I NEED TO DISPLAY A RESULTSET IN A JSP CORE TAG

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
i have a project that i am working on which is based on struts and hibernate and i have created a bean class which i am using to retrieve some data and i want to display the value in the JSP but it is rfeturning a null pointer exception i guessed the error is from the bean...but cant figure it out any hel will be appreciated.
Here is my code

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't included the JSP and you haven't said where the NPE occurs--it's going to be tough to help!

If the NPE is occurring in the code above then it's not really a Struts issue, but JDBC, for which there is a forum.
 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the jsp page contains only the get and set property for the bean which and the null pointer exception occurs at the getproperty tag

<<jsp:useBean id="c" scope="session" class="org.accounts.savings.struts.action.Idan" />
<h2><jsp:getProperty name = "c" property = "amount"/></h2>
 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case what i am trying to achieve is not too clear what i want is to retrieve some data from database with resultset and display the content on JSP and i want to follow the structure of struts where jsp is only used for displaying
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the code that creates org.accounts.savings.struts.action.Idan and puts it in the HttpSession?
 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code tags are primarily for... code.

Normally the action class would be responsible for putting objects into scope for the JSP page.

Which version of Struts are you using? The canonical ways for doing what you're doing are different for Struts 1 and Struts 2.
 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you answer the questions I asked? (And really, save the 'code' tags for code.)

I wrote:Normally the action class would be responsible for putting objects into scope for the JSP page.

Which version of Struts are you using? The canonical ways for doing what you're doing are different for Struts 1 and Struts 2.

 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am using struts 2...thanks for the concern i need to get this working asap
thanks
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So... you're not actually trying to display a resultset in the JSP, rather the results of some calculations done on a resultset.

In Struts 2 data is usually exposed by creating an action property and displaying it via the <s:property> tag or JSP EL. Personally I'd just create an "amount" property and set it to the results of the calculations done in the bean.
 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi David,
i would really appreicate it if you would demonstrate it using a simple code to display the results of some calculations done on a resultset
thanks
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Action:

JSP:

Config:

That's about it.
 
Ahmed Oladele
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David,
the Config is it the struts config and FooAction class where do i place it is it under struts action.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really sure what you just said, but the configuration goes in the struts.xml configuration file inside a package definition. The class should be a fully-qualified Java class, wherever you implemented it.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem you have declared "int amount" as a local variable same variable you are returning .so that it returns like that.
instead of that declare "amount " variable starting of method ,assign to the variable and return the same variable amount
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What?
 
rk palleru
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.
2.
3. public int getAmount()
4. { int temp=0;
5. int test=this.getAccountId();
6. try
7. {
8. Class.forName("com.mysql.jdbc.Driver");
9. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/allovermfb","root", "");
10. PreparedStatement stmt= con.prepareStatement("SELECT AMOUNT FROM SAVINGS_ACTIVITY_DETAILS WHERE ACCOUNT_ID =? AND AMOUNT >200000 AND Flag =1 AND ACCOUNT_ACTION_ID =6 AND Treated =0 ORDER BY created_date DESC");
11. stmt.setInt(1, test);
12. ResultSet results = stmt.executeQuery();
13. while(results.next())
14. {
15. temp= results.getInt("AMOUNT");
16.
17. }
stmt.close();
18. }catch(Exception e)
19. {
20. System.out.println("listing erore" + e);
21. }
22. return temp;
23. }
24.
25. public void setAmount(int amount){
26. this.amount=amount;
27. }
28. }
 
rk palleru
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but one thing here you are overwriting database values to "temp " variable.so final value which is in the result object only returns .otherwise use some "if" condition in while loop
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@rk: Please use code tags when posting code.

The OP asked about getting data back to the JSP, which is what I addressed. Without knowing anything more about the contents of the database the logic in getAmount() doesn't affect that part of the question--it's up to the OP to determine if the logic being used to generate the data is correct, which isn't really related to Struts itself.
reply
    Bookmark Topic Watch Topic
  • New Topic