473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need help on I/O

4
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.
Oct 27 '06 #1
4 2071
Hiten
4
No one can help me?
Oct 30 '06 #2
Hiten
4
No one can help me?
Oct 30 '06 #3
r035198x
13,262 8TB
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
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6.  
  7. public class ChatWindow extends Frame
  8. {
  9. private TextField text;
  10. private TextArea t;
  11. private Button clear, send, quit;
  12.  
  13. public ChatWindow(String title)
  14. {
  15.  
  16. super(title);
  17. try{
  18. clear = new Button("Clear");
  19. send = new Button("Send");
  20. quit = new Button("Quit");
  21. Panel left = new Panel();
  22. left.setLayout(new BoxLayout(left,BoxLayout.Y_AXIS));
  23. left.add(clear);
  24. left.add(send);
  25. left.add(quit);
  26. this.add(left, BorderLayout.WEST);
  27.  
  28. Panel bottom = new Panel();
  29. Choice choice = new Choice();
  30. choice.add("brb");
  31. choice.add("lol");
  32. choice.add("cul8r");
  33.  
  34. text = new TextField("", 55);
  35. bottom.add(choice);
  36. bottom.add(text);
  37. this.add(bottom, BorderLayout.SOUTH);
  38. bottom.setLayout(new FlowLayout(FlowLayout.LEFT));
  39.  
  40. Panel middle = new Panel();
  41.  
  42. t = new TextArea();
  43. t.setEditable(false);
  44. middle.add(t);
  45. this.add(middle, BorderLayout.CENTER);
  46.  
  47. File file = new File("chat.txt");
  48.  
  49. FileOutputStream FileOut = new FileOutputStream(file);
  50. FileInputStream FileIn = new FileInputStream(file);
  51.  
  52. //MessageReader mr = new MessageReader(FileIn, t);
  53.  
  54. SendAndClearHandler d = new SendAndClearHandler(FileOut,t,text);
  55.  
  56. send.addActionListener(d);
  57. clear.addActionListener(d);
  58. text.addActionListener(d);
  59.  
  60. //Thread th = new Thread(mr);
  61. //th.start();
  62.  
  63. ChoiceHandler c = new ChoiceHandler(t);
  64. choice.addItemListener(c);
  65.  
  66. QuitChat q = new QuitChat();
  67. quit.addActionListener(q);
  68. this.addWindowListener(q);
  69.  
  70. }catch(IOException e)
  71. {
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. public TextField getTextField()
  77. {
  78. return text;
  79. }
  80.  
  81. public static void main (String [] args)
  82. {
  83. ChatWindow C = new ChatWindow("Chat Window");
  84. C.pack();
  85. C.setVisible(true);
  86. }
  87. }
  88.  
  89.  
  90. class SendAndClearHandler implements ActionListener
  91. {
  92.  
  93. private TextField textF;
  94. private TextArea textA;
  95. private DataOutputStream DataOut;
  96.  
  97. public SendAndClearHandler(OutputStream out, TextArea ta, TextField tf)
  98. {
  99. out = DataOut;
  100. ta = textA;
  101. tf = textF;
  102. }
  103.  
  104. public void actionPerformed(ActionEvent e)
  105. {
  106.  
  107. Object component = e.getSource();
  108. String label = e.getActionCommand();
  109.  
  110. if(component instanceof TextField || label.equals("Send"))
  111. {
  112. try{
  113. DataOut.writeUTF(textF.getText());
  114. //textA.append(text + "\n");
  115. textF.setText("");
  116. DataOut.close();
  117. }catch(IOException t)
  118. {
  119. t.printStackTrace();
  120. }
  121. }
  122.  
  123. else if(label.equals("Clear"))
  124. {
  125. textA.setText("");
  126. }
  127. }
  128. }
  129.  
  130.  
  131. class MessageReader implements Runnable
  132. {
  133. //private TextArea txt;
  134. //private DataInputStream DataIn;
  135.  
  136. //public MessageReader(InputStream in, TextArea t)
  137. //{
  138. //in = DataIn;
  139. //t = txt;
  140. //}
  141.  
  142. public void run(){
  143.  
  144. //MessageReader mr = new MessageReader(FileIn, t);
  145.  
  146. try{
  147.     File file = new File("chat.txt");
  148.  
  149.     FileOutputStream FileOut = new FileOutputStream(file);
  150.     FileInputStream FileIn = new FileInputStream(file);
  151. DataInputStream DataIn= new DataInputStream(FileIn);
  152. new TextArea().append(DataIn.readUTF() + "\n");
  153. DataIn.close();
  154. }catch(IOException t)
  155. {
  156. t.printStackTrace();
  157. }
  158. }
  159. }
  160.  
  161.  
  162.  
  163. class QuitChat extends WindowAdapter implements ActionListener
  164. {
  165. public void quit()
  166. {
  167. System.exit(0);
  168. }
  169.  
  170. public void windowClosing(WindowEvent e)
  171. {
  172. this.quit();
  173. }
  174.  
  175. public void actionPerformed(ActionEvent a)
  176. {
  177. this.quit();
  178. }
  179. }
  180.  
  181.  
  182.  
  183. class ChoiceHandler implements ItemListener
  184. {
  185. private TextArea t;
  186.  
  187. public ChoiceHandler(TextArea text)
  188. {
  189. t = text;
  190.  
  191. }
  192.  
  193. public void itemStateChanged(ItemEvent e)
  194. {
  195. Object source = e.getSource();
  196. if(!(source instanceof Choice))
  197. {
  198. return;
  199. }
  200. Choice list = (Choice) source;
  201. String selected = list.getSelectedItem();
  202. t.append(selected + "\n");
  203. }
  204. }
Oct 30 '06 #4
Hiten
4
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.
Oct 31 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
6
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?
0
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...
7
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...
8
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...
3
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...
2
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...
8
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...
11
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...
0
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 ...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.