Strange exception when reading from a buffer

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I'm trying to read some data from a channel using java NIO. I create
a connection successfully, and then I try to read from the channel,
but I get the following error:

java.lang.ClassCastException: sun.nio.ch.ServerSocketChannelImpl

The following is the code that results in the error:

SocketChannel Channel;

public ChannelReader(ByteBuffer ReceiveBuffer, SelectionKey InputKey)
{
this.ReceiveBuffer = ReceiveBuffer;
this.key = InputKey;
try {
Channel = (SocketChannel) InputKey.channel();
} catch (Throwable Exception) {
System.out.println("ChannelReader: ChannelReader:" +
Exception);
}
}

The error occurs on the line: Channel = (SocketChannel)
InputKey.channel();

What is causing this? How can I fix it?

Thanks!
 
L

Lew

I'm trying to read some data from a channel using java NIO. I create
a connection successfully, and then I try to read from the channel,
but I get the following error:

java.lang.ClassCastException: sun.nio.ch.ServerSocketChannelImpl

The following is the code that results in the error:

SocketChannel Channel;

By well-established Java convention, variable names begin with a lower-case
letter. Type names begin with an upper-case letter.
public ChannelReader(ByteBuffer ReceiveBuffer, SelectionKey InputKey)
{
this.ReceiveBuffer = ReceiveBuffer;
this.key = InputKey;
try {
Channel = (SocketChannel) InputKey.channel();
} catch (Throwable Exception) {
System.out.println("ChannelReader: ChannelReader:" +
Exception);
}
}

The error occurs on the line: Channel = (SocketChannel)
InputKey.channel();

What is causing this? How can I fix it?

What is causing the 'ClassCastException' is an attempt to cast an object of
one class or type, in this case some subclass of 'ServerSocketChannel', to an
incompatible type, in this case 'SocketChannel'. You cannot cast from a type
to another type that is not a superclass or subclass of the source type.

Here's how to research that sort of problem on your own:
Thrown to indicate that the code has attempted to cast an object
to a subclass [sic] of which it is not an instance.

(They specify "subclass" because it is never necessary to cast to a superclass.)

'SocketChannel' is not a subclass of 'ServerSocketChannel', much less of
'sun.nio.ch.ServerSocketChannelImpl'.
 
R

Roedy Green

On Sat, 18 Jul 2009 21:24:19 -0700 (PDT),
Channel = (SocketChannel) InputKey.channel();


When you quote an error message, point out the line in the code which
triggered it. That makes it easier for others to track down your
problem.

see
http://mindprod.com/jgloss/runerrormessages.html#CLASSCASTEXCEPTION
--
Roedy Green Canadian Mind Products
http://mindprod.com

"For reason that have a lot to do with US Government bureaucracy, we settled on the one issue everyone could agree on, which was weapons of mass destruction."
~ Paul Wolfowitz 2003-06, explaining how the Bush administration sold the Iraq war to a gullible public.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top