File is not created, get NullPointerException


Last Updated:

  1. pramod.deore

    pramod.deore Member This Topic's Starter

    Joined:
    Jan 10, 2010
    Messages:
    22
    Likes Received:
    0
    Hello everybody again,

    I am developing one application that will create one txt file when it runs first time and saves IMSI number in that file , After that when phone boots every time it checks that IMSI number with IMSI number which is stored at txt file. But when I run this program it gives me Null pointer Exception. I have following code.


    //FinalSMS.java
    Code (Text):
    1.  
    2.  
    3.  
    4. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]package
    5. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]com.micro;[/SIZE]
    6.  
    7. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    8. [/LEFT]
    9. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.app.Activity;[/SIZE]
    10. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    11. [/LEFT]
    12. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.app.PendingIntent;[/SIZE]
    13. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    14. [/LEFT]
    15. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.BroadcastReceiver;[/SIZE]
    16. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    17. [/LEFT]
    18. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Context;[/SIZE]
    19. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    20. [/LEFT]
    21. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Intent;[/SIZE]
    22. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    23. [/LEFT]
    24. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.IntentFilter;[/SIZE]
    25. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    26. [/LEFT]
    27. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.os.Bundle;[/SIZE]
    28. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    29. [/LEFT]
    30. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.telephony.TelephonyManager;[/SIZE]
    31. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    32. [/LEFT]
    33. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.telephony.gsm.SmsManager;[/SIZE]
    34. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    35. [/LEFT]
    36. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.widget.Toast;[/SIZE]
    37.  
    38. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    39. [/LEFT]
    40. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]java.io.*;[/SIZE]
    41.  
    42. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/LEFT]
    43. [/LEFT]
    44. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] FinalSMS [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity[/SIZE]
    45. [LEFT][SIZE=2]{[/SIZE]
    46. [SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/**[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]Called[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]when[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]the[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]activity[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]is[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]first[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]created.[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]*/[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][/LEFT]
    47. [/COLOR][/SIZE]
    48. [LEFT][SIZE=2]File [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    49. [SIZE=2]String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imsi[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    50. [SIZE=2]String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]data[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    51. [SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#646464]
    52. [/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState)[/SIZE]
    53. [SIZE=2]{[/SIZE]
    54. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
    55. [SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
    56. [SIZE=2]createFile(); [/SIZE]
    57. [SIZE=2]checkIMSI();[/SIZE]
    58. [SIZE=2]}[/SIZE][/LEFT]
    59.  
    60. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] createFile()[/SIZE]
    61. [SIZE=2]{[/SIZE]
    62. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
    63. [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    64. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] File ([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"tempt.txt"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    65. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].exists())[/SIZE]
    66. [SIZE=2]{[/SIZE]
    67. [SIZE=2]FileReader fr = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] FileReader([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    68. [SIZE=2]BufferedReader br = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BufferedReader(fr);[/SIZE]
    69. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]data[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = br.readLine();[/SIZE][/LEFT]
    70.  
    71. [LEFT][SIZE=2]}[/SIZE]
    72. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
    73. [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    74. [SIZE=2]displayIMSI();[/SIZE]
    75. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].createNewFile();[/SIZE]
    76. [SIZE=2]PrintWriter pw = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] PrintWriter([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    77. [SIZE=2]TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TELEPHONY_SERVICE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
    78. [SIZE=2]String imsi = mTelephonyMgr.getSubscriberId(); [/SIZE]
    79. [SIZE=2]pw.println(imsi);[/SIZE]
    80. [SIZE=2]pw.flush();[/SIZE]
    81. [SIZE=2]pw.close();[/SIZE]
    82. [SIZE=2]}[/SIZE][/LEFT]
    83.  
    84. [LEFT][SIZE=2]}[/SIZE][/LEFT]
    85.  
    86. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (IOException ioe)[/SIZE]
    87. [SIZE=2]{[/SIZE]
    88. [SIZE=2]ioe.printStackTrace();[/SIZE]
    89. [SIZE=2]}[/SIZE]
    90. [SIZE=2]}[/SIZE][/LEFT]
    91.  
    92. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] checkIMSI()[/SIZE]
    93. [SIZE=2]{[/SIZE]
    94. [SIZE=2]TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TELEPHONY_SERVICE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
    95. [SIZE=2]String imsi = mTelephonyMgr.getSubscriberId(); [/SIZE]
    96. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]data[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].equals(imsi))[/SIZE]
    97. [SIZE=2]{[/SIZE]
    98. [SIZE=2]sendSMS([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"9960510915"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Sim has not changed."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    99. [SIZE=2]}[/SIZE]
    100. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
    101. [/COLOR][/SIZE][SIZE=2]{[/SIZE]
    102. [SIZE=2]sendSMS([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"9960510915"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Sim has been changed."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    103. [SIZE=2]}[/SIZE]
    104. [SIZE=2]}[/SIZE][/LEFT]
    105.  
    106. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] displayIMSI()[/SIZE]
    107. [SIZE=2]{[/SIZE]
    108. [SIZE=2]TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TELEPHONY_SERVICE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
    109. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imsi[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = mTelephonyMgr.getSubscriberId(); [/SIZE][/LEFT]
    110.  
    111. [LEFT][SIZE=2]} [/SIZE][/LEFT]
    112.  
    113. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] sendSMS(String phoneNumber, String message)[/SIZE]
    114. [SIZE=2]{ [/SIZE]
    115. [SIZE=2]String SENT = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS_SENT"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    116. [SIZE=2]String DELIVERED = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS_DELIVERED"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
    117.  
    118. [LEFT][SIZE=2]PendingIntent sentPI = PendingIntent.[I]getBroadcast[/I]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], 0,[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent(SENT), 0);[/SIZE][/LEFT]
    119.  
    120. [LEFT][SIZE=2]PendingIntent deliveredPI = PendingIntent.[I]getBroadcast[/I]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], 0,[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent(DELIVERED), 0);[/SIZE][/LEFT]
    121.  
    122. [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---when the SMS has been sent---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    123. [/COLOR][/SIZE][SIZE=2]registerReceiver([/LEFT]
    124. [/LEFT]
    125. [/SIZE][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BroadcastReceiver(){[/SIZE]
    126.  
    127. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onReceive(Context arg0, Intent arg1)[/SIZE]
    128. [SIZE=2]{[/SIZE]
    129. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (getResultCode())[/SIZE]
    130. [SIZE=2]{[/SIZE]
    131. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_OK[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    132. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS sent"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
    133. [SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    134. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    135. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_GENERIC_FAILURE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    136. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Generic failure"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
    137. [SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    138. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    139. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_NO_SERVICE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    140. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"No service"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
    141. [SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    142. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    143. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_NULL_PDU[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    144. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Null PDU"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
    145. [SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    146. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    147. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SmsManager.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_ERROR_RADIO_OFF[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    148. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Radio off"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
    149. [SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    150. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    151. [SIZE=2]}[/SIZE]
    152. [SIZE=2]}[/SIZE]
    153. [SIZE=2]}, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] IntentFilter(SENT));[/SIZE][/LEFT]
    154.  
    155. [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---when the SMS has been delivered---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    156. [/COLOR][/SIZE][SIZE=2]registerReceiver([/LEFT]
    157. [/LEFT]
    158. [/SIZE][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BroadcastReceiver()[/SIZE]
    159. [LEFT][SIZE=2]{[/SIZE]
    160. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onReceive(Context arg0, Intent arg1)[/SIZE]
    161. [SIZE=2]{[/SIZE]
    162. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (getResultCode())[/SIZE]
    163. [SIZE=2]{[/SIZE]
    164. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_OK[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    165. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS delivered"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE]
    166. [SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    167. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
    168. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]case[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RESULT_CANCELED[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]:[/SIZE]
    169. [SIZE=2]Toast.[I]makeText[/I](getBaseContext(), [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS not delivered"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
    170. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]; [/SIZE]
    171. [SIZE=2]}[/SIZE]
    172. [SIZE=2]}[/SIZE]
    173. [SIZE=2]},[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] IntentFilter(DELIVERED)); [/SIZE][/LEFT]
    174.  
    175. [LEFT][SIZE=2]SmsManager sms = SmsManager.[I]getDefault[/I]();[/SIZE]
    176. [SIZE=2]sms.sendTextMessage(phoneNumber, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], message, sentPI, deliveredPI); [/SIZE]
    177. [SIZE=2]}[/SIZE][/LEFT]
    178.  
    179. [SIZE=2]}[/SIZE]
    180.  
    181.  
    //MyStartupIntentReceiver .java

    Code (Text):
    1.  
    2.  
    3. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]package[/LEFT]
    4. [/LEFT]
    5. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]com.micro;[/SIZE]
    6.  
    7. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    8. [/LEFT]
    9. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Context; [/SIZE]
    10. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    11. [/LEFT]
    12. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Intent; [/SIZE]
    13. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
    14. [/LEFT]
    15. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.BroadcastReceiver; [/SIZE]
    16.  
    17. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/LEFT]
    18. [/LEFT]
    19. [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] MyStartupIntentReceiver [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BroadcastReceiver { [/SIZE]
    20.  
    21. [LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
    22. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onReceive(Context context, Intent intent) { [/SIZE]
    23. [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Create intent which will finally start the Main-Activity. */[/COLOR][/SIZE][/COLOR][/SIZE]
    24. [SIZE=2]Intent myStarterIntent = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent(context, FinalSMS.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]); [/SIZE]
    25. [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Set the Launch-Flag to the Intent. */[/COLOR][/SIZE][/COLOR][/SIZE]
    26. [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
    27. [/COLOR][/SIZE][SIZE=2]myStarterIntent.setFlags(Intent.[/LEFT]
    28. [/LEFT]
    29. [/SIZE][LEFT][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FLAG_ACTIVITY_NEW_TASK[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
    30.  
    31. [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Send the Intent to the OS. */[/COLOR][/SIZE][/COLOR][/SIZE]
    32. [SIZE=2]context.startActivity(myStarterIntent); [/SIZE]
    33. [SIZE=2]} [/SIZE]
    34. [SIZE=2]}[/SIZE][/LEFT]
    35.  
    36.  
    //AndroidManifest.java
    Code (Text):
    1.  
    2.  
    3. [LEFT][SIZE=2]<?[U]xml[/U] version="1.0" encoding="[U]utf[/U]-8"?>[/SIZE]
    4. [SIZE=2]<manifest xmlns:android="http://schemas.android.com/apk/res/android"[/SIZE]
    5. [SIZE=2]package="[U]com[/U].[U]micro[/U]"[/SIZE]
    6. [SIZE=2]android:versionCode="1"[/SIZE]
    7. [SIZE=2]android:versionName="1.0">[/SIZE]
    8. [SIZE=2]<application android:icon="[U]@drawable[/U]/icon" android:label="@string/app_name">[/SIZE]
    9. [SIZE=2]<activity android:name=".FinalSMS"[/SIZE]
    10. [SIZE=2]android:label="@string/app_name">[/SIZE]
    11. [SIZE=2]<intent-filter>[/SIZE]
    12. [SIZE=2]<action android:name="[U]android[/U].intent.action.MAIN" />[/SIZE]
    13. [SIZE=2]<category android:name="[U]android[/U].intent.category.LAUNCHER" />[/SIZE]
    14. [SIZE=2]</intent-filter>[/SIZE]
    15. [SIZE=2]</activity>[/SIZE]
    16. [SIZE=2]<receiver android:name=".MyStartupIntentReceiver">[/SIZE]
    17. [SIZE=2]<intent-filter>[/SIZE]
    18. [SIZE=2]<action android:name="[U]android[/U].intent.action.BOOT_COMPLETED" />[/SIZE]
    19. [SIZE=2]</intent-filter>[/SIZE]
    20. [SIZE=2]</receiver>[/SIZE]
    21. [SIZE=2]</application>[/SIZE]
    22. [SIZE=2]<uses-[U]sdk[/U] android:minSdkVersion="3" />[/SIZE][/LEFT]
    23.  
    24. [LEFT][SIZE=2]<uses-permission android:name="[U]android[/U].permission.SEND_SMS">[/SIZE]
    25. [SIZE=2]</uses-permission>[/SIZE][/LEFT]
    26.  
    27. [LEFT][SIZE=2]<uses-permission android:name="[U]android[/U].permission.RECEIVE_SMS">[/SIZE]
    28. [SIZE=2]</uses-permission>[/SIZE][/LEFT]
    29.  
    30. [LEFT][SIZE=2]<uses-permission android:name="[U]android[/U].permission.RECEIVE_BOOT_COMPLETED" />[/SIZE]
    31. [SIZE=2]<uses-permission android:name="[U]android[/U].permission.READ_PHONE_STATE" />[/SIZE][/LEFT]
    32. [SIZE=2]</manifest> [/SIZE]
    33.  
    34.  
    But when I run it it gives me following error messages
    Code (Text):
    1.  
    2.  
    3.  
    4. [SIZE=2][SIZE=2]01-29 14:58:28.948: ERROR/AndroidRuntime(837): Uncaught handler: thread main exiting due to uncaught exception
    5. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micro/com.micro.FinalSMS}: java.lang.NullPointerException
    6. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
    7. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
    8. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
    9. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
    10. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:99)
    11. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:123)
    12. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:3948)
    13. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method)
    14. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:521)
    15. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
    16. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
    17. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method)
    18. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): Caused by: java.lang.NullPointerException
    19. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.checkIMSI(FinalSMS.java:68)
    20. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.onCreate(FinalSMS.java:29)
    21. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    22. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
    23. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): ... 11 more
    24. [LEFT][/SIZE][/SIZE]
    [/LEFT]

    when I had checked whether the file is created or not in data/data/com/micro/files/ The file is not present.
    Because file is not created therefore it throws run time exception (NullPointerException) , Please help me

    Thanks
    [/LEFT]
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi,

    The reason for the null pointer exception is fairly clear from the code & the traceback.

    Given this bit of the traceback:

    Code (Text):
    1.  
    2. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): Caused by: java.lang.NullPointerException
    3. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.checkIMSI(FinalSMS.java:68)
    4. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.onCreate(FinalSMS.java:29)
    5. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
    6. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
    7. 01-29 14:58:28.978: ERROR/AndroidRuntime(837): ... 11 more
    8.  
    You know it crashed in the checkIMSI method.

    And given this:

    Code (Text):
    1.  
    2. publicvoid checkIMSI()
    3. {
    4. TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    5. String imsi = mTelephonyMgr.getSubscriberId();
    6. if(data.equals(imsi))
    7. {
    8. sendSMS("9960510915","Sim has not changed.");
    9. }
    10. else
    11. {
    12. sendSMS("9960510915","Sim has been changed.");
    13. }
    14. }
    15.  
    There are only 2 ways to get a null pointer exception in that method.
    You'll know which line is number 68, but I can't tell from what you've posted.

    But I'd guess that it's crashing because the data variable is null.

    Based on your createFile method, as file file doesn't exist you will not assign a value to the data variable, and so it will be null.

    Code (Text):
    1.  
    2. publicvoid createFile()
    3. {
    4. try
    5. {
    6. myFile = new File ("tempt.txt");
    7. if (myFile.exists())
    8. {
    9. FileReader fr = new FileReader(myFile);
    10. BufferedReader br = new BufferedReader(fr);
    11. data = br.readLine();           // ********* data only set if file exists
    12.  
    13. }
    14. else
    15. {
    16. displayIMSI();
    17. myFile.createNewFile();
    18. PrintWriter pw = new PrintWriter(myFile);
    19. TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    20. String imsi = mTelephonyMgr.getSubscriberId();
    21. pw.println(imsi);
    22. pw.flush();
    23. pw.close();
    24. }
    25.  
    26. }
    27.  
    For future reference it might help to narrow down the problem before posting, and just post the most relevant bits of code.

    A more concise description of your problem might be:

    The following code either does not create the file, or doesn't create it where I expect it to be:

    Code (Text):
    1.  
    2. myFile = new File ("tempt.txt");
    3. myFile.createNewFile();
    4.  
    And the answer to that problem can be found here:

    Working with Files :: anddev.org - Android Development Community | Android Tutorials

    In other words, you can't just use the standard java.io.* classes to create files.

    I hope that helps.

    Mark
     

Share This Page

Loading...