• Post Reply Bookmark Topic Watch Topic
  • New Topic

NPE on Load for a search box

 
Justin Muir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a JSP that has just a search box on it and should get results from MySQL Database (So not sure if this should be here, but i think)
The problem is that when it loads for the 1st time obviously it passes the queryText to the java class as a null value then give NullPointerException.
My code in the class for getting the queryText is

But i had the page open from an earlier run and i entered text to search on, and it DID display from the database...
I then have a CreateQuery() method.
The 1st line is
And here it gives a NPE if i leave out the check above to see if
So i don't know how to split the words, but also if i check if the field is blank it gives a NPE on startup??
By the way i send it to the class from the jsp like this: search.setQueryText(queryText);

Hope i've made sense!!

And please any help Thanks!
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Muir wrote:Hi

And here it gives a NPE if i leave out the check above to see if


Hi Justin,

Obviously, it will gives you NPE.. Because, calling length() on NULL Object gives you NPE.

Just check it with the NULL i.e. (if(queryText != null )) and then do the further processing with the queryText.

Rahul
 
Justin Muir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that loads without an error,

But now i think what is happening is that it is searching for EVERYTHING in the database i think because it searches for " ".

Please help me to just bring up the jsp and then only search when they enter text. Not when it just loads that it searches
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may validate the form using java script, so that the user will not provide the empty string in the Search Box.
 
Justin Muir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Nair wrote:You may validate the form using java script, so that the user will not provide the empty string in the Search Box.

Yes but i mean when i run the project, or load it, it gets that there is no text in the box and then sits forever trying to load the page. I am assuming this is because
it is finding everything with just a white space?

Not sure though
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, for the first time the search box is empty but I am not able to understand how the form is automatically submitted. Are you trying to show the result on the page bellow the search box when user loads the form, If this is the case then you need to handle the empty string condition in your code.

 
Justin Muir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks man, but i got it working now.

Onto the next problems now haha... I'm new to the Web dev. side and actually haven't worked with Java for quite a while so just getting the hang of things
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!