G
Guest
Gast
Guten Morgen, bei mir fliegt beim starten des Clients eine Nullpointer exception.
Server.java line 33
Ich kann nur nicht nachvollziehen warum
Server.java line 33
Ich kann nur nicht nachvollziehen warum
Code:
import java.net.*;
import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server extends Thread {
Boolean isStarted = false;
ServerSocket server;
Socket client;
DataOutputStream out;
DataInputStream in;
List<ClientData> list;
@Override
public synchronized void run() {
try {
server = new ServerSocket(4000);
} catch (IOException ex) {
System.out.println("Probleme mit Socket");
}
while (isStarted) {
try {
client = server.accept();
in = new DataInputStream(client.getInputStream());
out = new DataOutputStream(client.getOutputStream());
ClientData cl = new ClientData(client, out);
String chatNachricht = in.readUTF();
System.out.println(chatNachricht);
list.add(cl);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
server.close();
client.close();
} catch (IOException ex) {
System.out.println("put");
}
}
public void writeToAll(String x) {
for (ClientData d : list) {
try {
d.getOutputStream().writeUTF(x);
d.getOutputStream().flush();
} catch (IOException ex) {
System.out.println("Fehler beim erzeugen der Nachricht");
}
}
}
public List getClients() {
return list;
}
public void removeClient(Socket client) {
list.remove(client);
}
public void setOffline() {
isStarted = false;
}
public void setOnline() {
isStarted = true;
}
}
Code:
import java.net.*;
import java.io.*;
public class Client {
Client() throws IOException {
Socket server = new Socket("localhost", 4000);
DataOutputStream out = new DataOutputStream(server.getOutputStream());
DataInputStream in = new DataInputStream(server.getInputStream());
out.writeUTF("login331");
//out.writeUTF("passwort");
out.flush();
System.out.println(in.readUTF());
//System.out.println(input.read());
server.close();
out.close();
}
public static void main(String[] args) {
try {
Client client = new Client();
} catch (IOException e) {
System.err.println("Fehler: " + e);
}
}
}