ciao ragazzi sono alle prese con degli esercizi e ho trovato un problema che non sono riuscito a risolvere. premetto che non è il primo che faccio ma questo errore non mi era mai capitato non so se potete aiutarmi. di sicuro avrò scritto qualche bestialità, il problema è che non riesco a trovarla...
in particolare nella classe giocatore non appena lo eseguo mi da nullpointerexception precisamente quando ricavo lo stream dal socket. non riesco a risolverlo.
vi ringrazio anticipatamente
vi metto un altro po di codice perchè l'errore secondo me proviene da qualche altra parte...codice:import java.net.*; import java.io.*; public class Giocatore { int idpista; int porta; String indirizzo; ObjectOutputStream os; ObjectInputStream is; Turno t; Socket s; public Giocatore(int idpista,String indirizzo,int porta) { this.idpista=idpista; this.porta=porta; this.indirizzo=indirizzo; } public void lancia() { try{ s = new Socket(indirizzo,porta); //si blocca in questo punto e non alloca gli stream is = new ObjectInputStream(s.getInputStream()); os = new ObjectOutputStream(s.getOutputStream()); System.out.println("ciaaaaaaaaaaaaaaaa"); t = calcolaPunteggio(); os.writeObject(t); System.out.println("ho lanciato"); os.flush(); s.close(); is.close(); os.close(); }catch(Exception e){ System.err.println(e.getMessage()); } } private Turno calcolaPunteggio() { Turno tur=new Turno (idpista); tur.getPrimoTiro().setPunteggio(10); int parziale = tur.getPrimoTiro().getPunteggio(); if(tur.getPrimoTiro().isStrike()) { tur.setStrike(); } else{ tur.getSecondoTiro().setPunteggio(10-parziale); tur.setPunteggio(); } return tur; } }
codice:import java.io.*; import java.net.*; public class Servitore implements Runnable{ private int porta; ServerSocket ss ; Socket clientsock; public Servitore(int porta ){ this.porta=porta; } public void run() { try{ ss=new ServerSocket(porta); while(true) { clientsock = ss.accept(); LCD schermo = new LCD(clientsock); Thread t = new Thread(schermo); t.start(); clientsock.close(); } }catch(Exception e ){ System.err.println(e.getMessage()); } } }codice:import java.net.*; import java.io.*; public class LCD implements Runnable{ private Socket sc; ObjectInputStream is; ObjectOutputStream os; Turno tur; public LCD(Socket sc) { this.sc=sc; } public void run() { try{ os = new ObjectOutputStream(sc.getOutputStream()); //si blocca in questo punto e non alloca gli stream is = new ObjectInputStream(sc.getInputStream()); tur=(Turno)is.readObject(); System.out.println(tur.toString()); LCDFrame grafica = new LCDFrame(tur); sc.close(); is.close(); os.close(); }catch(Exception e){ System.out.println(e.getMessage()); } } }codice:import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class GiocatoreFrame extends JFrame implements ActionListener{ JPanel panel; Container c; JButton b1; Giocatore player; public GiocatoreFrame(Giocatore player) { this.player=player; panel=new JPanel(); c=getContentPane(); c.add(panel); setTitle("giocatore"); b1=new JButton("Tira"); add(b1); b1.addActionListener(this); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); } public void actionPerformed(ActionEvent e) { player.lancia(); } }codice:public class GiocatoreMain { public static void main(String args[]) { try{ Integer i = new Integer(0); i.parseInt(args[0]); Giocatore g = new Giocatore(1,"localhost",5555); GiocatoreFrame frame = new GiocatoreFrame(g); }catch(ArrayIndexOutOfBoundsException e){ System.err.println("inserisci l'id della pista"); } } }codice:public class ServitoreMain{ public static void main(String args[]) { Servitore serv = new Servitore(5555); Thread t = new Thread(serv); t.start(); } }

Rispondi quotando