ERROR/AndroidRuntime(706): java.lang.RuntimeException: Unable to start activity


Last Updated:

  1. pramod.deore

    pramod.deore Member This Topic's Starter

    Joined:
    Jan 10, 2010
    Messages:
    22
    Likes Received:
    0
    Hello everybody, I have write one application that writes data to the text file, but when I run application it throws following exception
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micro/com.micro.MyProject}: java.lang.NullPointerException

    what's wrong in my code.

    MyProject.java file contains following code

    Code (Text):
    1.  
    2.  
    3.  [LIST=1]
    4. [*][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]package[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] com.micro;
    5. [/LEFT]
    6. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.app.Activity;[/LEFT]
    7. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] java.io.*;[/LEFT]
    8. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.content.Context;[/LEFT]
    9. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.os.Bundle;[/LEFT]
    10. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]import[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] android.widget.Toast;
    11. [/LEFT]
    12. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MyProject [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Activity
    13. {
    14. Context con;
    15.  
    16. [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/**[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]Called[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]when[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]the[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]activity[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]is[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]first[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]created.[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]*/[/LEFT]
    17. [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT][/SIZE][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Override
    18. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] onCreate(Bundle savedInstanceState) {
    19. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].onCreate(savedInstanceState);
    20. setContentView(R.layout.main);
    21. WriteSettings([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Pramod Deore"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    22. }
    23. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WriteSettings(Context context, String data)
    24. {
    25. FileOutputStream fOut = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    26. OutputStreamWriter osw = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    27.  
    28. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/LEFT]
    29. [/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]{
    30. fOut = openFileOutput([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"settings.txt"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],MODE_PRIVATE);
    31. osw = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] OutputStreamWriter(fOut);
    32. osw.write(data);
    33. osw.flush();
    34. Toast.makeText(context, [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Settings saved"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],Toast.LENGTH_SHORT).show();
    35. }
    36. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (Exception e)
    37. {
    38. e.printStackTrace();
    39. Toast.makeText([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Settings not saved"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],Toast.LENGTH_SHORT).show();
    40.  
    41. }
    42. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]finally[/LEFT]
    43. [/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]{
    44. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/LEFT]
    45. [/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][LEFT]{
    46. osw.close();
    47. fOut.close();
    48. }
    49. [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (IOException e)
    50. {
    51. e.printStackTrace();
    52. }
    53. }
    54. }[/LEFT]
    55. }
    56. [/SIZE]
    57.  
    [/LIST]and I have .txt file under src/com/micro/setting.txt file.
    following is error part from Logcat file.



    Code (Text):
    1. [LIST=1]
    2. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.112[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]): Uncaught handler: thread main exiting due to uncaught exception
    3. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micro/com.micro.MyProject}: java.lang.NullPointerException
    4. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:[COLOR=#c00000]2268[/COLOR])
    5. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:[COLOR=#c00000]2284[/COLOR])   </SPAN>
    6. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.access$[COLOR=#c00000]1800[/COLOR](ActivityThread.java:[COLOR=#c00000]112[/COLOR])   </SPAN>
    7. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:[COLOR=#c00000]1692[/COLOR])   </SPAN>
    8. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.os.Handler.dispatchMessage(Handler.java:[COLOR=#c00000]99[/COLOR])   </SPAN>
    9. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.os.Looper.loop(Looper.java:[COLOR=#c00000]123[/COLOR])   </SPAN>
    10. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.main(ActivityThread.java:[COLOR=#c00000]3948[/COLOR])   </SPAN>
    11. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at java.lang.reflect.Method.invokeNative(Native Method)   </SPAN>
    12. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at java.lang.reflect.Method.invoke(Method.java:[COLOR=#c00000]521[/COLOR])   </SPAN>
    13. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:[COLOR=#c00000]782[/COLOR])   </SPAN>
    14. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:[COLOR=#c00000]540[/COLOR])   </SPAN>
    15. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at dalvik.system.NativeStart.main(Native Method)   </SPAN>
    16. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]): Caused by: java.lang.NullPointerException   </SPAN>
    17. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.widget.Toast.<init>(Toast.java:[COLOR=#c00000]88[/COLOR])   </SPAN>
    18. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.widget.Toast.makeText(Toast.java:[COLOR=#c00000]230[/COLOR])   </SPAN>
    19. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.micro.MyProject.WriteSettings(MyProject.java:[COLOR=#c00000]33[/COLOR])   </SPAN>
    20. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at com.micro.MyProject.onCreate(MyProject.java:[COLOR=#c00000]18[/COLOR])   </SPAN>
    21. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:[COLOR=#c00000]1123[/COLOR])   </SPAN>
    22. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:[COLOR=#c00000]2231[/COLOR])   </SPAN>
    23. [*][COLOR=#c00000]01[/COLOR]-[COLOR=#c00000]16[/COLOR] [COLOR=#c00000]11[/COLOR]:[COLOR=#c00000]05[/COLOR]:[COLOR=#c00000]12.137[/COLOR]: ERROR/AndroidRuntime([COLOR=#c00000]706[/COLOR]):     ... [COLOR=#c00000]11[/COLOR] more   </SPAN>
    24. [*]
    25. [/LIST]</SPAN>
    I also tried
    WriteSettings(this, "Pramod Deore");

    instead of
    1. WriteSettings(con, "Pramod Deore");
    but still getting the same error.
    Please help me how to solve this problem.

    Thanks
     

    Advertisement
  2. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
    at com.micro.MyProject.WriteSettings(MyProject.java:33) << whats line 33?
     
  3. pramod.deore

    pramod.deore Member This Topic's Starter

    Joined:
    Jan 10, 2010
    Messages:
    22
    Likes Received:
    0
    It is
    public void WriteSettings(Context context, String data)
    If I am wrong please correct me.
     
  4. pramod.deore

    pramod.deore Member This Topic's Starter

    Joined:
    Jan 10, 2010
    Messages:
    22
    Likes Received:
    0
    Now I don't get exception . the program runs successfully, but data is not saved under settings.txt.
     
  5. pramod.deore

    pramod.deore Member This Topic's Starter

    Joined:
    Jan 10, 2010
    Messages:
    22
    Likes Received:
    0
    Hello Everybody, now I think data is stored under
    data/data/com/micro/Settings.txt file ,(because Now size of Settings.txt is increased,)but I don't understand how to see content of this file. And when I will install this application under phone then where I will get this file.
     
  6. draffodx

    draffodx Member

    Joined:
    Oct 6, 2009
    Messages:
    8
    Likes Received:
    0
    Use the DDMS in eclipse to find the file and you can then save it as a text file to somewhere on your machine and open it with a text editor
     
  7. hakerjack

    hakerjack Member

    Joined:
    Oct 24, 2010
    Messages:
    6
    Likes Received:
    0
    Hey,

    I also got this exception, could you tell me how did you fix it?

    Thanks
     
  8. aguywithathing

    aguywithathing Member

    Joined:
    Oct 12, 2010
    Messages:
    18
    Likes Received:
    5
    Check your manifest?
     
  9. AlbertPucciani

    AlbertPucciani Well-Known Member

    Joined:
    Oct 2, 2010
    Messages:
    71
    Likes Received:
    8
    Try this:

    Code (Text):
    1.  
    2. public void WriteSetting(final String fileName, final String fileText) {
    3.         try {
    4.             File root = Environment.getExternalStorageDirectory();
    5.  
    6.             if (root.canWrite()){
    7.                 File theFile = new File(root, fileName);
    8.                 FileWriter fileWriter = new FileWriter(theFile );
    9.                 BufferedWriter out = new BufferedWriter(fileWriter);
    10.                 out.write(fileText);
    11.                 out.close();
    12.             }
    13.         } catch (IOException e) {
    14.             e.printStackTrace();
    15.         }
    16. }
    17.  
    This will write a file with 'fileName' and 'fileText' to the root of the SD Card.

    In your manifest, you also need to declare:
    HTML:
    1.  
    2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    3.  
     

Share This Page

Loading...