Help with threads

Put your problem here if it does not fit any of the other categories.

Help with threads

Postby misato » Wed Jan 14, 2009 12:13 pm

Hi,
I'm doing a simple IRC client but i'm a little confused about using threads.

Now i've got 4 classes:

- one for the handler (to handle messages obviously),
- another for the UI that has a textEdit where to show everything that comes from the IRC server, as well as everything that you write to it.
- another one for the socket, to connect to the IRC server
- and finally, the "main" class.

This "main" class has a handler object, and launches 2 threads one for the UI class and another for the socket one.
This is done like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ChatUI ui = new ChatUI();
  3.  
  4. ChatSocket socket = new ChatSocket(SERVER_IP, PORT, NICK, USER);
  5.  
  6.                
  7.  
  8. handler = new ChatHandler(socket, ui);
  9.  
  10.        
  11.  
  12. Thread tSocket = new Thread(socket);
  13.  
  14. Thread tUi = new Thread(ui);
  15.  
  16.                
  17.  
  18. tSocket.start();
  19.  
  20. tUi.start();   
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The ChatUI constructor is where i set the layout:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public ChatUI (){
  2.  
  3.                 setContentView(R.layout.main);
  4.  
  5.         text = (TextView) findViewById(R.id.text);
  6.  
  7.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The problem is that my app crashes with a NullPointerException caused when I try to launch the UI thread.
I don't know the best way to do this, and I'll appreciate advice.

Thanks in antticipation.
misato
Freshman
Freshman
 
Posts: 7
Joined: Wed Jan 07, 2009 11:55 am

Top

Postby Patillo » Mon Jan 19, 2009 4:57 pm

It sounds like the thread is being used without being initialized. I can't be of much more help with this amount of code. You may want to check out the activity life cycle because it causes objects to be created in a strange fashion (at least it seems strange from my Win32 experience).
Patillo
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 19, 2009 4:51 pm

Postby Quetzalcoatl » Mon Jan 19, 2009 6:30 pm

Code: Select all
text = (TextView) findViewById(R.id.text);


Is text value being retrieved properly? stick a couple of breakpoints in to see if anything obvious is still NULL.
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests