I keep getting an error when i run the following code.
Code:
public class Main
{
public static void main(String[] args) throws Exception
{
Server serv = new Server(0);
Moo a = new Moo(serv.getConnect());
}
}
Server.java contains
Code:
import java.io.*;
import java.net.*;
public class Server extends Thread
{
public static Connections sockets;
public final static int DEFAULT_PORT = 6789;
protected int port;
protected ServerSocket listen_socket;
public static void fail(Exception e, String msg)
{
System.err.println(msg + ": " + e);
System.exit(1);
}
public Server(int port) throws Exception
{
if (port == 0) port = DEFAULT_PORT;
this.port = port;
try { listen_socket = new ServerSocket(port); }
catch (IOException e) { fail(e, "Exception creating server socket"); }
System.out.println("Server:"+InetAddress.getLocalHost().getHostAddress()+":"+port);
this.start();
}
public String getConnect() throws Exception
{
return InetAddress.getLocalHost().getHostAddress();
}
public void run()
{
try
{
while(true)
{
Socket client_socket = listen_socket.accept();
Client a = new Client(client_socket);
sockets.add(a);
}
}
catch (IOException e) {
fail(e, "Exception while listening for connections");
}
}
public static void main(String[] args) throws Exception
{
sockets = new Connections();
int port = 0;
if (args.length == 1) {
try { port = Integer.parseInt(args[0]); }
catch (NumberFormatException e) { port = 0; }
}
new Server(port);
}
}
class Connections extends Thread
{
protected Socket send;
protected PrintStream out;
static Client[] clients;
static int x = 0;
public Connections()
{
clients = new Client[100];
}
public static boolean add(Client data)
{
if(x<100)
{
clients[x] = data;
x++;
System.out.println("Added:"+data.getIP());
return true;
}
System.out.println("To many connections");
return false;
}
public void send(String data,String ip2)
{
System.out.println(ip2+":"+data);
for(int i = 0;i < x;i++)
{
if(clients[i] != null)
{
clients[i].send(data);
}
}
}
}
class Client extends Thread
{
protected Socket client;
protected DataInputStream in;
protected PrintStream out;
public Client(Socket client_socket)
{
client = client_socket;
try
{
in = new DataInputStream(client.getInputStream());
out = new PrintStream(client.getOutputStream());
}
catch (IOException e)
{
try
{
client.close();
}catch (IOException e2){ }
System.err.println("Exception while getting socket streams: " + e);
return;
}
this.start();
}
public void send(String data)
{
out.println(data);
}
public String getIP()
{
return client.getInetAddress().getHostAddress();
}
public void run()
{
String line;
StringBuffer revline;
int len;
try {
while(true)
{
line = in.readLine();
if (line == null) break;
Server.sockets.send(line,getIP());
}
}
catch (IOException e){ ; }
finally { try {client.close();} catch (IOException e2) {;} }
}
}
Moo.java contains
Code:
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class Moo extends Thread
{
static String name;
public static final int DEFAULT_PORT = 6789;
static public MooFrame frame;
DataInputStream sin;
PrintStream out;
static Send outer;
public Moo(String host)
{
frame = new MooFrame();
frame.setVisible(true);
int port = DEFAULT_PORT;
name = JOptionPane.showInputDialog("Chat Name?");
Socket s = null;
try
{
s = new Socket(host,port);
sin = new DataInputStream(s.getInputStream());
out = new PrintStream(s.getOutputStream());
frame.setText("Connected to " + s.getInetAddress()+ ":"+ s.getPort());
outer = new Send(s,name);
this.start();
}
catch(Exception e)
{
frame.setText("Error:Connection to server closed");
}
}
public void run()
{
try
{
while(true)
{
String line = "";
line = sin.readLine();
if (line == null)
{
System.out.println("Connection closed by server.");
break;
}
frame.setText(line);
}
}catch(Exception e) { }
}
public static void main(String[] args)
{
Moo a = new Moo("localhost");
}
}
class Send extends Thread
{
protected Socket server;
protected PrintStream out;
protected String nick;
public Send(Socket server_socket,String name) throws Exception
{
server = server_socket;
out = new PrintStream(server.getOutputStream());
nick = name;
}
public void send(String a)
{
try
{
System.out.println(a);
out.print(nick+": "+a);
}
catch (Exception e) { }
}
}
class MooFrame extends Frame
{
TextArea output;
TextField input;
boolean send = false;
String sendjunk;
public MooFrame()
{
GridLayout moo = new GridLayout(2,1);
BorderLayout border = new BorderLayout();
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
MooFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Chat Client");
setMenuBar(menuBar);
setSize(new Dimension(400, 400));
Panel moo2 = new Panel(moo);
output = new TextArea("", 5, 40);
output.setEditable(false);
input = new TextField();
input.addKeyListener
(
new KeyListener()
{
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
int i = e.getKeyCode();
if(i == 10)
{
Moo.outer.send(input.getText()+"\n");;
}
}
public void keyReleased(KeyEvent e)
{
}
}
);
moo2.add(output);
moo2.add(input);
this.add(moo2);
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MooFrame.this.windowClosed();
}
}
);
}
public String getText()
{
return output.getText();
}
public void setText(String data)
{
output.append(data+"\n");
}
protected void windowClosed()
{
System.exit(0);
}
}
When i run them seperatly it works ok but when i run them using the Main class is says this
Exception in thread "Thread-0" java.lang.NullPointerException
at Connections.add(Server.java:71)
at Server.run(Server.java:37)
Any help?