Exception in thread "FrozenComServerControl" java.lang.NullPointerException - Det er den fejl der har drevet mig til vanvid den sidste time...
Er i gang med at lave en simpel socket baseret chat, IRC agtigt hvor serveren sender clients streng ud til alle connectede clienter.
Fejlen sker da jeg vil have fat i en ArrayList der indeholder samtlige client tråde.
Fejlen ligger i:
- package frozenchat;
-
- import java.net.*;
- import java.io.*;
-
- public class FrozenComServerThread extends FrozenComServerControl {
- private Socket socket = null;
- private PrintWriter out = null;
- private BufferedReader in = null;
-
- public FrozenComServerThread(Socket socket) throws IOException {
- super("FrozenComServerThread");
- this.socket = socket;
- try {
- this.out = new PrintWriter(this.socket.getOutputStream(), true);
- this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
- } catch(IOException e) {
- System.err.println("Couldn't get I/O rights to server: localhost");
- System.exit(1);
- }
-
- }
-
- public void write(String aString) {
- this.out.println(aString);
- }
-
- public void run() {
- try {
- String inputString, outputString;
- this.write("Welcome User!");
-
- while((inputString = this.in.readLine()) != null) {
- if(inputString.equals("Bye!"))
- {
- this.write("Bye!");
- break;
- }
- for(int i = 0, length = super.allClients.size(); i < length; i++) // <------------ DET ER HER DEN BROKKER SIG!
- super.allClients.get(i).write(inputString);
- }
-
- out.close();
- in.close();
- socket.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- }
Og:
- package frozenchat;
- import java.net.*;
- import java.io.*;
- import java.util.*;
-
- public class FrozenComServerControl extends Thread {
- public ArrayList<FrozenComServerThread> allClients = null;
-
- public FrozenComServerControl() throws IOException {
- ServerSocket serverSocket = null;
- this.allClients = new ArrayList<FrozenComServerThread>();
- boolean listening = true;
- try {
- serverSocket = new ServerSocket(4444);
- } catch(IOException e) {
- System.err.println("Couldn't listen to port: 4444");
- System.exit(1);
- }
-
- while(listening)
- {
- FrozenComServerThread aThread = new FrozenComServerThread(serverSocket.accept());
- aThread.start();
- this.allClients.add(aThread);
-
- }
-
- serverSocket.close();
- }
-
- public FrozenComServerControl(String aString) {
- super("FrozenComServerControl");
- }
- }
Sig endelig til hvis i har brug for flere filer at se på.
Indlæg senest redigeret d. 14.11.2009 00:32 af Bruger #11328