Results 1 to 4 of 4
  1. #1
    Akanam is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default NullPointerException from java.awt.Container

    Hi,

    I'm stuck at this:

    Throwing an exception:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.awt.Container.addImpl(Unknown Source)
    	at java.awt.Container.add(Unknown Source)
    	at code.Chat.<init>(Chat.java:59)
    	at code.ChatTest.main(ChatTest.java:10)
    Source:

    Java Code:
    package code;
    
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.EOFException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
     
    
    public class Chat extends JFrame
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	ObjectOutputStream outputstream;
    	ObjectInputStream inputstream;
    
    	Container bottom = new Container();
    	JButton send;
    	JTextArea text;
    	JTextField input;
    	 
    	ServerSocket server;	
    	Socket connection;
     
        public Chat()
        {
        	
            super("Chat: Supadubachat");
            
            setLocation(100,100);
            setSize(800,600);
            setResizable(false);
            setLayout(new BorderLayout());
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            // Components
            send = new JButton("Send");
            //nickList = new JTextField("Nicklist");
            
            // Defaults
            //input.setEditable(false);
            //text.setEditable(false);
            
            // Adding componentsbottom.setLayout(new BorderLayout());
            
    
            	bottom.add(input, BorderLayout.CENTER);
                bottom.add(send, BorderLayout.EAST);
                add(text, BorderLayout.CENTER);
                add(bottom, BorderLayout.SOUTH);
            
            
            // Add action listeners
            input.addActionListener(
            		new ActionListener() {
            			public void actionPerformed(ActionEvent event)
            			{
            				sendMessage(event.getActionCommand());
            				input.setText("");
            			}
            		}
             );
            
            // Show everything
            // getContentPane().add(new PaintableArea());
            setVisible(true);
        }
    
    // send message to client
    
    private void sendMessage(String message) 
    {
    	try
    	{
    		outputstream.writeObject("Alex: "+ message);
    		outputstream.flush();
    		showMessage("\nAlex: "+ message);
    	}
    	
    	catch(IOException ioException)
    	{
    		text.append("\n Error: Dude I cant send that message");
    	}
    }
    
    // updates chat window
    
    private void showMessage(final String textmsg)
    {
    	SwingUtilities.invokeLater(
    			new Runnable()
    			{
    				public void run()
    				{
    					text.append(textmsg);
    				}
    			}
    	);
    }
    
    //
    
    // wait for connection, then display connection info
    
    private void waitForConnection() throws IOException
    {
    	showMessage(" Waiting for someone to connect... \n");
    	connection = server.accept();
    	showMessage(" Now connect to " + connection.getInetAddress().getHostName());
    	
    }
    
    // get stream to send and receive data
    
    private void setupStreams() throws IOException
    {
    	outputstream = new ObjectOutputStream(connection.getOutputStream());
    	outputstream.flush();
    	inputstream = new ObjectInputStream(connection.getInputStream());
    	showMessage("\n Streams are now setup! \n");
    }
    
    // let user type stuff
    
    private void ableToType(final boolean tof)
    {
    	SwingUtilities.invokeLater(
    			new Runnable()
    			{
    				public void run()
    				{
    					text.setEditable(tof);
    				}
    			}
    	);
    }
    
    // during chatting
    
    private void whileChatting() throws IOException
    {
    	String message = " You are now connected! ";
    	sendMessage(message);
    	
    	ableToType(true);
    	
    	do 
    	{
    		try
    		{
    			message = (String) inputstream.readObject();
    			showMessage("\n" + message);
    		}
    		
    		catch(ClassNotFoundException classNotFoundException)
    		{
    			showMessage("\n idk wtf that user sent!");
    		}
    	} while (!message.equals("CLIENT - END"));
    }
    
    // close streams and sockets after done chatting
    
    private void closeCrap()
    {
    	showMessage("\n Closing connections... \n");
    	ableToType(false);
    	
    	try 
    	{
    		outputstream.close();
    		inputstream.close();
    		connection.close();
    	} 
    	
    	catch(IOException ioException)
    	{
    		ioException.printStackTrace();
    	}
    }
    
    public void startRunning()
    {
        
    	try 
    	{
    		server = new ServerSocket(61619,100);
    		
    		while (true)
    		{
    			try
    			{
    				waitForConnection();
    				setupStreams();
    				whileChatting();
    			}
    			
    			catch (EOFException eofException)
    			{
    				showMessage("\n Server ended the connection!");
    			}
    			
    			finally
    			{
    				closeCrap();
    			}
    		}
    	} 
    	
    	catch(IOException ioException)
    	{
    		ioException.printStackTrace();
    	}
      }
    
    public static void main(String args[])
    {
    	Chat chat = new Chat();
    	chat.startRunning();
    }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: NullPointerException from java.awt.Container

    Something on the offending line is null. Here's the line:

    Java Code:
          bottom.add(input, BorderLayout.CENTER);
    You can test it to see which:

    Java Code:
          System.out.println("bottom is null: " + (bottom == null));
          System.out.println("input is null: " + (input == null));
          bottom.add(input, BorderLayout.CENTER);
    And then the solution should be obvious (d'oh!).

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: NullPointerException from java.awt.Container

    Moved from Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: NullPointerException from java.awt.Container

    You shouldn't be using java.awt.Container in a Swing GUI. Go through the Oracle Swing tutorial trail systematically and learn what to use where.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 05:59 AM
  2. Java Generic Container Question - please help!!!
    By zhoujackji in forum New To Java
    Replies: 3
    Last Post: 11-13-2010, 12:12 PM
  3. Replies: 3
    Last Post: 04-22-2009, 12:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •