Java, drag and drop and the curse of the transients
I've been working on PCPal for a few days and I've got to the point where I wanted to implement drag and drop functionality. After reading a couple of tutorials and scanning the API, all looked well enough; seemed fairly straightforward. Basically, you have to create a transfer handler for each side of the transaction (though it can be the same object type, as in my initial case) - then form a data transport object (a transferable); register with the required component types, implements some check logic in the handlers to test for the correct datatypes, and then glue in the mouse drag code (well, actually, reading that doesn't seem so straight forward). After getting all working with a basic string transfer between a custom thumbnail object and a custom button object, I decided to hook in the back end storage abstraction layer so I could pass the location of the object I was dragging rather than a generic string representation. All of a sudden I'm getting nothing happening at the closing end of the transfer... put breakpoints in... and 30 minutes later or so I track it down to an exception being thrown by a _debug_ statement! wtf... so, turns out it's a null pointer exception: but before this method call the exact same object (or so I thought) was working fine...
heh... thing they don't tell you in the tutorials I read is that in the middle of the transfer there's a serialization/de-serialization process going on, and my new data type I was passing held it's primary data store (in this case a File object down at the bottom) as a transient field - oops... transient fields don't pass through serialization, and simply get recreated as empty objects at the other side (i.e. my data store object had become a 'new File()'). So... when I went to exxtract the absolute path from the abstraction object I was passing, the File object had no internals - just a null ***BANG***.
Note to self: DON'T USE transients WITH D'n'D!!
A two hour lesson there ... I think I need a session on Empire now :)
heh... thing they don't tell you in the tutorials I read is that in the middle of the transfer there's a serialization/de-serialization process going on, and my new data type I was passing held it's primary data store (in this case a File object down at the bottom) as a transient field - oops... transient fields don't pass through serialization, and simply get recreated as empty objects at the other side (i.e. my data store object had become a 'new File()'). So... when I went to exxtract the absolute path from the abstraction object I was passing, the File object had no internals - just a null ***BANG***.
Note to self: DON'T USE transients WITH D'n'D!!
A two hour lesson there ... I think I need a session on Empire now :)

0 Comments:
Post a Comment
<< Home