0

hey guys
I'm trying to get hold of a StreamConnection

try {
             StreamConnection conn =  ( StreamConnection )  Connector.open ( url ) ; 

        } catch (Exception ex) {
            ex.printStackTrace();
        }

however when i do this i get the following error

java.lang.ClassCastException

According to my know of and google it should compile fine. I've used this in a J2EE project and it works correctly but not in a J2ME???

5
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Bens
0

the exception tells you exactly what's wrong; you can't cast it, because it's not that type

what makes you think that it is a StreamConnection? and why do you need it to be?

0

The stream connection is dervived from that class and therefore can be an instance of it. The connector class can be casted to any of its child / parents... hence the HTTPConnection instance etc... take a look at my pervious url link

0

Before assuming anything just do a System.out.println(Connector.open(url).getClass()) and see which class instance are you actually trying to cast.

0

The example will show you to convert one data type into another data type by using type casting. The step involved in the program are given below- Vector v = new Vector() :-Creates a new vector. Object obj = v.add( i ) :-Adds an integer type to the vector and stores in an object

To avoid this exception we have to add Integer y = new Integer((String)obj) instead of String y = (String)obj.

CastException.java

import java.util.Vector;
 
public class CastException {
 
  public static void main(String[] args) {
   Vector v = new Vector();
   int i = 10;
   Object obj = v.add(i);
   try {
   String y = (String)obj;
   v.add(y);
  } catch (ClassCastException e) {
   System.out.println("Class is really: " + obj.getClass().getName());
   e.printStackTrace();
   }
   }
 }

Output of the program:

java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
 at CastException.main(CastException.java:11)

Edited by ~s.o.s~: Added code tags, learn to use them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.