Re: Why is Java lying?



You have to cast from Object to String.


java does not know that that "Object" is a "String".
it does know that "String"s are "Object"s.

You have to cast whenever you go from general->specific

String v2 = (String) session.getValue(attr);

On Nov 28, 12:12 pm, Thomas Fritsch <i.dont.like.s...@xxxxxxxxxxx>
wrote:
laredotorn...@xxxxxxxxxxx wrote:
I'm trying to compile a very simple JSP page on Tomcat 5.5, JDK 1.5

<%@ page import="java.util.*" %>
<%
Object v = new String("b");
session.setAttribute("a", v);

Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String attr = (String) e.nextElement();
String v1 = session.getAttribute(attr);
String v2 = session.getValue(attr);I suspect the JSP compiler complains about the line above, because
HttpSession.getValue is declared as
public Object getValue(String name)
but not as
public String getValue(String name)
See
<http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession....>.



out.println("attr: " + attr + " v1:" + v1 + " v2:" + v2);
} // while
%>

but I'm getting this compile error:

An error occurred at line: 2 in the jsp file: /session_vars.jsp
Generated servlet error:
Type mismatch: cannot convert from Object to String

An error occurred at line: 2 in the jsp file: /session_vars.jsp
Generated servlet error:
Type mismatch: cannot convert from Object to String

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

First off, the line number is incorrect (line 2 is "<%") but more
importantly, I thought String extended Object.Don't ask me why the compiler says "line: 2", although it actually seems
to be line 10.



Any thoughts? - Dave--
Thomas

.



Relevant Pages

  • Re: Invalid CastException with SqlDBType
    ... Add(string*, SqlDbType, int, string**) ... The DateTime.Now is trying to cast into a int. ... not in Sql set up the column as identity and let it seed itself. ... > update a MS SQL Database with a stored procedure. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Porting up to VC++ 2005
    ... Since you are dealing with strings that contain file names, ... characters, but that's a different question to be handled in a different way. ... done, which should be unnecessary, it should be an LPTSTR cast, which ... It only applies to string and ...
    (microsoft.public.vc.mfc)
  • Re: Confused about using Reflection to examine a collection
    ... the precise type that Reflection is saying it is. ... say that 'obj' is actually a custom collection. ... If it were a basic type, such as 'string' then I would do a cast like this: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... the boolean is implicitly cast to a string. ... If you look up the manual on type casting, you'll find that FALSE is cast to an empty string, and TRUE is cast to a non-empty string. ...
    (comp.lang.php)
  • Re: Error whilst processing ORDER BY clause
    ... My guess would be that you need to CAST it to a boolean since it is assuming ... > We receieve the error "Error whilst processing ORDER BY clause in the SQL ... name in the clause to reference a string or int data type it also succeeds, ... Can we use a CAST ...
    (microsoft.public.exchange2000.applications)