Strange exception

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I'm attempting to add a byte[] to a TreeSet. The first time that I
add a byte[] to it, it works just fine. The second time, it causes an
exception, although it is really not clear what causes the exception
to occur. This is what is happening when the exception occurs:

//convert a string containing an IP address to a byte[]
byte[] IPAddressBytes = InputIPAddress.getBytes();

//Add the byte[] to the TreeSet
MyTreeSet.add(IPAddressBytes);

This is being run inside a loop. The first time through, it runs just
fine. The second time through, I get the following exception:

Exception in thread "main" java.lang.ClassCastException: [B
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)

If I try putting this in a try block, I get:
OutgoingConnectionHandler: Add:java.lang.ClassCastException: [B

I have no clue what [B is supposed to mean. Any ideas? What am I
doing wrong here?

Thanks!
 
K

Knute Johnson

I'm attempting to add a byte[] to a TreeSet. The first time that I
add a byte[] to it, it works just fine. The second time, it causes an
exception, although it is really not clear what causes the exception
to occur. This is what is happening when the exception occurs:

//convert a string containing an IP address to a byte[]
byte[] IPAddressBytes = InputIPAddress.getBytes();

//Add the byte[] to the TreeSet
MyTreeSet.add(IPAddressBytes);

This is being run inside a loop. The first time through, it runs just
fine. The second time through, I get the following exception:

Exception in thread "main" java.lang.ClassCastException: [B
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)

If I try putting this in a try block, I get:
OutgoingConnectionHandler: Add:java.lang.ClassCastException: [B

I have no clue what [B is supposed to mean. Any ideas? What am I
doing wrong here?

Thanks!

How do you compare two arrays of byte? If you don't have a Comparator
then I think that is where your problem lies.
 
M

Mike Schilling

I'm attempting to add a byte[] to a TreeSet. The first time that I
add a byte[] to it, it works just fine. The second time, it causes
an
exception, although it is really not clear what causes the exception
to occur. This is what is happening when the exception occurs:

//convert a string containing an IP address to a byte[]
byte[] IPAddressBytes = InputIPAddress.getBytes();

//Add the byte[] to the TreeSet
MyTreeSet.add(IPAddressBytes);

This is being run inside a loop. The first time through, it runs
just
fine. The second time through, I get the following exception:

Exception in thread "main" java.lang.ClassCastException: [B
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)

If I try putting this in a try block, I get:
OutgoingConnectionHandler: Add:java.lang.ClassCastException: [B

I have no clue what [B is supposed to mean. Any ideas? What am I
doing wrong here?

"[B" is the internal name for the class "Array of bytes". What's
going on is that TreeSet keeps its entries sorted, meaning that it
needs a way to compare two elements to each other. When you add the
first element, there's nothing to comapre it to, so you're OK. When
you add the second element, the TreeSet code casts both the
Comparable, so it can compare them, but "array of byte" doesn't
implement Comparable, hence the exception. (I'm assuming you didn't
create the TreeSet to use a Comaprator rather than the "natrual
ordering" imposed by Comparable; if you had, we'd see its compare
method on the stack trace.)

Why are you using a TreeSet? If it's because you do want to keep the
addreses sorted in some order, then you need to implement a Comparator
that implements that ordering and specify it when constructing the
TreeSet object. If not, you're using the wrong Set implementation.
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top