Sign in to follow this  
Ganoosh_

[java] Bullshit exceptions

Recommended Posts

Ganoosh_    173
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

Share this post


Link to post
Share on other sites
Illco    928
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

Share this post


Link to post
Share on other sites
Ganoosh_    173
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.

Share this post


Link to post
Share on other sites
pinacolada    834
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.

Share this post


Link to post
Share on other sites
Illco    928
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. :-)

Share this post


Link to post
Share on other sites
OrangyTang    1298
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
i just glanced at this thread, so sorry if i repeat.

try not using java

Share this post


Link to post
Share on other sites
tebriel    904
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

Share this post


Link to post
Share on other sites
tebriel    904
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()
}


Share this post


Link to post
Share on other sites
Darragh    308
Quote:

System.out.print("IOException thrown by someBullshitThatThrowsAnIOException()");


[lol] If only every error message was this amusing!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this