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

The simple answer here is to do some debugging by putting some debug code into your class.

Start by setting a starting value to rating such that you have,
String rating = "Rating Not Null";

Then test, see if rating comes out null, or does rating still contain the initial value....

If it contains the initial value I have to say, that none of the "if" is matching... SO, have the getRating function return a string that outputs all your internal variables' values to see what is missing and what is NOT what you expect it to be...

i.e.
public String getRating(){
...

//return rating
return "Gender = " + gender + "\r\nAge = " + age + "\r\nAmount = " + amount + "\r\n" + "rating = " + rating + "\r\nDONE."
}

This way you see everything that your getRating method can see and we don't worry about using high performance string buffers or string builders since we are just testing... do it the quick and dirty way...

Then you can see what is going on...

Also, I recommend you explicitly declare the variables as private
private String gender;

Now, what version of Java are you using?
Perhaps you should use
if(gender.equalsIgnoreCase("male")){}
else if (gender.equalsIgnoreCase("female")){}
//add a catch all
else {
this.rating = "gender not matched";
}

see in your code above you have the wrong clause in the IF statements...

You ask for the String value of a String... redundant unless you wish to filter for potential NULL Strings...

BUT that aside, String == String does NOT work in Java,... Java String are Objects, and the == sign compares the addresses of those objects... EVEN if the object contains the same data, if the address of the object is NOT the same, then the Strings won't be equal...

When you have gender = "male"; then later you have if gender == "male" Java will make a NEW String object with the contents of "male"... it does NOT reuse the old one, because it has no way of knowing if it is safe to reuse an object in memory with same value... the values may change independently and it doesn't know if that is OK or not, so it acts like Nancy and "just say no!"...

So you need to do a different comparison which uses the String comparison methods, such as the one I have suggested... then
gender.equalsIgnoreCase("male") will yield a boolean as you expect...

You should still watch for values other than the expected, so add the catch all to any if/else if statements you have making them if/else if/ else

even if it doesn't makes sense that other values may be there, an nice error message is a quick way to discover where the trouble lays....

Peace,

Hi,
I am sorry,as I didn't read u r complete code.Why don't u try this,instead of the lengthy code???
String str=beanid.function();
Remember,,,I have given String here,but the datatype of the function and the variable should match.
Suppose if u return some string
String function wxyz()
{
String msg="hello";
return msg;
}
Then in u r JSP, u should write
String str=beanid.wxyz();
So u r str will not b null.I shall see u r program in detail next time and give u the most appropriate answer.Right now,check if this works???Message me if u have any doubts....
all the best,,,bye...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.