Hi there,
I have a class that returns a value based on a calculation with in the class
2 of the variables are called from a database, which is working however when I try to output the result on a jsp page ie. calling the property 'rating' I am told it is "null" on the page....
was wondering if sombody could review my code and make any suggestions as to where my problem lies .....
The variable amount is inputted by the user...
This is the class for calculating:
/* * Pressup.java * * Created on 29 April 2007, 13:03 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** * * @author Hegstatic *
/[B]package[/B] fitness;
[B]import[/B] java.sql.*;
[B]import[/B] java.text.*;
[B]public[/B] [B]class[/B] Pressup {
/** Creates a new instance of Pressup */
[B]int[/B] age;
[B]int[/B] amount;
String rating;
String gender;
Connection con;
String error;
[B]public[/B] [B]int[/B] getAge(String userName) [B]throws[/B] SQLException,
Exception{
[B]if[/B](con != [B]null[/B]){
[B]try[/B]{
ResultSet rs;
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT age FROM gymuser WHERE Username= 'userName' ");
[B]while[/B] (rs.next()) {
age = rs.getInt("age");
} [B]return[/B] age;
} [B]catch[/B](SQLException sqle){
error = "SQLException: could not search gymuser"; [B]throw[/B] [B]new[/B] Exception(error, sqle);
}
[B]catch[/B](Exception e){
error = "An exception occured while searching gymuser"; [B]throw[/B] [B]new[/B] Exception(error);
}
} [B]else[/B]
{ error = "Exception: connection to database was lost"; [B]throw[/B] [B]new[/B] Exception(error);}
}
[B]public[/B] [B]void[/B] setAge([B]int[/B] age)
{ this.age = age;
System.out.println(age);
}
[B]public[/B] String getGender(String userName) [B]throws[/B] SQLException, Exception{
[B]if[/B](con != [B]null[/B]){
[B]try[/B]{
ResultSet rs;
Statement stmt = con.createStatement();
rs = stmt.executeQuery("SELECT gender FROM gymuser WHERE Username= 'userName' ");
[B]while[/B] (rs.next()) {
gender = rs.getString("gender");
}
[B]return[/B] gender;
} [B]catch[/B](SQLException sqle){
error = "SQLException: could not search gymuser";
[B]throw[/B] [B]new[/B] Exception(error, sqle); } [B]catch[/B](Exception e){
error = "An exception occured while searching gymuser"; [B]throw[/B] [B]new[/B] Exception(error); }
} [B]else[/B] {
error = "Exception: connection to database was lost"; [B]throw[/B] [B]new[/B] Exception(error);} }
[B]public[/B] [B]void[/B] setGender(String gender){
this.gender = gender; }
[B]public[/B] [B]int[/B] getAmount(){
[B]return[/B] amount;}
[B]public[/B] [B]void[/B] setAmount([B]int[/B] amount)
{
this.amount = amount; }
[B]public[/B] String getRating()
{ [B]if[/B] (String.valueOf(gender) == "male")
{ [B]if[/B] (age>= 0 && age <= 29 && amount >=55 ) { rating = "Excellent"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <= 29 && amount >=45 && amount <=54 ) {
rating = "Good"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <= 29 && amount >=35 && amount <=44 ) { rating = "Average"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <= 29 && amount >=20 && amount <=34 ) { rating = "Fair"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <= 29 && amount >=0 && amount <=19 ) { rating = "Poor"; }
[B]else[/B] [B]if[/B](age>= 30 && age <=39 && amount >=45) { rating = "Excellent"; }
[B]else[/B] [B]if[/B](age>= 30 && age <=39 && amount >=35 && amount <=44) { rating = "Good"; }
[B]else[/B] [B]if[/B](age>= 30 && age <=39 && amount >=25 && amount <=34) { rating = "Average"; }
[B]else[/B] [B]if[/B](age>= 30 && age <=39 && amount >=15 && amount <=24) { rating = "Fair"; }
[B]else[/B] [B]if[/B](age>= 30 && age <=39 && amount >=0 && amount <=14) { rating = "Poor"; }
[B]else[/B] [B]if[/B](age>= 40 && age >=49 && amount >=40) { rating = "Excellent"; }
[B]else[/B] [B]if[/B](age>= 40 && age <=49 && amount >=30 && amount <=39) { rating = "Good"; }
[B]else[/B] [B]if[/B](age>= 40 && age <=49 && amount >=20 && amount <=29) { rating = "Average"; }
[B]else[/B] [B]if[/B](age>= 40 && age <=49 && amount >=12 && amount <=19) { rating = "Fair"; }
[B]else[/B] [B]if[/B](age>= 40 && age <=49 && amount >=0 && amount <=11) { rating = "Poor"; }
[B]else[/B] [B]if[/B](age>= 50 && age <=59 && amount >=35) { rating = "Excellent"; }
[B]else[/B] [B]if[/B](age>= 50 && age <=59 && amount >=25 && amount <=34) { rating = "Excellent"; }
[B]else[/B] [B]if[/B](age>= 50 && age <=59 && amount >=15 && amount <=24) { rating = "Average"; } [B] else[/B] [B]if[/B](age>= 50 && age <=59 && amount >=8 && amount <=14) { rating = "Fair"; }
[B]else[/B] [B]if[/B](age>= 50 && age <=59 && amount >=0 && amount <=7) { rating = "Poor"; }
[B]else[/B] [B]if[/B](age>= 60 && age <=79 && amount >=30) { rating = "Excellent"; }
[B]else[/B] [B]if[/B](age>= 60 && age <=79 && amount >=20 && amount <=29) { rating = "Good"; }
[B]else[/B] [B]if[/B](age>= 60 && age <=79 && amount >=10 && amount <=19) { rating = "Average"; }
[B]else[/B] [B]if[/B](age>= 60 && age <=79 && amount >=5 && amount <=9) { rating = "Fair"; }
[B]else[/B] [B]if[/B](age>= 60 && age <=79 && amount >=0 && amount <=4) { rating = "Poor"; } }
[B]else[/B] [B]if[/B] (String.valueOf(gender) == "female") { [B]if[/B] (age>= 0 && age <=29 && amount >=49) { rating = "Excellent"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <=29 && amount >=34 && amount <=48) { rating = "Good"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <=29 && amount >=17 && amount <=33) { rating = "Average"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <=29 && amount >=6 && amount <=16) { rating = "Fair"; }
[B]else[/B] [B]if[/B] (age>= 0 && age <=29 && amount >=0 && amount <=5) { rating = "Poor"; }
[B]else[/B] [B]if[/B] (age>= 30 && age <=39 && amount >=40) { rating = "Excellent"; }
[B]else[/B] [B]if[/B] (age>= 30 && age <=39 && amount >=25 && amount <=35) { rating = "Good"; }
[B]else[/B] [B]if[/B] (age>= 30 && age <=39 && amount >=12 && amount <=24) { rating = "Average"; }
[B]else[/B] [B]if[/B] (age>= 30 && age <=39 && amount >=4 && amount <=11) { rating = "Fair"; }
[B]else[/B] [B]if[/B] (age>= 30 && age <=39 && amount >=0 && amount <=3) { rating = "Poor"; }
[B]else[/B] [B]if[/B] (age>= 40 && age <=49 && amount >=35) { rating = "Excellent"; }
[B]else[/B] [B]if[/B] (age>= 40 && age <=49 && amount >=20 && amount <=34) { rating = "Good"; }
[B]else[/B] [B]if[/B] (age>= 40 && age <=49 && amount >=8 && amount <=19) { rating = "Average"; }
[B]else[/B] [B]if[/B] (age>= 40 && age <=49 && amount >=3 && amount <=7) { rating = "Fair"; }
[B]else[/B] [B]if[/B] (age>= 40 && age <=49 && amount >=0 && amount <=2) { rating = "Poor"; }
[B]else[/B] [B]if[/B] (age>= 50 && age <=59 && amount >=30) { rating = "Excellent"; }
[B]else[/B] [B]if[/B] (age>= 50 && age <=59 && amount >=15 && amount <=29) { rating = "Good"; }
[B]else[/B] [B]if[/B] (age>= 50 && age <=59 && amount >=6 && amount <=14) { rating = "Average"; }
[B]else[/B] [B]if[/B] (age>= 50 && age <=59 && amount >=2 && amount <=5) { rating = "Fair"; }
[B]else[/B] [B]if[/B] (age>= 50 && age <=59 && amount >=0 && amount <=1) { rating = "Poor"; }
[B]else[/B] [B]if[/B] (age>= 60 && age <=69 && amount >=30) { rating = "Excellent"; }
[B]else[/B] [B]if[/B] (age>= 60 && age <=69 && amount >=20 && amount <=29) { rating = "Good"; }
[B]else[/B] [B]if[/B] (age>= 60 && age <=69 && amount >=11 && amount <=19) { rating = "Average"; }
[B]else[/B] [B]if[/B] (age>= 60 && age <=69 && amount >=5 && amount <=10) { rating = "Fair"; }
[B]else[/B] [B]if[/B] (age>= 60 && age <=69 && amount >=0 && amount <=4) { rating = "Poor"; }
}
[B]return[/B] rating;
}
[B]public[/B] [B]void[/B] setRating(String rating) {
this.rating = rating; } }
This is the jsp page im calling the value from :
<!--Here the user calls either the newEmployer or newStudent methhods from the DBQuery, depending on the user type-->[EMAIL="%@page contentType="]%@page contentType="text/html"%[/EMAIL]
<%@page import="java.sql.*, fitness.*"%>
<jsp:useBean id="pressup" scope= "session" class="fitness.Pressup" />
<jsp:useBean id="currentuser" scope="session" class="fitness.currentuserBean" />
<jsp:setProperty name="pressup" property="amount" param="amount" />
<html>
<head>
<title>Press up</title>
</head>
<body>
Your Pressup rating is<jsp:getProperty name="pressup" property="rating" />
<form action="Pressupupdate.jsp" method = post /> <input type="submit" value="Save and Move on" /> </form> </body> </html>
The comes up reading "Your Pressup rating is null"
Again any help would be greatly appreciated....
Thanks,
Heg