[java] Bullshit exceptions

Started by
9 comments, last by Darragh 18 years, 11 months ago
On a game I'm currently working in Java, I'm trying to get the 16 bit data from a BufferedImage. I do this by creating a BufferedImage of TYPE_USHORT_565_RGB, and set the data. I then use this statement: DataBuffer data = image.getRaster().getDataBuffer(); to get the DataBuffer. However I need to convert the DataBuffer to DataBufferUShort to be able to retrieve data, and I also need it in an unsigned short form. ((DataBufferUShort)data).getData() ^^ That is how I do that (not the full statement by the way, don't correct syntax). What then happens when I run it, is it causes ClassCastException. I have no idea why it is doing this. It's a direct subclass of DataBuffer. Any ideas why this might be happening? Any help would be appreciated, thanks
Advertisement
Quote:
What then happens when I run it, is it causes ClassCastException. I have no idea why it is doing this. It's a direct subclass of DataBuffer.


The exception explicitly says you it is not a subclass of DataBuffer. If you ask me: I believe the computer! :-)

Although the buffer may hold unsigned data that does not guarantee that the object is a super class as you think (unless the documents say so and even then it is not sure of course).

Greetz,

Illco
It's a direct subclass, and that's why I don't get why it's throwing that exception. DataBuffer - http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/DataBuffer.html
It's listed in the subclasses.
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/DataBufferUShort.html
That extends DataBuffer.
DataBufferUShort is a subclass of DataBuffer, so it would be cool if you turned a DataBufferUShort into a DataBuffer. But you're trying to go backwards. A DataBuffer can't necessarily be casted into a DataBufferUShort.

Let's say Cat is a subclass of an Animal. If someone gives me a random Animal, I can't necessarily start feeding it cat food.
Ok. So generally: B extends A. But that does not mean that every instance of A is also a B! It just says that some instances of B are also A's.

DataBufferUShort extends DataBuffer. So some instances of DataBuffer are in fact DataBufferUShort's. But it is not true that you can cast any DataBuffer to a DataBufferUShort. In this particular case it seems you can not.

@pinacolade: well you could try to feed it cat food but you better be careful what type it actually was. :-)
Quote:Original post by Ganoosh_
On a game I'm currently working in Java, I'm trying to get the 16 bit data from a BufferedImage. I do this by creating a BufferedImage of TYPE_USHORT_565_RGB, and set the data.

Obviously then your creation isn't doing what you think it is. I can't see anything that says that USHORT_565 actually uses a UShort buffer behind the scenes, as such you're not guranteed to get anything specific back. Have a look in the debugger (or print the data.class to System.out) and see what class it *actually* is. You'll probably find its actually using a different internal format.

In general, explicit casts like this are bad unless it specifically mentions that such casts will work! Otherwise just work with the interface you've been given.
i just glanced at this thread, so sorry if i repeat.

try not using java
Quote:Original post by Anonymous Poster
i just glanced at this thread, so sorry if i repeat.

try not using java


Ya, good one! :p

Can't have those damn exceptions and crap, enforcing correctness, and everything! :P
Include the stack trace from the exception. Sometimes that will tell you what the real class is.

"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Lazy man's exception handling (well you could remove the print statement to be lazier) hehe.. and change the IOException to a more general Exception for total slacking.

try {  someBullshitThatThrowsAnIOException();} catch (IOException e) {  System.out.print("IOException thrown by someBullshitThatThrowsAnIOException()");  e.printStackTrace()}

This topic is closed to new replies.

Advertisement