• 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

Can't perform logic on my bean class getter methods

 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to create an SQL query string based on which fields they entered information into on the form. The form is a pretty generic search form with 5 fields that they can use for searching. This example is very simple because if I can get this working I'll be able to get the whole thing going. Here is my class...



As you can see I create an instance of my bean and save the form values to it. If I do a getServletContext().getRequestDispatcher("/results.jsp").forward(request,response); outside of the 'if' logic it forwards across and displays all non null values properly. But when I try to check for null or for "" it won't hop into the logic.

If a bean class variable has no value how do I check that?

[ November 27, 2007: Message edited by: Bryce Martin ]
[ November 27, 2007: Message edited by: Bear Bibeault ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bryce Martin:
If a bean class variable has no value how do I check that?


If you haven't assigned a value to an object reference it will default to null. Primitives will default to their own default values.

P.S. In your doPost, what happens if the if condition fails?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had it just redirect anyway. That is how I tested that the if wasn't working. I started with just the if, then when I got a blank page I put the else to make it redirect anyway and it worked and I saw values. Then I took it out for this example because it didn't look logical....I wanted to keep confusion to a minimum.

I've tested this with the null and its not working. If I redirect anyway it shows the right values for the bean variables. So I'm not sure what is going on.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here's what new with this problem. I simplified my 'if' statement to look at just purchaseOrder.



If I don't put any value into the purchaseOrder field it still sees it as not null, but when I display it with EL it doesn't print anything. So there is no value for the variable in the bean, but its not null and its not "" or " ". I have no idea how to test this...and I NEED to.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the bean class look like?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Very straight forward.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If setPurchaseOrder is never called, the value of the property will be null.

Using the output of a JSP as a debugging mechanism isn't the best course of action. Do you not have any type of logging enabled for your application?
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setPurchaseOrder is called in my doPost.

But the value of the getParameter("purchase_order") should be null since I didn't enter anything into the field. Wouldn't it then put null into the bean? And if not, what does it put in there? There doesn't seem to be anything in it but none of the tests that I know of to check if its null, or blank seem to work.

Should I just reference the request parameter without putting it in the bean?

No, I don't have any logging in my program. How would I turn that on and where would the results be?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bryce Martin:
But the value of the getParameter("purchase_order") should be null since I didn't enter anything into the field.


So sure about that, are you?

none of the tests that I know of to check if its null, or blank seem to work.


How are you testing for the empty string?

No, I don't have any logging in my program. How would I turn that on and where would the results be?


Most web developers like Log4J.
[ November 27, 2007: Message edited by: Bear Bibeault ]
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I'm sure. Because if I do enter something in the field the doPost method sees it and the value will be printed when I call it using the EL from my jsp. When I enter values in the fields it sees them and prints them. When i don't, its sees nothing and prints nothing.

But it tests to != null is true all the time.


I've tried these 3 different tests. None of them detect anything null or empty.





None of these statements will forward over.

I'll have to look into the logging stuff.
[ November 27, 2007: Message edited by: Bryce Martin ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put on your Java thinking cap and think long and hard about what this statement is really doing:
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear, I understand that statement shouldn't do anything. I know that an empty string would be a null value. But I was just trying to cover all my bases, i'm in desperation mode with this problem. This is something that I need to figure out before I can continue with this project.

The question has become this.... Why does the line of code


not meet the criteria of myBeanClass.getPurchaseOrder() == null when the parameter is null? And, if for some reason I have a version of HTML that no one else has and forms post some default majik value that is blank but not an actual blank or null, why would the form not post a null value when the field is empty?

Nothing adds up.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An empty String and a null are not the same thing.

In Java, null means that the variable doesn't point to anything; there is no object. An empty String IS an object so it will not be null.
If you haven't already, grab any Java 101 book and look this up.
It is usually covered near the beginning of the book (any book that teaches the fundamentals of Java). This section will also cover the difference between String.equals() and '==' and why the == symbol is not appropriate for comparing string contents.

Once you have a firm grip on that you'll want to take note that getParameter will return null if the named parameter doesn't exist in the request. If it does exist but it is empty (the user didn't enter anything) then an empty string will be returned so a test for null will always return false.

Any easy way to test for an empty field in a servlet environment is:


Or if you want to test for a null string or an empty string:

[ November 27, 2007: Message edited by: Ben Souther ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bryce Martin:
Bear, I understand that statement shouldn't do anything.

No, you are missing the point. How do you check for equality, rather than identify, in Java? Is it with the == operator?

I know that an empty string would be a null value

Incorrect. null and the empty string are not even remotely alike.

The question has become this.... Why does the line of code

not meet the criteria of myBeanClass.getPurchaseOrder() == null when the parameter is null?

Because the parameter is not null. It will be the empty string if you leave a field blank (but not disabled).

why would the form not post a null value when the field is empty?

null is a Java concept, not an HTTP one. The parameter will either not exist or it will be the empty string. There will never be a parameter whose value is null.

Nothing adds up.

This is because you are missing a basic Java point: How do you test for the empty string? You have yet to demonstrate that you know how to do this.
[ November 27, 2007: Message edited by: Bear Bibeault ]
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The adding up part just happened. I forgot about the .equals bit. I used that in my rendition of this project and totally forgot about it. Like I said, I'm new to the java game and forgot (coming from a C++ background) that == isn't liked when comparing strings in java. In all my reorienting with OOP i had null comparisons on the brain. Sorry for all the friggin fuss. rookie mistake. All is well, and hopefully i'll be able to finish this project without too many more problems.

Thanks for all the help.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I bet it's not something you'll forget again, is it? Makes it all worthwhile.
 
Bryce Martin
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, I surely won't forget that again. Once again, thank you. The project is moving along nicely now.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic