Null pointer exception probably due to synchronisation issue

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

Null pointer exception probably due to synchronisation issue

Postby madhunandan » Mon Jan 10, 2011 3:22 pm

Hello,

I have a small app which is the client running on Android emulator. Server (small Java app in Eclipse) keeps sending strings. Client will receive these strings and display an image if a string in the correct format and right contents is received.

I send 3 strings-1st in wrong format, 2nd with wrong value and 3rd in correct format and correct value. First time, all 3 messages were sent and received; information about 1st and 2nd was shown correctly. And for the 3rd message, image should have been shown. But it does not happen and I get the error. I get a warning "Activity pause timeout for HistoryRecord" and then error.

When I try the same thing again, only first 2 messages are received and the 3rd message is not received. This happens randomly. Sometimes all 3 messages are received and sometimes only 2.

Code and LogCat below.

Appreciate any help/suggestion on this.

Thank you for time spent and efforts put on this.

Cheers,
Madhu

Main activity

Code: Select all
public class TCPListen extends Activity implements TCPListener {
   private TextView mTitle;
   
    /** Called when the activity is first created. */
    @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            //setContentView(R.layout.main); 
            
         // Set up the window layout
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.main);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

            // Set up the custom title
            mTitle = (TextView) findViewById(R.id.title_left_text);
            mTitle.setText(R.string.app_name);
            mTitle = (TextView) findViewById(R.id.title_right_text);
            
            TcpServiceHandler handler=new TcpServiceHandler(this); 
            handler.execute("192.168.62.23"); 
        } 
          
        public void callCompleted(ArrayList<String> tempStr){ 
           for (int i = 0; i < tempStr.size()-1; i++)   {
              //    String RetStr = responseMessage.get(i);
               String source = tempStr.get(i);            
          
           //mTitle.setText(source);
           if(source.matches("<MSG><N>.*<!N><V>.*<!V><!MSG>"))   {
              Log.d("TCP", "C: In the parser" + " " + source);
                //mTitle.setText(source1);
                //tempStr = "";
                //String source = "<MSG><N>shiftDirection<!N><V>1<!V><!MSG>";   
           
                // Match characters lying before message name
                String find1 = "<MSG><N>";       
                // Match characters lying before message value
                String find2 = ".*<!N><V>";       
                // Mask the characters by replacing them with blank space
                String replace = "";
           
                // Mask the characters lying before message name
                Pattern pattern = Pattern.compile(find1);
                Matcher matcher = pattern.matcher(source);
                String output = matcher.replaceAll(replace);
           
                // Split the message and extract message name
                String name1[] = output.split("<!N>");
                String msgName = name1[0];
           
                //Log.i(TAG, "Message name is" + msgName);
           
                //Mask the characters lying before message value
                Pattern pattern1 = Pattern.compile(find2);
                Matcher matcher1 = pattern1.matcher(output);
                String output1 = matcher1.replaceAll(replace);
           
                //Split the remaining message and extract message value
                String val1[] = output1.split("<!V>");
                String actVal = val1[0];
           
                //Log.d("TCP", msgName);       
                //Pass the message name and value for further action           
                float msgValue = Float.parseFloat(actVal);           
                actionOnData(msgName, msgValue);           
            }   else Log.d("TCP", "C:Wrong format message" + " " + source);
           }
        }
      
        
        
      //Function to display driver assistance info/images based on individual messages
        public void actionOnData(String name, float value) {
           String tempName = name;
           float tempVal = value;             
           //while (true)   {
              if(tempName.equals("shiftDirection") && tempVal == 1.0)   {
                 Log.d("TCP","C:Correct message " + tempName + " " + tempVal);                  
                   Intent myIntent = new Intent();
                   myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
                   //myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where key needs current package prefix.
                   startActivity(myIntent);                
               } else Log.d("TCP", "C:Wrong value message" + " " + tempName);
              
           //}
           
        }
}


Interface

Code: Select all
import java.util.ArrayList;

public interface TCPListener {
   public void callCompleted(ArrayList<String> msg);
}


AsyncTask

Code: Select all
public class TcpServiceHandler extends AsyncTask<String, Integer, ArrayList<String>>{ 
        TCPListener _listener; 
        public TcpServiceHandler(TCPListener listener){ 
            _listener=listener; 
        } 
          
        //@Override 
        //protected void onPreExecute() { 
            //you may wish to show a wait screen here 
        //}            
          
        @Override 
        protected ArrayList<String> doInBackground(String... params) { 
            //you should do the desktop app connectivity part here 
           ArrayList<String> StrArr = new ArrayList<String>();
            String str = ""; 
           //String str = "";
            //while (true)   {
            try{              
                //response=//stream received from desktop app 
               InetAddress serverAddr = InetAddress.getByName("192.168.62.23");
               Log.d("TCP", "C: Connecting...");
               Socket socket = new Socket(serverAddr, 1200);
               Log.d("TCP", "C: Receiving...");
               //char[] buffer = new char[1024];
               //int bytes;
                //String end = "<!MSG>";
                //StringBuilder curMsg = new StringBuilder();

                    try {
                      
                         BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                        
                         for(int i = 0; i < 10; i++)   {
                         StrArr.add(in.readLine());
                         //while(str != null)   {
                         //     StrArr.add(str);
                         //}
                         //StrArr.add(str);
                         //return str;
                       }
                         //return str;
                         //Pass str to main activity for parsing
                         //mHandler.obtainMessage(TCPIPCommn.INCOMING_MESSAGE, str).sendToTarget();                   
                       } catch(Exception e) {
                          Log.d("TCP","C: Error", e);
                           e.printStackTrace();
                       } finally {
                          socket.close();
                          Log.d("TCP","C: Done danaadan.");
                       }                   
            } 
            catch(Exception e){}              
            return StrArr;              
            //return str;             
        //} 
        }   
          
        @Override 
        protected void onPostExecute(ArrayList<String> responseMessage) {          
            //notify your activity that you have received message from desktop app
           //for (int i = 0; i < responseMessage.size(); i++)   {
           //    String RetStr = responseMessage.get(i);
              _listener.callCompleted(responseMessage);
           //    try {
          //      Thread.sleep(5000);
          //   } catch (InterruptedException e) {
          //      Log.d("TCP","C:Wait error", e);
          //      e.printStackTrace();
          //   }
             
           //}
              
        } 
    }


LogCat
Code: Select all
01-10 15:11:35.200: WARN/KeyCharacterMap(124): No keyboard for id 0
01-10 15:11:35.200: WARN/KeyCharacterMap(124): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-10 15:11:37.181: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.settings/.Settings }
01-10 15:11:38.601: INFO/ActivityManager(58): Displayed activity com.android.settings/.Settings: 1169 ms (total 244461 ms)
01-10 15:11:40.301: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.ApplicationSettings }
01-10 15:11:41.041: INFO/ActivityManager(58): Displayed activity com.android.settings/.ApplicationSettings: 725 ms (total 725 ms)
01-10 15:11:42.310: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.ManageApplications }
01-10 15:11:42.620: DEBUG/dalvikvm(126): GC_FOR_MALLOC freed 2632 objects / 174952 bytes in 85ms
01-10 15:11:43.030: DEBUG/dalvikvm(126): GC_FOR_MALLOC freed 1370 objects / 329072 bytes in 76ms
01-10 15:11:48.033: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.VIEW cmp=com.android.settings/.InstalledAppDetails (has extras) }
01-10 15:11:48.380: DEBUG/dalvikvm(126): GC_FOR_MALLOC freed 5176 objects / 372360 bytes in 84ms
01-10 15:11:49.000: INFO/ActivityManager(58): Displayed activity com.android.settings/.InstalledAppDetails: 883 ms (total 6661 ms)
01-10 15:11:54.300: DEBUG/dalvikvm(124): GC_EXPLICIT freed 1074 objects / 58400 bytes in 238ms
01-10 15:11:59.390: DEBUG/dalvikvm(158): GC_EXPLICIT freed 1766 objects / 89928 bytes in 282ms
01-10 15:12:02.620: DEBUG/AndroidRuntime(354): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-10 15:12:02.630: DEBUG/AndroidRuntime(354): CheckJNI is ON
01-10 15:12:03.120: DEBUG/AndroidRuntime(354): --- registering native functions ---
01-10 15:12:04.560: DEBUG/AndroidRuntime(354): Shutting down VM
01-10 15:12:04.571: DEBUG/dalvikvm(354): Debugger has detached; object registry had 1 entries
01-10 15:12:04.601: INFO/AndroidRuntime(354): NOTE: attach of thread 'Binder Thread #3' failed
01-10 15:12:05.121: DEBUG/AndroidRuntime(362): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-10 15:12:05.121: DEBUG/AndroidRuntime(362): CheckJNI is ON
01-10 15:12:05.330: DEBUG/AndroidRuntime(362): --- registering native functions ---
01-10 15:12:06.131: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.android.TCPListen/.TCPListen }
01-10 15:12:06.221: DEBUG/AndroidRuntime(362): Shutting down VM
01-10 15:12:06.231: DEBUG/dalvikvm(362): Debugger has detached; object registry had 1 entries
01-10 15:12:06.291: INFO/ActivityManager(58): Start proc com.example.android.TCPListen for activity com.example.android.TCPListen/.TCPListen: pid=369 uid=10037 gids={3003}
01-10 15:12:06.301: INFO/AndroidRuntime(362): NOTE: attach of thread 'Binder Thread #3' failed
01-10 15:12:06.981: DEBUG/dalvikvm(58): GREF has increased to 301
01-10 15:12:07.171: INFO/ActivityManager(58): Displayed activity com.example.android.TCPListen/.TCPListen: 981 ms (total 981 ms)
01-10 15:12:07.200: DEBUG/TCP(369): C: Connecting...
01-10 15:12:07.530: DEBUG/TCP(369): C: Receiving...
01-10 15:12:07.530: INFO/global(369): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
01-10 15:12:09.641: DEBUG/TCP(369): C: Done danaadan.
01-10 15:12:09.651: DEBUG/TCP(369): C: In the parser <MSG><N>shiftDirection<!N><V>0<!V><!MSG>
01-10 15:12:09.670: DEBUG/TCP(369): C:Wrong value message shiftDirection
01-10 15:12:09.680: DEBUG/TCP(369): C:Wrong format message SD<!N><V>0<!V><!MSG>
01-10 15:12:09.690: DEBUG/TCP(369): C: In the parser <MSG><N>shiftDirection<!N><V>1<!V><!MSG>
01-10 15:12:09.711: DEBUG/TCP(369): C:Correct message shiftDirection 1.0
01-10 15:12:09.711: INFO/ActivityManager(58): Starting activity: Intent { cmp=com.example.android.TCPListen/.Images }
01-10 15:12:09.740: DEBUG/AndroidRuntime(369): Shutting down VM
01-10 15:12:09.750: WARN/dalvikvm(369): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-10 15:12:09.770: ERROR/AndroidRuntime(369): FATAL EXCEPTION: main
01-10 15:12:09.770: ERROR/AndroidRuntime(369): java.lang.NullPointerException
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at com.example.android.TCPListen.TCPListen.callCompleted(TCPListen.java:43)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at com.example.android.TCPListen.TcpServiceHandler.onPostExecute(TcpServiceHandler.java:75)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at com.example.android.TCPListen.TcpServiceHandler.onPostExecute(TcpServiceHandler.java:1)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at android.os.AsyncTask.finish(AsyncTask.java:417)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at android.os.Looper.loop(Looper.java:123)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at java.lang.reflect.Method.invoke(Method.java:521)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-10 15:12:09.770: ERROR/AndroidRuntime(369):     at dalvik.system.NativeStart.main(Native Method)
01-10 15:12:09.981: DEBUG/dalvikvm(58): GC_FOR_MALLOC freed 8198 objects / 417664 bytes in 162ms
01-10 15:12:10.241: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{43fd1ac0 com.example.android.TCPListen/.TCPListen}
01-10 15:12:12.080: INFO/Process(369): Sending signal. PID: 369 SIG: 9
01-10 15:12:12.151: INFO/ActivityManager(58): Process com.example.android.TCPListen (pid 369) has died.
01-10 15:12:12.170: INFO/WindowManager(58): WIN DEATH: Window{4402bf88 com.example.android.TCPListen/com.example.android.TCPListen.TCPListen paused=false}
01-10 15:12:12.420: INFO/UsageStats(58): Unexpected resume of com.android.settings while already resumed in com.example.android.TCPListen
01-10 15:12:12.591: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 369 uid 10037
01-10 15:14:59.360: DEBUG/SntpClient(58): request time failed: java.net.SocketException: Address family not supported by protocol
User avatar
madhunandan
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Dec 08, 2010 4:40 pm
Location: Rüsselsheim, Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media and 7 guests