[Solved] Null ptr exception before starting activity

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

[Solved] Null ptr exception before starting activity

Postby chimaera » Fri Jun 19, 2009 1:51 am

Hi All,

I just deployed my first app on the market tonight. It was running nice on the emulator and on my G2.
But apparently several users have difficulties starting the app, and I can't figure out why.

Here is the crash dump:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.comp.app/com.comp.app.AppActivity}: java.lang.NullPointerException
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
android.app.ActivityThread.access$1800(ActivityThread.java:112)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:3948)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:521)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
dalvik.system.NativeStart.main(Native Method)


Any ideas ?
Last edited by chimaera on Sun Jun 21, 2009 10:56 am, edited 1 time in total.
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Top

Postby WarrenFaith » Fri Jun 19, 2009 6:57 am

and on my G2.

You have the G2?

Back to topic:
Can you personally reproduce it? A bit of code would also be nice just to see what you do on startup...

Second question: Is the NPE thrown in the first run or does it appear randomly?
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby chimaera » Fri Jun 19, 2009 12:52 pm

WarrenFaith wrote:
and on my G2.

You have the G2?

Back to topic:
Can you personally reproduce it? A bit of code would also be nice just to see what you do on startup...

Second question: Is the NPE thrown in the first run or does it appear randomly?


Yes I like the soft keyboard.

Actually, that's all the info I get from my crash dump uploader. I've made modifications to also upload the exception cause. But for the moment the app in unpublished. I will probably try to release a free beta version for extended testing.
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Solved

Postby chimaera » Sun Jun 21, 2009 10:55 am

It was the typical "forget to check for null return" bug.

At startup, I use the following code to get a list of activities.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. getPackageManager().queryIntentActivities (i, PackageManager.MATCH_DEFAULT_ONLY);
  2.  
  3.  
  4.  
  5. Iterator <ResolveInfo> it = send_activity.iterator();
  6.  
  7. while(it.hasNext())
  8.  
  9. {
  10.  
  11.   ResolveInfo activity = it.next();
  12.  
  13.   if (ativity.activityInfo.applicationInfo.className != null ) ...
  14.  
  15. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


It appears that this code is (only by chance) working on G2 and the emulator, but not on the G1.
The applicationInfo can be null if a service exists that can handle the intent.
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Postby Emmanuel7 » Thu Oct 15, 2009 12:52 am

May I ask you how you do get this crash dump ?

I think I have seen somewhere a code released that could send you n email on crashes, but I can't find it again...
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby chimaera » Thu Oct 15, 2009 1:32 am

I use a background thread that upload the core file to my web server through http.
Here is the android code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. HttpClient client = new DefaultHttpClient();
  2. if (timeout != 0)
  3. {
  4.   HttpParams params = client.getParams();
  5.   HttpConnectionParams.setConnectionTimeout(params, timeout);
  6.   HttpConnectionParams.setSoTimeout(params, timeout);
  7. }
  8.                                
  9. HttpPut request = new HttpPut("<url>");
  10. HttpEntity entity = new FileEntity(file, "application/x-gzip");
  11. request.setEntity(entity);
  12.                                
  13. HttpResponse response = client.execute(request);
  14.                                
  15. if (response.getStatusLine().getStatusCode() == 200) file.delete();
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


It's PHP on the server side:
Code: Select all
<?php
   function getRealIpAddr()
   {
       if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
       {
         $ip=$_SERVER['HTTP_CLIENT_IP'];
       }
       elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
       {
         $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
       }
       else
       {
         $ip=$_SERVER['REMOTE_ADDR'];
       }
       return $ip;
   }

   $ip=getRealIpAddr();
   
   $putdata = fopen("php://input", "r");
      
   $fp = fopen("core/errordump.".$ip.".txt.gz", "w");
      
         
   while ($data = fread($putdata, 1024))
               fwrite($fp, $data);
      
     fclose($fp);
     fclose($putdata);
   }
?>


Hope this helps
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Top

Postby Emmanuel7 » Sun Oct 18, 2009 3:36 am

Thank you chimerae, but one more question :
Where do you get your trace file from ?
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby chimaera » Sun Oct 18, 2009 10:32 pm

You have to install a default uncaught exception handler. I do this in application.onCreate() Like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onCreate()
  3.  
  4. {
  5.  
  6.   original_handler = Thread.getDefaultUncaughtExceptionHandler();
  7.  
  8.                
  9.  
  10.   Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()
  11.  
  12.   {
  13.  
  14.     public void uncaughtException(Thread thread, Throwable throwable)
  15.  
  16.     {
  17.  
  18.        try
  19.  
  20.        {
  21.  
  22.           DumpUploader.makeCore(throwable);
  23.  
  24.                        
  25.  
  26.           DumpUploader.upload(2000);
  27.  
  28.        }
  29.  
  30.        catch(Throwable t)
  31.  
  32.        {
  33.  
  34.        }
  35.  
  36.  
  37.  
  38.         if (original_handler != null) original_handler.uncaughtException(thread, throwable);
  39.  
  40.     }});       
  41.  
  42. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


You can extract the stack trace from throwable.getStackTrace() and the original cause by recursively calling:
Throwable cause = throwable.getCause() ... getCause() .. getCause() until cause is null when this is the first exception.
This is important because the last exception is generally useless...

I also return information on the device thanks to the Build object (Build.DEVICE, Build.BOARD, Build.MODEL, ...). This can help troubleshoot platform-related bugs.

Off-topic: did you find your idea for a great new app ?
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Postby Emmanuel7 » Sun Oct 18, 2009 11:47 pm

Thanks for that, it is appreciated !

For the future, I still try to improved my game ( Word Prospector ), I want at least to make an online score.

Then I have several other ideas for games, of different sizes...
Actually, its mostly the time that I'm missing, rather than the ideas...
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: TurnitinBot [Bot] and 13 guests