• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps File is not created, get NullPointerException

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:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]package
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]com.micro;[/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.app.Activity;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.app.PendingIntent;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.BroadcastReceiver;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Context;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Intent;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.IntentFilter;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.os.Bundle;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.telephony.TelephonyManager;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.telephony.gsm.SmsManager;[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.widget.Toast;[/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]java.io.*;[/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/LEFT]
[/LEFT]
[/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]
[LEFT][SIZE=2]{[/SIZE]
[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]
[/COLOR][/SIZE]
[LEFT][SIZE=2]File [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imsi[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]data[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#646464]
[/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]
[SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]createFile(); [/SIZE]
[SIZE=2]checkIMSI();[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
[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]
[SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[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]
[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]
[SIZE=2]{[/SIZE]
[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]
[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]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]data[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = br.readLine();[/SIZE][/LEFT]
 
[LEFT][SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[SIZE=2]displayIMSI();[/SIZE]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]myFile[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].createNewFile();[/SIZE]
[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]
[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]
[SIZE=2]String imsi = mTelephonyMgr.getSubscriberId(); [/SIZE]
[SIZE=2]pw.println(imsi);[/SIZE]
[SIZE=2]pw.flush();[/SIZE]
[SIZE=2]pw.close();[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (IOException ioe)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]ioe.printStackTrace();[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
[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]
[SIZE=2]{[/SIZE]
[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]
[SIZE=2]String imsi = mTelephonyMgr.getSubscriberId(); [/SIZE]
[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]
[SIZE=2]{[/SIZE]
[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]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
[/COLOR][/SIZE][SIZE=2]{[/SIZE]
[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]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
[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]
[SIZE=2]{[/SIZE]
[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]
[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imsi[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = mTelephonyMgr.getSubscriberId(); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]} [/SIZE][/LEFT]
 
[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]
[SIZE=2]{ [/SIZE]
[SIZE=2]String SENT = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS_SENT"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String DELIVERED = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SMS_DELIVERED"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
 
[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]
 
[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]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---when the SMS has been sent---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]registerReceiver([/LEFT]
[/LEFT]
[/SIZE][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BroadcastReceiver(){[/SIZE]
 
[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]
[SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (getResultCode())[/SIZE]
[SIZE=2]{[/SIZE]
[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]
[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]
[SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
[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]
[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]
[SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
[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]
[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]
[SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
[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]
[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]
[SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
[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]
[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]
[SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] IntentFilter(SENT));[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//---when the SMS has been delivered---[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]registerReceiver([/LEFT]
[/LEFT]
[/SIZE][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BroadcastReceiver()[/SIZE]
[LEFT][SIZE=2]{[/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] onReceive(Context arg0, Intent arg1)[/SIZE]
[SIZE=2]{[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]switch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (getResultCode())[/SIZE]
[SIZE=2]{[/SIZE]
[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]
[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]
[SIZE=2]Toast.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LENGTH_SHORT[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]).show();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE]
[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]
[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]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]break[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]; [/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]},[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] IntentFilter(DELIVERED)); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]SmsManager sms = SmsManager.[I]getDefault[/I]();[/SIZE]
[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]
[SIZE=2]}[/SIZE][/LEFT]
 
[SIZE=2]}[/SIZE]
//MyStartupIntentReceiver .java

Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]package[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]com.micro;[/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Context; [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.Intent; [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/LEFT]
[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]android.content.BroadcastReceiver; [/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/LEFT]
[/LEFT]
[/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]
 
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/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] onReceive(Context context, Intent intent) { [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Create intent which will finally start the Main-Activity. */[/COLOR][/SIZE][/COLOR][/SIZE]
[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]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Set the Launch-Flag to the Intent. */[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//myStarterIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH);[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f]
[/COLOR][/SIZE][SIZE=2]myStarterIntent.setFlags(Intent.[/LEFT]
[/LEFT]
[/SIZE][LEFT][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]FLAG_ACTIVITY_NEW_TASK[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]); [/SIZE]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Send the Intent to the OS. */[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]context.startActivity(myStarterIntent); [/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]}[/SIZE][/LEFT]

//AndroidManifest.java
Code:
[LEFT][SIZE=2]<?[U]xml[/U] version="1.0" encoding="[U]utf[/U]-8"?>[/SIZE]
[SIZE=2]<manifest xmlns:android="http://schemas.android.com/apk/res/android"[/SIZE]
[SIZE=2]package="[U]com[/U].[U]micro[/U]"[/SIZE]
[SIZE=2]android:versionCode="1"[/SIZE]
[SIZE=2]android:versionName="1.0">[/SIZE]
[SIZE=2]<application android:icon="[U]@drawable[/U]/icon" android:label="@string/app_name">[/SIZE]
[SIZE=2]<activity android:name=".FinalSMS"[/SIZE]
[SIZE=2]android:label="@string/app_name">[/SIZE]
[SIZE=2]<intent-filter>[/SIZE]
[SIZE=2]<action android:name="[U]android[/U].intent.action.MAIN" />[/SIZE]
[SIZE=2]<category android:name="[U]android[/U].intent.category.LAUNCHER" />[/SIZE]
[SIZE=2]</intent-filter>[/SIZE]
[SIZE=2]</activity>[/SIZE]
[SIZE=2]<receiver android:name=".MyStartupIntentReceiver">[/SIZE]
[SIZE=2]<intent-filter>[/SIZE]
[SIZE=2]<action android:name="[U]android[/U].intent.action.BOOT_COMPLETED" />[/SIZE]
[SIZE=2]</intent-filter>[/SIZE]
[SIZE=2]</receiver>[/SIZE]
[SIZE=2]</application>[/SIZE]
[SIZE=2]<uses-[U]sdk[/U] android:minSdkVersion="3" />[/SIZE][/LEFT]
 
[LEFT][SIZE=2]<uses-permission android:name="[U]android[/U].permission.SEND_SMS">[/SIZE]
[SIZE=2]</uses-permission>[/SIZE][/LEFT]
 
[LEFT][SIZE=2]<uses-permission android:name="[U]android[/U].permission.RECEIVE_SMS">[/SIZE]
[SIZE=2]</uses-permission>[/SIZE][/LEFT]
 
[LEFT][SIZE=2]<uses-permission android:name="[U]android[/U].permission.RECEIVE_BOOT_COMPLETED" />[/SIZE]
[SIZE=2]<uses-permission android:name="[U]android[/U].permission.READ_PHONE_STATE" />[/SIZE][/LEFT]
[SIZE=2]</manifest> [/SIZE]

But when I run it it gives me following error messages
Code:
[SIZE=2][SIZE=2]01-29 14:58:28.948: ERROR/AndroidRuntime(837): Uncaught handler: thread main exiting due to uncaught exception
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
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:123)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:3948)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:521)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): Caused by: java.lang.NullPointerException
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.checkIMSI(FinalSMS.java:68)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.onCreate(FinalSMS.java:29)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): ... 11 more
[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]
 
Hi,

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

Given this bit of the traceback:

Code:
01-29 14:58:28.978: ERROR/AndroidRuntime(837): Caused by: java.lang.NullPointerException
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.checkIMSI(FinalSMS.java:68)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at com.micro.FinalSMS.onCreate(FinalSMS.java:29)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
01-29 14:58:28.978: ERROR/AndroidRuntime(837): ... 11 more

You know it crashed in the checkIMSI method.

And given this:

Code:
publicvoid checkIMSI()
{
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
String imsi = mTelephonyMgr.getSubscriberId(); 
if(data.equals(imsi))
{
sendSMS("9960510915","Sim has not changed.");
}
else
{
sendSMS("9960510915","Sim has been changed.");
}
}

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:
publicvoid createFile()
{
try
{
myFile = new File ("tempt.txt");
if (myFile.exists()) 
{
FileReader fr = new FileReader(myFile);
BufferedReader br = new BufferedReader(fr);
data = br.readLine();           // ********* data only set if file exists
 
}
else
{
displayIMSI();
myFile.createNewFile();
PrintWriter pw = new PrintWriter(myFile);
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
String imsi = mTelephonyMgr.getSubscriberId(); 
pw.println(imsi);
pw.flush();
pw.close();
}
 
}

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:
myFile = new File ("tempt.txt");
myFile.createNewFile();

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
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones