What line does the debug catch the error? Have you stepped through to see what line causes the exception to throw?
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of raqz Sent: Thursday, April 08, 2010 1:37 AM To: Android Developers Subject: [android-developers] Trying to start a thread dedicated to sending and receiving data Hi, I have a main class activity that does the task of sending control to other activities on click of a button. I am also starting a thread in the main class. This thread basically sets up a connection with the server and is active throughout the lifetime of the application. I have two functions in the thread class, send and receive data from server. Therefore, when the other activities need to send/receive data from server, they use those particular functions. First of all please let me know if what I am trying to do is fine as per the architecture of the operating system. If its fine, when I try to run the code, the app crashes and i see a NullPointerException and a RuntimeException in the DDMS log. Could some one please help me out in this or suggest a better way to implement the concept. Also, please be assured that, the other functionality of the code works perfectly fine. The main class code is as below [code] package com.getfriends; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class GetFriendsActivity extends Activity{ private MyFriend obj= new MyFriend(); List<MyFriend> listOfFriends = new ArrayList<MyFriend>(); int i=0; private String FName=null; private String LName=null; private String Latitude=null; private String Longitude=null; private String TimeStamp=null; public static ArrayList<String> namesArray=new ArrayList<String>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Thread cThread= null; try{ cThread = new Thread(new ConnectSocket()); cThread.start();} catch (Exception e){ Toast.makeText(getBaseContext(), "Unable to start thread", Toast.LENGTH_LONG).show(); } Toast.makeText(getBaseContext(), "Starting", Toast.LENGTH_LONG).show(); try { getFriendsList(); } catch (Exception e) { Toast.makeText(getBaseContext(), "Unable to fetch friend list:"+e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } try { displayFriendsList(); } catch (Exception e) { Toast.makeText(getBaseContext(), "Unable to display friend list:"+e.getMessage() +e.getLocalizedMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } ConnectSocket socket = new ConnectSocket(); socket.sendData("ABCDEF"); String k = socket.recieveData(); Toast.makeText(getBaseContext(), "Recieved from server:"+k, Toast.LENGTH_LONG).show(); } private void getFriendsList() { String line[] = new String[3]; line[0]= "Abdul#Raqeeb#23.44#34.44#4.45"; line[1]= "Abdul#Azeez#33.44#44.44#5.45"; line[2]= "Kiral#Azeez#53.44#454.44#6.45"; for(int k=0;k<=2;k++){ StringTokenizer tokens=new StringTokenizer(line[k],"#"); try{ while(tokens.hasMoreTokens()) { FName=tokens.nextToken(); LName=tokens.nextToken(); Latitude=tokens.nextToken(); Longitude=tokens.nextToken(); TimeStamp=tokens.nextToken(); listOfFriends.add(new MyFriend(FName,LName,Latitude,Longitude,TimeStamp)); } } catch (Exception e){ Toast.makeText(this, "Some prob here:"+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } } } [/code] The code related to the thread is [code] package com.getfriends; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.Socket; import android.util.Log; public class ConnectSocket implements Runnable{ /** Called when the activity is first created. */ public String fromServer=null; public static BufferedReader inFromServer=null; public static Socket clientSocket=null; public InetAddress address =null; public static DataOutputStream outToServer=null; @Override public void run(){ try { address = InetAddress.getByName("127.0.0.1"); Log.d("TCP", "C: Connecting..."); clientSocket = new Socket(address, 9999); outToServer = new DataOutputStream(clientSocket.getOutputStream()); inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); } catch (Exception e) { System.out.println("Not Connected to the internet"); } } public void sendData(String s){ try { if(s.equals(null)) s="hello"; Log.d("TCP", "C: Sending..."); outToServer.writeBytes(s+'#'); outToServer.flush(); } catch (IOException e) { // TODO Auto-generated catch block Log.d("TCP", "unable to send msg"); e.printStackTrace(); } } public String recieveData(){ try { fromServer = inFromServer.readLine(); if(fromServer.equals(null)) fromServer="bad luck"; } catch (IOException e) { // TODO Auto-generated catch block Log.d("TCP", "unable to send msg"); e.printStackTrace(); } return fromServer; } } [/code] Many thanks, Raqeeb -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

