-2

I wanted to do a multithreaded ChatServer, which is based on telnet and I thought I got it but after I type in 1 message in telnet it gives the following error

"Exception in thread "Thread-0" java.lang.NullPointerException

at Server.sendAll(Server.java:20)

at ServerThreads.run(ServerThreads.java:33)"

I know where it causes the problem but I don't know how I can fix it specifically in my code. I think all the other stuff is working fine. This is my ServerThread Class

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class ServerThreads extends Thread {

    private String prefix;
    private Socket socket;
    PrintWriter pw;

    public ServerThreads(String prefix, Socket s){
        this.prefix = prefix;
        this.socket = s;
        this.start();
    }

    public void run(){

        try {
            PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            Server.list.einfuegen(0, this);
            pw.println("Client verbunden mit: "+prefix);
            
            while(true){
                //System.out.println("User: "+ socket.getInetAddress() + " Nachricht: " + line);
                String line = br.readLine();
                Server.sendAll(line, prefix);
                pw.flush();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

This is my Server Class

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {
    static EinfachListe list = new EinfachListe();
    static int zaehler = 1;
    private int port;
    ServerSocket socket;

    public Server(int port) throws IOException{
        this.port = port;
        socket = new ServerSocket(port);
    }

    static public void sendAll(String s, String prefix){
        for(int a = 0; a<list.getAnzahl(); a++){
            ((ServerThreads) (list.get(a))).pw.println(s);
        }
    }

    public void starteServer(){
        System.out.println("Der Server wurde gestartet");

        while(true){
            try {
                Socket s = socket.accept();
                //ServerThreads user = new ServerThreads("User:Anzahl Nachrichten "+zaehler++, 1000, s);
                String client = "Client"+zaehler++;
                new ServerThreads(client, s);
                //user.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

I don't think that the mistake is in my LinkedList but if you think it is I can also copy the code of my LinkedList in here. Thank you for the help!

Community
  • 1
  • 1
Tom.TT
  • 1
  • 1

1 Answers1

0

Either your list.get(a) returns null or the corresponding ServerThread is not running - in which case its pw would still be null causing your Exception.

Jan
  • 13,738
  • 3
  • 30
  • 55