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.
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.
Comment