ciao a tutti...
ho creato questa classe che mi converte array di byte in oggetti e viceversa, per la trasmissione di oggetti tramite pacchetti datagram.
quando faccio questa cosa:codice:public class DatagramUtility { public static byte[] getBytes(Object obj) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream outStream = new ObjectOutputStream(baos); outStream.writeObject(obj); byte[] buf = baos.toByteArray(); outStream.close(); baos.close(); return buf; } public static Object getObject(byte[] buf) throws IOException, ClassNotFoundException{ ByteArrayInputStream bais = new ByteArrayInputStream(buf); ObjectInputStream inStream = new ObjectInputStream(bais); Object obj = inStream.readObject(); return obj; } }
LocalPlayer p = new LocalPlayer();
byte[] b = DatagramUtility.getBytes(p);
DatagramUtility.getObject(b);
ovvero converto un oggetto in byte e subito dopo lo faccio diventare un oggetto di nuovo, mi ritorna un nullpointer exception proprio nella 3^ riga...
Se faccio la stessa operazione con un oggetto di Java, come String, tutto funziona correttametne...
vi posto la classe LocalPlayer e tutta la sua gerarchia
la classe padre, player e di tipo serializable e tutte hanno un uid assegnato...codice:public class LocalPlayer extends ClientPlayer implements KeyListener { private static final long serialVersionUID = 2266117503032923258L; private ServerListener sendToServer; public LocalPlayer() { super(new Point(0,0),(byte)0,0); } public LocalPlayer(Point point,int direction) { super(point,(byte)0,direction); } public void setServerListener(ServerListener serverListener) { sendToServer = serverListener; } public ServerListener getServerListener() { return sendToServer; } public void keyTyped(KeyEvent ke) {} public void keyPressed(KeyEvent ke) {} public void keyReleased(KeyEvent ke) { boolean arrowKeys,directionChange; arrowKeys = (ke.getKeyCode()==KeyEvent.VK_UP)||(ke.getKeyCode()==KeyEvent.VK_DOWN) ||(ke.getKeyCode()==KeyEvent.VK_LEFT)||(ke.getKeyCode()==KeyEvent.VK_RIGHT); directionChange=getDirection() != ke.getKeyCode(); if (arrowKeys && directionChange) { setDirection(ke.getKeyCode()); computeDirection(); try { sendToServer.sendToServer((Player) this); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } } } } public class ClientPlayer extends Player implements ActionListener { private static final long serialVersionUID = 644461926280548522L; public static final byte OFFSET = 2; private int left; private int top; private Timer moveTick; private Avatar clientAvatar; public ClientPlayer(Point point, byte id,int direction) { super(point,id,direction); computeDirection(); moveTick = new Timer(Player.TIME,this); clientAvatar = new Avatar(getLocation()); } .... } public abstract class Player implements Serializable { private static final long serialVersionUID = -1266517203068921238L; public static final int DELTA = 5; public static final int TIME = 50; public static final int BALL_SIZE = 24; public static final Dimension SCREEN_SIZE = new Dimension(450,500); private Point location; private int direction; private byte id; ... }
dove sbaglio?
vi ringrazio anticipatamente

Rispondi quotando