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 nullpointerexception. 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(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);
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public 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("");
}
}
}
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class MessageReader implements Runnable
{
private TextArea txt;
private DataInputStream DataIn;
public MessageReader(InputStream in, TextArea t)
{
in = DataIn;
t = txt;
}
public void run()
{
try{
txt.append(DataIn.readUTF() + "\n");
DataIn.close();
}catch(IOException t)
{
t.printStackTrace();
}
}
}
import java.awt.event.*;
public 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();
}
}
import java.awt.*;
import java.awt.event.*;
public 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");
}
}
Thank you for ur time.
4 2071
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 - 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");
-
}
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sofia |
last post by:
My name is Sofia and I have for many years been running a personals site,
together with my partner, on a non-profit basis. The site is currently not
running due to us emigrating, but during its...
|
by: Robert Maas, see http://tinyurl.com/uh3t |
last post by:
System login message says PHP is available, so I tried this:
http://www.rawbw.com/~rem/HelloPlus/h.php
It doesn't work at all. Browser just shows the source.
What am I doing wrong?
|
by: Gregory Nans |
last post by:
hello,
i need some help to 'tree-ify' a string...
for example i have strings such as :
s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i
need))"""
and i need to...
|
by: Mike Kamermans |
last post by:
I hope someone can help me, because what I'm going through at the moment
trying to edit XML documents is enough to make me want to never edit XML
again.
I'm looking for an XML editor that has a...
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl...
|
by: Bob.Henkel |
last post by:
I write this to tell you why we won't use postgresql even though we wish we
could at a large company. Don't get me wrong I love postgresql in many
ways and for many reasons , but fact is fact. If...
|
by: Michael R. Pierotti |
last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$")
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a...
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only...
|
by: Alan Mailer |
last post by:
A project I'm working on is going to use VB6 as a front end. The back
end is going to be pre-existing MS Access 2002 database tables which
already have records in them *but do not have any...
|
by: U S Contractors Offering Service A Non-profit |
last post by:
Brilliant technology helping those most in need Inbox
Reply
U S Contractors Offering Service A Non-profit
show details
10:37 pm (1 hour ago)
Brilliant technology helping those most in need ...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |