Need help on I/O

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hiten
    New Member
    • Oct 2006
    • 4

    #1

    Need help on I/O

    Hello all.

    I have been successful in making a chat program, without streaming the data. However, now that I have replaced the program with input and output streams, I keep getting a nullpointerexce ption. Its most probably to do with the readUTF and writeUTF methods, I have tried everything with no luck. Can someone guide me as to where I am going wrong, I'm fairly new to java, so any help will be greatly appreciated.

    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    public class ChatWindow extends Frame
    {
    private TextField text;
    private TextArea t;
    private Button clear, send, quit;

    public ChatWindow(Stri ng title)
    {

    super(title);
    try{
    clear = new Button("Clear") ;
    send = new Button("Send");
    quit = new Button("Quit");
    Panel left = new Panel();
    left.setLayout( new BoxLayout(left, BoxLayout.Y_AXI S));
    left.add(clear) ;
    left.add(send);
    left.add(quit);
    this.add(left, BorderLayout.WE ST);

    Panel bottom = new Panel();
    Choice choice = new Choice();
    choice.add("brb ");
    choice.add("lol ");
    choice.add("cul 8r");

    text = new TextField("", 55);
    bottom.add(choi ce);
    bottom.add(text );
    this.add(bottom , BorderLayout.SO UTH);
    bottom.setLayou t(new FlowLayout(Flow Layout.LEFT));

    Panel middle = new Panel();

    t = new TextArea();
    t.setEditable(f alse);
    middle.add(t);
    this.add(middle , BorderLayout.CE NTER);

    File file = new File("chat.txt" );

    FileOutputStrea m FileOut = new FileOutputStrea m(file);
    FileInputStream FileIn = new FileInputStream (file);

    MessageReader mr = new MessageReader(F ileIn, t);

    SendAndClearHan dler d = new SendAndClearHan dler(FileOut,t, text);

    send.addActionL istener(d);
    clear.addAction Listener(d);
    text.addActionL istener(d);

    Thread th = new Thread(mr);
    th.start();

    ChoiceHandler c = new ChoiceHandler(t );
    choice.addItemL istener(c);

    QuitChat q = new QuitChat();
    quit.addActionL istener(q);
    this.addWindowL istener(q);

    }catch(IOExcept ion e)
    {
    e.printStackTra ce();
    }
    }

    public TextField getTextField()
    {
    return text;
    }

    public static void main (String [] args)
    {
    ChatWindow C = new ChatWindow("Cha t Window");
    C.pack();
    C.setVisible(tr ue);
    }
    }

    import java.io.*;
    import java.awt.*;
    import java.awt.event. *;

    public class SendAndClearHan dler implements ActionListener
    {

    private TextField textF;
    private TextArea textA;
    private DataOutputStrea m DataOut;

    public SendAndClearHan dler(OutputStre am out, TextArea ta, TextField tf)
    {
    out = DataOut;
    ta = textA;
    tf = textF;
    }

    public void actionPerformed (ActionEvent e)
    {

    Object component = e.getSource();
    String label = e.getActionComm and();

    if(component instanceof TextField || label.equals("S end"))
    {
    try{
    DataOut.writeUT F(textF.getText ());
    //textA.append(te xt + "\n");
    textF.setText(" ");
    DataOut.close() ;
    }catch(IOExcept ion t)
    {
    t.printStackTra ce();
    }
    }

    else if(label.equals ("Clear"))
    {
    textA.setText(" ");
    }
    }
    }

    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    public class MessageReader implements Runnable
    {
    private TextArea txt;
    private DataInputStream DataIn;

    public MessageReader(I nputStream in, TextArea t)
    {
    in = DataIn;
    t = txt;
    }

    public void run()
    {
    try{
    txt.append(Data In.readUTF() + "\n");
    DataIn.close();
    }catch(IOExcept ion t)
    {
    t.printStackTra ce();
    }
    }
    }

    import java.awt.event. *;

    public class QuitChat extends WindowAdapter implements ActionListener
    {
    public void quit()
    {
    System.exit(0);
    }

    public void windowClosing(W indowEvent e)
    {
    this.quit();
    }

    public void actionPerformed (ActionEvent a)
    {
    this.quit();
    }
    }

    import java.awt.*;
    import java.awt.event. *;

    public class ChoiceHandler implements ItemListener
    {
    private TextArea t;

    public ChoiceHandler(T extArea text)
    {
    t = text;

    }

    public void itemStateChange d(ItemEvent e)
    {
    Object source = e.getSource();
    if(!(source instanceof Choice))
    {
    return;
    }
    Choice list = (Choice) source;
    String selected = list.getSelecte dItem();
    t.append(select ed + "\n");
    }
    }

    Thank you for ur time.
  • Hiten
    New Member
    • Oct 2006
    • 4

    #2
    No one can help me?

    Comment

    • Hiten
      New Member
      • Oct 2006
      • 4

      #3
      No one can help me?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Hiten
        No one can help me?
        Ok now that you insist, NullPointer has nothing to do with readUTF method(You can check the docs if you want). The problem must be the way you are using your thread like any other class which is worse than dangerous. Look for example at the following modification of your code which should suggest that either the text area or the input stream itself is the one giving null
        Code:
        import java.io.*;
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        
        
        public class ChatWindow extends Frame
        {
        private TextField text;
        private TextArea t;
        private Button clear, send, quit;
        
        public ChatWindow(String title)
        {
        
        super(title);
        try{
        clear = new Button("Clear");
        send = new Button("Send");
        quit = new Button("Quit");
        Panel left = new Panel();
        left.setLayout(new BoxLayout(left,BoxLayout.Y_AXIS));
        left.add(clear);
        left.add(send);
        left.add(quit);
        this.add(left, BorderLayout.WEST);
        
        Panel bottom = new Panel();
        Choice choice = new Choice();
        choice.add("brb");
        choice.add("lol");
        choice.add("cul8r");
        
        text = new TextField("", 55);
        bottom.add(choice);
        bottom.add(text);
        this.add(bottom, BorderLayout.SOUTH);
        bottom.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        Panel middle = new Panel();
        
        t = new TextArea();
        t.setEditable(false);
        middle.add(t);
        this.add(middle, BorderLayout.CENTER);
        
        File file = new File("chat.txt");
        
        FileOutputStream FileOut = new FileOutputStream(file);
        FileInputStream FileIn = new FileInputStream(file);
        
        //MessageReader mr = new MessageReader(FileIn, t);
        
        SendAndClearHandler d = new SendAndClearHandler(FileOut,t,text);
        
        send.addActionListener(d);
        clear.addActionListener(d);
        text.addActionListener(d);
        
        //Thread th = new Thread(mr);
        //th.start();
        
        ChoiceHandler c = new ChoiceHandler(t);
        choice.addItemListener(c);
        
        QuitChat q = new QuitChat();
        quit.addActionListener(q);
        this.addWindowListener(q);
        
        }catch(IOException e)
        {
        e.printStackTrace();
        }
        }
        
        public TextField getTextField()
        {
        return text;
        }
        
        public static void main (String [] args)
        {
        ChatWindow C = new ChatWindow("Chat Window");
        C.pack();
        C.setVisible(true);
        }
        }
        
        
        class SendAndClearHandler implements ActionListener
        {
        
        private TextField textF;
        private TextArea textA;
        private DataOutputStream DataOut;
        
        public SendAndClearHandler(OutputStream out, TextArea ta, TextField tf)
        {
        out = DataOut;
        ta = textA;
        tf = textF;
        }
        
        public void actionPerformed(ActionEvent e)
        {
        
        Object component = e.getSource();
        String label = e.getActionCommand();
        
        if(component instanceof TextField || label.equals("Send"))
        {
        try{
        DataOut.writeUTF(textF.getText());
        //textA.append(text + "\n");
        textF.setText("");
        DataOut.close();
        }catch(IOException t)
        {
        t.printStackTrace();
        }
        }
        
        else if(label.equals("Clear"))
        {
        textA.setText("");
        }
        }
        }
        
        
        class MessageReader implements Runnable
        {
        //private TextArea txt;
        //private DataInputStream DataIn;
        
        //public MessageReader(InputStream in, TextArea t)
        //{
        //in = DataIn;
        //t = txt;
        //}
        
        public void run(){
        
        //MessageReader mr = new MessageReader(FileIn, t);
        
        try{
        	File file = new File("chat.txt");
        
        	FileOutputStream FileOut = new FileOutputStream(file);
        	FileInputStream FileIn = new FileInputStream(file);
        DataInputStream DataIn= new DataInputStream(FileIn);
        new TextArea().append(DataIn.readUTF() + "\n");
        DataIn.close();
        }catch(IOException t)
        {
        t.printStackTrace();
        }
        }
        }
        
        
        
        class QuitChat extends WindowAdapter implements ActionListener
        {
        public void quit()
        {
        System.exit(0);
        }
        
        public void windowClosing(WindowEvent e)
        {
        this.quit();
        }
        
        public void actionPerformed(ActionEvent a)
        {
        this.quit();
        }
        }
        
        
        
        class ChoiceHandler implements ItemListener
        {
        private TextArea t;
        
        public ChoiceHandler(TextArea text)
        {
        t = text;
        
        }
        
        public void itemStateChanged(ItemEvent e)
        {
        Object source = e.getSource();
        if(!(source instanceof Choice))
        {
        return;
        }
        Choice list = (Choice) source;
        String selected = list.getSelectedItem();
        t.append(selected + "\n");
        }
        }

        Comment

        • Hiten
          New Member
          • Oct 2006
          • 4

          #5
          Thank you, I really appreciate your help. I haven't got time now, but will try and modify the code when I get a chance then get back to you.

          Comment

          Working...