Hiho, mein Chat funzt nich wie er soll. Ich krieg immer ne NullPointerException und hab keinen Schimmer warum...
hier erstma der Code vom Server:
Java
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 connections;
Thread connect;
String[] memberlist;
int memberlistcounter;
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();
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 = (connection) connections.elementAt(i);
you.out.println(msg);
}
}
public void addtomemberlist(String name)
{
connection you;
System.out.println(name); // hier gibt er mir auch den richtigen Text aus..
memberlist[memberlistcounter] = name; // hier bekomm ich ne NullPointerException
int i;
int x;
for (i = 0; i < connections.size(); i++)
{
you = (connection) connections.elementAt(i);
you.out.println("+-+newlist+-+");
for (x = 0; x == memberlistcounter++; x++)
you.out.println(memberlist[x]);
}
memberlistcounter++;
}
}
Alles anzeigen
hier die connection-klasse:
Java
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();
}
public void run()
{
String line;
try
{
while(true)
{
line = in.readLine();
if (line.charAt(0) != '<' && line != null) // line kann doch garnich null sein?!
server.addtomemberlist(line); // hier bekomm ich auch ne NullPointerException
else
{
if(line != null)
server.broadcast(line);
}
}
} catch (IOException e)
{
System.out.println("Fehler:" + e);
}
}
}
Alles anzeigen
hier das dazugehörige Applet:
Java
import java.net.*;
import java.io.*;
import java.awt.*;
import java.applet.*;
public class chatapplet extends Applet implements Runnable
{
public static final int PORT = 8765;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
TextArea memberlist;
Thread thread;
public void init()
{
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
memberlist = new TextArea();
memberlist.setFont( new Font("Dialog", Font.PLAIN, 12));
memberlist.setEditable(false);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("West", outputarea);
this.add("Center", memberlist);
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
inputfield.setBackground(Color.white);
outputarea.setBackground(Color.white);
memberlist.setBackground(Color.yellow);
}
public void start()
{
String username = this.getParameter("username");
if (username == null)
username = "Admin";
String greeting = "Greetings " + username;
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Connecting failed!");
System.exit(1);
}
say("Connection established.");
say(greeting);
if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
out.println(username);
}
public void stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public void run()
{
String line;
try
{
while(true)
{
line = in.readLine();
if (line == "+-+newlist+-+")
{
memberlist.repaint();
break;
}
if (line.charAt(0) != '<')
{
memberlist.append(line+'\n');
}
else
{
if(line != null)
outputarea.appendText(line+'\n');
}
}
} catch (IOException e) { say("Lost connection."); }
}
public boolean action(Event e, Object what)
{
String username = "<" + this.getParameter("username") + "> ";
if (e.target==inputfield)
{
String inp = username + (String) e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public void say(String msg)
{
outputarea.appendText("*** "+msg+" ***\n");
}
}
Alles anzeigen
Sinn des Ganzen ist, wenn sich ein User einloggt der Username über die connection-Klasse an den Chatserver weitergegeben wird, welcher darauß einen Array aus Strings macht und den dann wieder an die Clienten austeilt.
Nachrichten versenden funktioniert aber das mit der Memberliste eben nicht, weil er irgendwie der Meinung ist, dass die Variable "name" null ist.
Kann mir wer helfen?