Nullpointerexception beim Start

einfach den Code in \[java\]
Code
bla bla bla
\[/java\]

Ohne die Backslashes einfügen
 
das ist vom chatserver
Code:
import java.net.*;
import java.io.*;
import java.util.*;

public class chatserver implements Runnable
{
	public static final int PORT = 8765;
	protected ServerSocket listen;
	protected Vector<connection> connections;
	Thread connect;

	public chatserver()
	{
		try
		{
			listen = new ServerSocket(PORT);
		} catch (IOException e)
		{
			System.err.println("Fehler beim Erzeugen der Sockets:"+e);
			System.exit(1);
		}

		connections = new Vector<connection>();

		connect = new Thread(this);
		connect.start();
	}

	public void run()
	{
		try
		{
			while(true)
			{
				Socket client=listen.accept();

				connection c = new connection(this, client);
				connections.addElement(c);
			}
		} catch (IOException e)
		{
			System.err.println("Fehler beim Warten auf Verbindungen:"+e);
			System.exit(1);
		}
	}

	public static void main(String[] args)
	{
		new chatserver();
	}

	public void broadcast(String msg)
	{
		int i;
		connection you;

		for (i=0; i<connections.size(); i++)
		{
			you = connections.elementAt(i);
			you.out.println(msg);
		}
	}
}

chatapplet
Code:
import java.net.*;
import java.io.*;
import java.util.*;

public class chatserver implements Runnable
{
	public static final int PORT = 8765;
	protected ServerSocket listen;
	protected Vector<connection> connections;
	Thread connect;

	public chatserver()
	{
		try
		{
			listen = new ServerSocket(PORT);
		} catch (IOException e)
		{
			System.err.println("Fehler beim Erzeugen der Sockets:"+e);
			System.exit(1);
		}

		connections = new Vector<connection>();

		connect = new Thread(this);
		connect.start();
	}

	public void run()
	{
		try
		{
			while(true)
			{
				Socket client=listen.accept();

				connection c = new connection(this, client);
				connections.addElement(c);
			}
		} catch (IOException e)
		{
			System.err.println("Fehler beim Warten auf Verbindungen:"+e);
			System.exit(1);
		}
	}

	public static void main(String[] args)
	{
		new chatserver();
	}

	public void broadcast(String msg)
	{
		int i;
		connection you;

		for (i=0; i<connections.size(); i++)
		{
			you = connections.elementAt(i);
			you.out.println(msg);
		}
	}
}
und connection
Code:
import java.net.*;
import java.io.*;

class connection extends Thread
{
	protected Socket client;
	protected DataInputStream in;
	protected PrintStream out;
	protected chatserver server;

	public connection(chatserver server, Socket client)
	{
		this.server=server;
		this.client=client;

		try
		{
			in = new DataInputStream(client.getInputStream());
			out = new PrintStream(client.getOutputStream());
		} catch (IOException e)
		{
			try { client.close(); } catch (IOException e2) {} ;
			System.err.println("Fehler beim Erzeugen der Streams: " + e);
			return;
		}

		this.start();
	}


	@SuppressWarnings("deprecation")
	public void run()
	{
		String line;

		try
		{
			while(true)
			{
				line=in.readLine();
				if(line!=null)
					server.broadcast(line);
			}
		} catch (IOException e)
		{
			System.out.println("Fehler:" + e);
		}
	}
}
 
Einfach die Klasse chatserver öffnen und auf den Run Pfeil (falls du Eclipse verwendest)
Den Client auch auf dem selben Weg starten und alles läuft ganz einwandfrei.
 
Hast du auch ein richtiges Eclipse für Java und JRE installiert?
Sonst müsste es normalerweise den grünen Pfeil geben oder Run --> Run oder Str+ F11
 

Neue Beiträge

Zurück