0

I'm developing an android application to connect to a server. I'm having some issues with errors, the exact same application works perfectly with java swing on a standard pc. The exact error is E/AndroidRuntime(1257): java.lang.ClassCastException: net.rc.packet.incoming.lister.PServerList cannot be cast to net.rc.packet.Packet This is my PServerList class

package net.rc.packet.incoming.lister;
import java.util.List;
import net.rc.entity.ServerDescription;
import net.rc.packet.incoming.ServerPacket;
public class PServerList implements ServerPacket {
    public List < ServerDescription > server_descriptions;
    public PServerList(List < ServerDescription > server_descriptions) {
        this.server_descriptions = server_descriptions;
    }
    public String toString() {
            StringBuilder string_builder = new StringBuilder();
            string_builder.append("[");
            for (ServerDescription server_description: server_descriptions) {
                string_builder.append(server_description);
                string_builder.append(", ");
            }
            string_builder.append("]");
            return string_builder.toString();
        }
    }
}

And here's my socketprocessor class

package net.rc.socket;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.inject.Inject;
import net.rc.packet.Packet;
import net.rc.socket.processor.PacketProcessor;
public class SocketProcessor { @ Inject
    public SocketProcessor(List < PacketProcessor > all_processors) {
        this.all_processors = all_processors;
    }
    private final List < PacketProcessor > all_processors;
    private final Queue < Byte > buffer = new LinkedBlockingQueue < Byte > ();
    private PacketProcessor active_processor = null;
    public void accept(byte[] bytes) {
        for (int i = 0; i < bytes.length; i++) {
            buffer.offer(bytes[i]);
        }
    }

    public boolean isComplete() {
        while (!buffer.isEmpty()) {
            if (active_processor == null) {
                int packet_code = (buffer.peek() - 32) & 0xFF;
                for (PacketProcessor processor: all_processors) {
                    if (processor.handles(packet_code)) {
                        active_processor = processor;
                        break;
                    }
                }
            } else {
                if (active_processor.isSegmentDone() && !active_processor.isComplete()) {
                    active_processor = null;
                    continue;
                }
            }
            if (active_processor.isComplete()) {
                return true;
            }
            active_processor.accept(buffer.poll());
        }
        if (active_processor == null) {
            return false;
        } else {
            return active_processor.isComplete();
        }
    }

    public < T extends Packet > T getPacket() {
        if (!isComplete()) return null;
        T packet = active_processor.getPacket();
        active_processor = null;
        return packet;
    }

    public boolean isEmpty() {
        return buffer.isEmpty();
    }
}

Any ideas? I'm stuck. Here's the logcat

E/AndroidRuntime(1257): java.lang.ClassCastException: net.rc.packet.incoming.lister.PServerList cannot be cast to net.rc.packet.Packet
E/AndroidRuntime(1257):  at net.rc.socket.SocketProcessor.getPacket(SocketProcessor.java:70)
E/AndroidRuntime(1257):  at net.rc.packet.DefaultPacketServer.receive(DefaultPacketServer.java:40)
E/AndroidRuntime(1257):  at net.rc.RemoteControlHandler.run(RemoteControlHandler.java:23)
E/AndroidRuntime(1257):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
E/AndroidRuntime(1257):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
E/AndroidRuntime(1257):  at java.lang.Thread.run(Thread.java:856)
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Nick Garfitt
  • 35
  • 1
  • 9

1 Answers1

0
public < T extends Packet > T getPacket() {
    if (!isComplete()) return null;
    T packet = active_processor.getPacket();
    active_processor = null;
    return packet;
}

Extends, used this way, doesn't mean that T is a Packet. You could use instanceOf instead.

aran
  • 10,978
  • 5
  • 39
  • 69
  • Certainly it means it 'is-a' Packet. -1 – user207421 Apr 29 '13 at 10:02
  • No it doesn't. Look again. PServerList cannot be cast to net.rc.packet.Packet. So that method is getting a PServerList as a Packet. And the compiler says it "is-not" a Packet. – aran Apr 29 '13 at 10:08
  • So I should do something like if(!T instanceof packet) return; ? Sorry been awake for hours trying to fix this, slowly falling asleep. – Nick Garfitt Apr 29 '13 at 10:19
  • If you don't want to get the CastException, make an if of (T instanceOf Packet). That will asure you that T is indeed a Packet. – aran Apr 29 '13 at 10:29
  • 'T extends Packet' means that T extends or implements Packet. Contrary to the first sentence of your answer. That's *all* it means. There is nothing else it could mean. If there's something else in the list the problem lies elsewhere. – user207421 Apr 29 '13 at 10:32
  • It's really pretty simple: if you are trying to typecast an object of class A into an object of class B, and they aren't compatible, you get a class cast exception. So if you try to EXTEND a T when it's not a Packet, you get the error. The first lines that I copied are gramatically correct as he uses wildcards. Still: Extends doesn't mean that T is a Packet and none of what I've written is incorrect. – aran Apr 29 '13 at 10:36
  • This code is from my friend who gave me it to port a java swing desktop application to an android application, and he's unreachable so I can't ask him how it all works. Plus i'm not too good with java. So my question is, how would I change the method public < T extends etc to not give an error. I know it would need an if statement but i'm getting really confused. – Nick Garfitt Apr 29 '13 at 10:38
  • The error is probably here: T packet = active_processor.getPacket();Before that line, add this : if (T instanceOf Packet) – aran Apr 29 '13 at 10:39
  • The packet class is as follows. `package net.rc.packet; import java.io.Serializable; public interface Packet extends Serializable { }` – Nick Garfitt Apr 29 '13 at 10:40
  • I tried [this](http://pastebin.com/HJzYzxQp), but now nothing happens, the app just hangs with GC_CONCURRENT freed 494K, 20% free 12434K/15495K, paused 2ms+2ms, total 22ms. – Nick Garfitt Apr 29 '13 at 10:46
  • Put a counter inside the if and print how many times a T is an instance of Packet. Maybe the problem resides elsewhere. – aran Apr 29 '13 at 10:49
  • That's it. I solved your exception, at least. The problem you're having is another, since it isn't able to get Packets. Hope to help you next time too, but I think you may start a new question for this one. – aran Apr 29 '13 at 11:00
  • @EJP, I hope you consider your possition. – aran Apr 29 '13 at 11:00
  • The odd thing is, it works perfectly on PC, yet android seems to be more picky. – Nick Garfitt Apr 29 '13 at 11:08
  • First of all, I'd try contacting the guy who did this. If impossible, make some research and try to understand what the code does and why are you not receiving any packets. Maybe you'll be stuck still, but I bet that if you come here with a good presented question, folks around here will be glad to help. Me included. – aran Apr 29 '13 at 11:10