26. april 2005 - 21:30
Der er
4 kommentarer og 1 løsning
DatagramSocket giver runtime Exception
Hej Arne, ;) Jeg kan simpelthen ikke lure hvorfor jeg får denne runtime exception: java.lang.NullPointerException Når dette kode køres: while(true){ bufReader = new BufferedReader(new inputStreamReader(System.in)); try{ inputString = bufReader.readLine(); System.out.println("Trying to sendmessage: " + inputString); DatagramPacket packet = new DatagramPacket(inputString.getBytes(), inputString.getBytes().length, remoteAddress, remotePort); System.out.println("" + remoteAddress); sock.send(packet); }catch(Exception e){System.out.println("UserThread1 : " + e);} fejlen kommer ved sock.send(packet) Kan du eller andre hjælpe?
Annonceindlæg fra HP
26. april 2005 - 21:50
#2
import java.net.*; class SendMessagesMain{ public static void main(String args[]){ try{ DatagramSocket sock = new DatagramSocket(2000); InetAddress remoteAddress = InetAddress.getByName("xx.xx.xx.xx"); int remotePort = 3000; UserInput ui = new UserInput(sock,remotePort,remoteAddress); ui.start(); byte[] inputBuffer = new byte[sock.getReceiveBufferSize()]; DatagramPacket dp = new DatagramPacket(inputBuffer,inputBuffer.length); // Server loop while(true){ sock.receive(dp); System.out.println("" + dp.getData()); } }catch(Exception e){System.out.println("bla: " + e);} } } import java.io.*; import java.net.*; public class UserInput extends Thread{ DatagramSocket sock; InetAddress remoteAddress; int remotePort; public UserInput(DatagramSocket sock, int port, InetAddress add){ try{ remoteAddress = add; remotePort = port; sock = new DatagramSocket(port); }catch(Exception e){} } BufferedReader bufReader; String inputString; public void run(){ while(true){ bufReader = new BufferedReader(new InputStreamReader(System.in)); try{ inputString = bufReader.readLine(); System.out.println("Trying to sendmessage: " + inputString); DatagramPacket packet = new DatagramPacket(inputString.getBytes(), inputString.getBytes().length, remoteAddress, remotePort); System.out.println("" + remoteAddress); sock.send(packet); }catch(Exception e){System.out.println("UserThread1 : " + e);} } } }
26. april 2005 - 21:53
#4
Det skal selvfølgelig være: public UserInput(DatagramSocket sock, int port, InetAddress add){ try{ remoteAddress = add; remotePort = port; this.sock = sock; }catch(Exception e){} }