Error help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Error help

  1. #1
    Join Date
    Jan 2006
    Posts
    7

    Error help

    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?

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Looks to me like sockets was never initialized before the run() method. It's getting initialized in the main(String[]) method, but I think the run() method runs first. Not sure.
    Print messages to the console in run and the main method and see which comes first.

    Good luck!

  3. #3
    Join Date
    Jan 2006
    Posts
    7
    Gah I Am Such An Idiot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center