Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

variable "retriever" might not have been initialized - confused

Thread view: 
phillip.s.powell@gmail.com - 16 Jan 2006 10:07 GMT
[CODE]
 String message = "", send = "", quit = "", errorMsg = "", cookie =
"";
 boolean hasSubmittedMessage;
 HTMLRetriever retriever;

 // SEND MESSAGE TO SERVLET
 if (hasSubmittedMessage && send.length() > 0) {
  try {      //  USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
   retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
                                 URLEncoder.encode(message, "UTF-8") +
"&nickname=" +
                                 URLEncoder.encode(cookie, "UTF-8")
                                );                                    // SEND MESSAGE
  } catch (UnsupportedEncodingException uee) {
   errorMsg = "Error involving message submittal: " + uee.toString();
  } catch (Exception e) {
   errorMsg += "Unknown error: " + e.toString();
  }
 } else if (hasSubmittedMessage && quit.length() > 0) {
  try {    //  USING NEW VERSION OF URLEncoder.encode() THAT REQUIRES
try{} BLOCK DUE TO NEW 2ND PARAMETER OF ENC-TYPE
   retriever = new HTMLRetriever(ChatGlobals.SERVLET_SELF +
"/ppowell.ChatServlet?message=" +
                                 URLEncoder.encode("/q", "UTF-8") +
"&nickname=" +
                                 URLEncoder.encode(cookie, "UTF-8")
                                );                                    // SEND "QUIT" COMMAND
  } catch (UnsupportedEncodingException uee) {
   errorMsg += "Error involving message submittal: " + uee.toString();
  } catch (Exception e) {
   errorMsg += "Unknown error: " + e.toString();
  }
 }

 try {
  if (retriever.getHTML() != null && quit.length() > 0)
   out.println("<script type=\"text/javascript\">\n<!--
self.parent.close();\n//-->\n</script>\n");
 } catch (Exception e) {} // DO NOTHING

// STUFF

[/CODE]

Produces this error:

[ERROR]
/~ppowell/includes/chat_submit_message.jsp:63: variable retriever might
not have been initialized
  if (retriever.getHTML() != null && quit.length() > 0)
[/ERROR]

I can't figure out why since I'm capturing all errors because
"retriever" might not be initialized, which is the case if
hasSubmittedMessage = false.  You see, if the user hasn't yet submitted
a message, nothing can be sent to the servlet, thus, the HTMLRetriever
variable "retriever" will not be initialized into a class object
because it can't unless it receives a value, which it can only do so
upon the right conditions, otherwise, no value can be passed into
"retriever".

Anyone know what I can do about this? I'm sorry but once again in PHP
this is not a problem, and I'm sorry but I have no choice but to
"translate" PHP scripts into JSP due to client dropping PHP support.

Thanx
Phil
Torkel Franzen - 16 Jan 2006 10:25 GMT
> Anyone know what I can do about this?

 You satisfy the compiler by changing the declaration of retriever to

  HTMLRetriever retriever=null;
phillip.s.powell@gmail.com - 16 Jan 2006 10:45 GMT
Good grief that was too simple, and I just didn't get it.  Tack så
mycket!

Phil

> > Anyone know what I can do about this?
>
>   You satisfy the compiler by changing the declaration of retriever to
>
>    HTMLRetriever retriever=null;
Nigel Wade - 17 Jan 2006 17:00 GMT
> Good grief that was too simple, and I just didn't get it.  Tack så
> mycket!
[quoted text clipped - 6 lines]
>>
>>    HTMLRetriever retriever=null;

The problem is that there is a route to the code:

   if (retriever.getHTML()...

where retriever has not been set to any value. The catch blocks don't assign it,
but allow execution to continue.

As Torkel says, you can satisfy the compiler by initializing retriever to null,
and the compiler error will go away. The logic is still broken, however, as
there is still a route to that piece of code where you use retriever without it
having a proper value. Now, you will get a NullPointerException if that route
is taken. The compiler error is there to help point out to you that there is
something wrong in the code. Getting rid of the compiler error message hasn't
got rid of the fundamental error in the logic.

You've replaced a compiler error with a logic bomb waiting to dump on you at
some later time, unless you catch that NullPointerException and deal with it
appropriately, or protect the retriever.getHTML() by testing for null first.

I think a better option would be to re-organize the code so that retriever is
only used within the try {} block, where it has a valid value. The code looks
like it's designed with errors signalled by return value rather than thrown
exceptions.

This would be one "exceptional" way of doing it:

try {
if (hasSubmittedMessage && send.length() > 0) {
   retriever = new HTMLRetriever(...
}
else if (hasSubmittedMessage && quit.length() > 0) {
   retriever = new HTMLRetriever(...
}
if (retriever.getHTML() ...
}
} catch (UnsupportedEncodingException uee) {
...

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.