Hey guys. I have a simple question about my android app. I know what you are thinking " this dumbass just posted this in the wrong thread". But I did it for a reason. The error that I have is related to java...not android.

(I will star every line of code that I talk about)

So here is the problem: I get a NullPointerException at line 75 in the MainActivity class. So what I do is look at that line. It reads:

if(number.length() == 0){

So what I'm thinking is that I forgot to set the value of number. Then I look right above that and it says:

SettingsActivity settings = new SettingsActivity();
number = settings.phone_number;

, and I find that I assigned the variable to phone_number, which if you are smart enough to figure out is a string with a phone number inside of it. At this point I don't know what to do. I made sure everything was public, but I'm sure it has to do with the placement of variables inside brackets but I am just not seeing it.

Here are the MainActivity and SettingsActivity classes:

thanks in advance for anything that you guys do

package com.bobrown101.gpsbuddy;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.telephony.SmsManager;

public class MainActivity extends Activity {

    public String Lat;
    public String Long;
    boolean toggleBtn = false;

    public int x = 7200;
    public long y;
    public String z;
    public String phone_number;
    boolean bool = false;

    public TextView t;
    public TextView s;
    public TextView state;
    public EditText edittext;
    public String number;
    public String notif;
    public String actual_number;
    public Button buttonOn;
    public Button buttonOff;





    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Button buttonStart = (Button)findViewById(R.id.button1);  
         buttonOn = (Button)findViewById(R.id.button4);
         buttonOff = (Button)findViewById(R.id.button5);
         edittext = (EditText)findViewById(R.id.editText1);
         buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above

         t=new TextView(this);
         s=new TextView(this);
         state=new TextView(this);
         t=(TextView)findViewById(R.id.textView3); 
         s=(TextView)findViewById(R.id.textView7);
         state=(TextView)findViewById(R.id.textView6);

         buttonOff.setEnabled(false);
         }

    public OnClickListener startListener = new OnClickListener() {

        public void onClick(View v) {
            SettingsActivity settings = new SettingsActivity();
            number = settings.phone_number;

            if(number.length() == 0){
                //Toast.makeText(getBaseContext(), "Please check to make sure you have chosen the recipients contact in the settings menu", Toast.LENGTH_LONG).show();
                System.out.println("here is the phone number that you were trying to send the message to: " + number);
            }else{
                doEverything();  
                Toast.makeText(getBaseContext(), "Manual message SENT!", Toast.LENGTH_SHORT).show();
            }
        }
    };


    public void button_on(View v) {
        if(!bool){
            Toast.makeText(getBaseContext(), "Please input a phone number, then press submit.", Toast.LENGTH_SHORT).show();
        }else{

            state.setText("On "); 
            Calendar cal = Calendar.getInstance();

            Intent intent = new Intent(this, MyService.class);
            PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

            AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            System.out.println(x);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), x*1000, pintent);  //7200 is 2 hours 

            //toggleBtn = true;
             //System.out.println("On button pressed. Value of toggleBtn: " + toggleBtn);
            Toast.makeText(getBaseContext(), "Service is now ON!", Toast.LENGTH_SHORT).show();
             //Toast.makeText(getBaseContext(), String.valueOf(toggleBtn), Toast.LENGTH_SHORT).show();

            s.setText("");
            buttonOn.setEnabled(false);
            buttonOff.setEnabled(true); 
        }
    }


    public void button_off(View v) {
        MyService service = new MyService();
        Calendar cal = Calendar.getInstance();

         Intent intent = new Intent(this, MyService.class);
         PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

         AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);




        alarm.cancel(pintent); 
        Toast.makeText(getBaseContext(), "Service is now OFF!", Toast.LENGTH_SHORT).show();

        state.setText("Off");
        service.counter = 0;
        buttonOn.setEnabled(true);
        buttonOff.setEnabled(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void sendSMS(String x) {
        String phoneNumber = number;
        String message = x;

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);
    }
    public void getLocation(){
        GPSTracker mGPS = new GPSTracker(this);

        if(mGPS.canGetLocation ){

        double mLat=mGPS.getLatitude();
        double mLong=mGPS.getLongitude();
        Lat = Double.toString(mLat);
        Long = Double.toString(mLong);      

        }else{
        // can't get the location
        }
    }

    public void doEverything(){
        getLocation();
        String text = Lat + "," + Long;
        sendSMS(text);
    }
    public void time_up(View v){  //stops the alarm and ads 30 minutes to the timer
        Calendar cal = Calendar.getInstance();

         Intent intent = new Intent(this, MyService.class);
         PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

         AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
         System.out.println(x);
         alarm.cancel(pintent);


         //alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), x*1000, pintent);  //7200 is 2 hours 
        x = x +3600; 
        y = x/3600;
        z = y + " hours";
        t.setText(z);
        notif = s.getText().toString();
        if(!(notif == "The Service was turned off because you adjusted the time. Please press 'on' to restart")){
            Toast.makeText(getBaseContext(), "Service must be stopped to adjust time. If you would like GPS Buddy to restart, please pres 'ON' now", Toast.LENGTH_LONG).show();
            s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
        }else{
            s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
        }
    }
    public void time_down(View v){ //stops the alarm and subtracts 30 minutes to the timer

        if(x == 3600){
            x = 3600;
            Toast.makeText(getBaseContext(), "I'm sorry. The lowest interval is 1 hour.", Toast.LENGTH_LONG).show();
        }else{
            Calendar cal = Calendar.getInstance();

             Intent intent = new Intent(this, MyService.class);
             PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

             AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
             System.out.println(x);
             alarm.cancel(pintent);
            x = x -3600; 
            y = x/3600;
            z = y + " hours";
            t.setText(z);
            notif = s.getText().toString();
            if(!(notif == "The Service was turned off because you adjusted the time. Please press 'on' to restart")){
                Toast.makeText(getBaseContext(), "Service must be stopped to adjust time. If you would like GPS Buddy to restart, please pres 'ON' now", Toast.LENGTH_LONG).show();
                s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
            }else{
                s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
            }
        }
    }

    public void number_submit(View v){
        //number = edittext.getText().toString();
        SettingsActivity settings = new SettingsActivity();

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent myIntent = new Intent(MainActivity.this, SettingsActivity.class);
                //myIntent.putExtra("key", value); //Optional parameters
                MainActivity.this.startActivity(myIntent);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }


}

here is the other

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Contacts; 
import android.provider.ContactsContract.CommonDataKinds.Phone;;

public class SettingsActivity extends Activity {




    String DEBUG_TAG = "CONTACT CHOOSER ERROR: ";
    private static final int CONTACT_PICKER_RESULT = 1001;
    public String phone_number;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        // Show the Up button in the action bar.
        setupActionBar();

    }

    /**
     * Set up the {@link android.app.ActionBar}.
     */
    private void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void button_submit(View v){

        doLaunchContactPicker(v);

    }


    public void doLaunchContactPicker(View view) {  
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);  

        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
    }

    @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data) {  

        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case CONTACT_PICKER_RESULT:  
                Cursor cursor = null;  

                try {  
                    Uri result = data.getData();  
                    Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());  
                    // get the contact id from the Uri  
                    String id = result.getLastPathSegment();  
                    // query for everything phone number
                    cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                    // let's just get the first email  
                    if (cursor.moveToFirst()) {  
                        phone_number = cursor.getString(phoneIdx);  


                        Log.w(DEBUG_TAG, phone_number);
                    } else {  
                        Log.w(DEBUG_TAG, "No results");  
                    }  
                } catch (Exception e) {  
                    Log.e(DEBUG_TAG, "Failed to get phone data", e);  
                } finally {  
                    if (cursor != null) {  
                        cursor.close();  
                    }  
                    EditText phoneEntry = (EditText) findViewById(R.id.editText1);  
                    phoneEntry.setText(phone_number);  

                    if (phone_number.length() == 0) {  
                        Toast.makeText(this, "No phone number found for contact.",  Toast.LENGTH_LONG).show();  
                    }else{

                    }
                }  
                break;  
            }  
        } else {  
            Log.w(DEBUG_TAG, "Warning: activity result not ok");  
        }  
    } 

}
Stuugie commented: LoL, I wasn't thinking dumbass but lol to that! +2

public String phone_number;

that is your declaration of phone_number in your activity class. now, you instantiate your activity class, but you never set a value to phone_number, so ... yes, it does still have a null-reference.

you may want to do a few things here:
- set default values, or define a constructor that does so. you haven't set a constructor yourself, so you'll use the one the compiler generates for you, and in that one, you don't set a value to phone_number.
- keep your instance variables private, having all your variables public is pretty bad design. set them to private and use mutators.

Thanks for your tips. Just try to bair with me, but I'm really confused about what you just said.

here are the adjusted areas about what you were talking about:

public OnClickListener startListener = new OnClickListener() {

        public void onClick(View v) {
            //SettingsActivity settings = new SettingsActivity();
            //number = settings.text;

            if(number.length() == 0){
                //Toast.makeText(getBaseContext(), "Please check to make sure you have chosen the recipients contact in the settings menu", Toast.LENGTH_LONG).show();
                System.out.println("here is the phone number that you were trying to send the message to: " + number);
            }else{
                doEverything();  
                Toast.makeText(getBaseContext(), "Manual message SENT!", Toast.LENGTH_SHORT).show();
            }
        }
    };

here is the other code:

@Override  
public void onActivityResult(int requestCode, int resultCode, Intent data) {  

    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case CONTACT_PICKER_RESULT:  
            Cursor cursor = null;  

            try {  
                Uri result = data.getData();  
                Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());  
                // get the contact id from the Uri  
                String id = result.getLastPathSegment();  
                // query for everything phone number
                cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
                int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                // let's just get the first email  
                if (cursor.moveToFirst()) {  
                    phone_number = cursor.getString(phoneIdx);  
                    text = phone_number;

                    Log.w(DEBUG_TAG, phone_number);
                } else {  
                    Log.w(DEBUG_TAG, "No results");  
                }  
            } catch (Exception e) {  
                Log.e(DEBUG_TAG, "Failed to get phone data", e);  
            } finally {  
                if (cursor != null) {  
                    cursor.close();  
                }  
                EditText phoneEntry = (EditText) findViewById(R.id.editText1);  
                phoneEntry.setText(phone_number);  
                MainActivity main = new MainActivity();
                main.number = phone_number;

                if (phone_number.length() == 0) {  
                    Toast.makeText(this, "No phone number found for contact.",  Toast.LENGTH_LONG).show();  
                }else{

                }
            }  
            break;  
        }  
    } else {  
        Log.w(DEBUG_TAG, "Warning: activity result not ok");  
    }  
} 

let me explain what I'm doing:

I created a settings menu for my app that requires a phone number
Then I made it so the user can choose a contact from a contact dialog.
The user will click a button that says "choose a contact"
Then the dialog will open and they will click on a name
Then the app will fetch the phone number and set the text of a edittext**** as the phone number in case they want to edit it or anything.
Then i set the variable 'number' from the MainActivity class to 'phone_number' from the SettingsActivity class(which is also the same variable used to set the text of the edittext****)

After I choose the contact, the number shows up in the edittext which tells me that phone_number has the right value

The problem is, is when I try to set 'number' in the mainactivity class to 'phone_number' from the SettingsActivity class.

well ... I didn't even know you had a startListener so .. that wasn't really what I was talking about.
what I meant was, in your Activity class (inherited members taken into account), you basically have:
(and I'm not using the right names here)

public class MyActivity{

  public String phone_number;

  public MyActivity(){}

}

In this part of the code, you don't set a value for phone_number. since it's an instance member, it's default set to null.

now, in your calling class, you do this:

SettingsActivity settings = new SettingsActivity();
number = settings.phone_number;

so, at first, you run the default constructor, which looks like the one I added above in that MyActivity example. it doesn't do anything, except for initializing the instance of SettingsActivity. that doesn't mean it initializes the instance variables.

change that Activity class to either:

public class MyActivity{

  public String phone_number ="00";

  public MyActivity(){}

}

or to

public class MyActivity{

  public String phone_number;

  public MyActivity(){this.phone_number="00";}

}

now, these are both bogus numbers of course, but this way

    SettingsActivity settings = new SettingsActivity();
    number = settings.phone_number;

the settings.phone_number call will there return "00" instead of null, so at least the nullpointer 'll be solved.

my other point was: don't make your instance members public. you shouldn't be able to call them by: settings.phone_number. make use of the OO concepts, like encapsulation, to make your code a lot safer.so, my suggestion, it would be something like:

public class MyActivity{

  private String phone_number ="00";

  public MyActivity(){}

}

or

public class MyActivity{

  private String phone_number;

  public MyActivity(){this.phone_number="00";}

}

and add the mutators (setters and getters)

  public String getPhoneNumber(){
    return this.phone_number;
  }

  public void setPhoneNumber(String number){
    this.phone_number = number;
  }

now, they're still bogus, but now the access and influence the calling classes have on the instance member(s) are more restricted, giving you more abilities to guarantee the contents or validation of those members.

The calling class would then become something like:

    SettingsActivity settings = new SettingsActivity();
    number = settings.getPhoneNumber();

Thanks for your help. It will be a while until I can change the code, but thanks in advance!!!!

Okay, I fixed the problem, but now I have another one:

I made a settings menu called SettingsActivity.

What I do is click on "Choose a Contact", and it brings up a contact dialog.

Then I click on the contact and fetch the phone number. (this all happens in onActivityResult)

Then I set the variable phone_number to the string that I just got from the contact dialog.

Then I do

main.setPhoneNumber(phone_number);

to set the variable number (Located in the MainActivity class) to phone_number.

Then I create an alarm manager when I click the 'on' button in the MainActivity to launch MyService at an interval (which will inevitably send a text at an interval)

EVERYTHING WORKS UP UNTIL THIS POINT

When MyService loads, It runs onStartCommand. This function runs every time that the service is called (which is when the alarmManager from MainActivity calls it)

In onStartCommand, it calls doEverything().

Do everything looks like this:

public void doEverything(){
    number = MainActivity.getPhoneNumber();     
    getLocation();
        String text = Lat + "," + Long;
    sendSMS(text);
}

doEverything() will get the phone number fromMainActivity, then get you location, then convert it to the message we want to send, and then send it via the sendSMS(text) command

Here is where the error is coming from.

For some reason, the number variable comes back as null. It shouldn't be because on line 126 of SettingsActivity, I do this MainActivity.setPhoneNumber(phone_number);

here is the error log;

06-20 14:26:23.730: I/System.out(18005): Here is number coming from myService: 
06-20 14:26:23.730: D/AndroidRuntime(18005): Shutting down VM
06-20 14:26:23.730: W/dalvikvm(18005): threadid=1: thread exiting with uncaught exception (group=0x40a79930)
06-20 14:26:23.730: E/AndroidRuntime(18005): FATAL EXCEPTION: main
06-20 14:26:23.730: E/AndroidRuntime(18005): java.lang.RuntimeException: Unable to start service com.bobrown101.gpsbuddy.MyService@40ce9cc8 with Intent { flg=0x4 cmp=com.bobrown101.gpsbuddy/.MyService (has extras) }: java.lang.IllegalArgumentException: Invalid destinationAddress
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2801)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.app.ActivityThread.access$1900(ActivityThread.java:153)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.os.Looper.loop(Looper.java:137)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.app.ActivityThread.main(ActivityThread.java:5227)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at dalvik.system.NativeStart.main(Native Method)
06-20 14:26:23.730: E/AndroidRuntime(18005): Caused by: java.lang.IllegalArgumentException: Invalid destinationAddress
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:77)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at com.bobrown101.gpsbuddy.MyService.sendSMS(MyService.java:34)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at com.bobrown101.gpsbuddy.MyService.doEverything(MyService.java:54)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at com.bobrown101.gpsbuddy.MyService.onStartCommand(MyService.java:20)
06-20 14:26:23.730: E/AndroidRuntime(18005):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2784)
06-20 14:26:23.730: E/AndroidRuntime(18005):    ... 10 more

-------------------------------------------------------------------------------------------------

package com.bobrown101.gpsbuddy;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.SmsManager;

public class MyService extends Service {
    public String Lat;
    public String Long;
    public boolean toggleBtn;
    public boolean bool;
    public int counter = 0;
    String number;

      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {

          doEverything();   

          return Service.START_STICKY; //changed from START_NOT_STICKY

      }

      public void sendSMS(String x) {

          System.out.println("Here is number coming from myService: " + number);

          String phoneNumber = number;
          String message = x;

          SmsManager smsManager = SmsManager.getDefault();
          smsManager.sendTextMessage(phoneNumber, null, message, null, null);
        }
        public void getLocation(){
            GPSTracker mGPS = new GPSTracker(this);

            if(mGPS.canGetLocation ){

            double mLat=mGPS.getLatitude();
            double mLong=mGPS.getLongitude();
            Lat = Double.toString(mLat);
            Long = Double.toString(mLong);      

            }else{
            // can't get the location
            }
        }
        public void doEverything(){
            number = MainActivity.getPhoneNumber();
            getLocation();
            String text = Lat + "," + Long;
            sendSMS(text);
        }

      @Override
      public IBinder onBind(Intent intent) {
      //TODO for communication return IBinder implementation
        return null;
      }

------------------------------------------------------------------------------------------------

package com.bobrown101.gpsbuddy;


import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.telephony.SmsManager;

public class MainActivity extends Activity {

    public String Lat;
    public String Long;

    public static int x = 7200;
    public long y;
    public String z;

    public TextView t;
    public static TextView s;
    public TextView state;
    public EditText edittext;
    public static String number = "";
    public static String notif;
    public String actual_number;
    public Button buttonOn;
    public Button buttonOff;
    public static String phone_number;
    public String value;
    public static boolean running = false;


    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Button buttonStart = (Button)findViewById(R.id.button1);  
         buttonOn = (Button)findViewById(R.id.button4);
         buttonOff = (Button)findViewById(R.id.button5);
         edittext = (EditText)findViewById(R.id.editText1);
         buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above

         t=new TextView(this);
         s=new TextView(this);
         state=new TextView(this);
         t=(TextView)findViewById(R.id.textView3); 
         s=(TextView)findViewById(R.id.textView7);
         state=(TextView)findViewById(R.id.textView6);

         buttonOff.setEnabled(false);
         }

    public OnClickListener startListener = new OnClickListener() {


        public void onClick(View v) {           
            if(number.length() == 0){               
                Toast.makeText(getBaseContext(), "Please make sure you have chosen a contact in the settings menu.", Toast.LENGTH_SHORT).show();
            }else{
                doEverything();  
                Toast.makeText(getBaseContext(), "Manual message SENT!", Toast.LENGTH_SHORT).show();
            }
       }
    };    
    public void button_on(View v) {
        if(number.length() == 0){
            Toast.makeText(getBaseContext(), "Please make sure you have chosen a contact in the settings menu.", Toast.LENGTH_SHORT).show();
        }else{      
            state.setText("On "); 
            Calendar cal = Calendar.getInstance();  
            Intent intent = new Intent(this, MyService.class);
            PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);   
            AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            System.out.println(x);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);  //temporarily set at 30 seconds for debugging, but change back to x when you are done            
            Toast.makeText(getBaseContext(), "Service is now ON!", Toast.LENGTH_SHORT).show();           
            s.setText("");
            buttonOn.setEnabled(false);
            buttonOff.setEnabled(true); 
            running = true;
        }
    }


    public void button_off(View v) {
        MyService service = new MyService();
        Intent intent = new Intent(this, MyService.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pintent); 
        Toast.makeText(getBaseContext(), "Service is now OFF!", Toast.LENGTH_SHORT).show();

        state.setText("Off");
        service.counter = 0;
        buttonOn.setEnabled(true);
        buttonOff.setEnabled(false);
        running = false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }   
    public static void sendSMS(String x) {
        String phoneNumber = number;
        String message = x;

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);
    }
    public void getLocation(){
        GPSTracker mGPS = new GPSTracker(this);

        if(mGPS.canGetLocation ){
            double mLat=mGPS.getLatitude();
            double mLong=mGPS.getLongitude();
            Lat = Double.toString(mLat);
            Long = Double.toString(mLong);  
        }
    }

    public void doEverything(){
        getLocation();
        String text = Lat + "," + Long;
        sendSMS(text);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) { //creates and opens SettingsActivity
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                Intent myIntent = new Intent(MainActivity.this, SettingsActivity.class);
                //myIntent.putExtra("key", value); //Optional parameters
                MainActivity.this.startActivity(myIntent);
                return true;            
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public static void setPhoneNumber(String x){
        number = x;
        System.out.println("X = " + x + " number = " + number);
    }
    public static String getPhoneNumber(){
        return number;
    }

}

-----------------------------------------------------------------------------------------

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Contacts; 
import android.provider.ContactsContract.CommonDataKinds.Phone;;

public class SettingsActivity extends Activity {




    String DEBUG_TAG = "CONTACT CHOOSER ERROR: ";
    public static final int CONTACT_PICKER_RESULT = 1001;
    public static String phone_number;
    public String text;
    public static TextView hours;

    public int x = 7200;
    public long y;
    public String z;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        // Show the Up button in the action bar.
        setupActionBar();
        hours = (TextView)findViewById(R.id.textView4);
    }

    /**
     * Set up the {@link android.app.ActionBar}.
     */
    public void setupActionBar() {

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void button_submit(View v){

        doLaunchContactPicker(v);

    }


    public void doLaunchContactPicker(View view) {  
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);  

        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);  
    }

    @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data) {  //this is what gets the phone number from the contacts dialog

        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case CONTACT_PICKER_RESULT:  
                Cursor cursor = null;  

                try {  
                    Uri result = data.getData();  
                    Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());  
                    // get the contact id from the Uri  
                    String id = result.getLastPathSegment();  
                    // query for everything phone number
                    cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  
                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                    // let's just get the first email  
                    if (cursor.moveToFirst()) {  
                        phone_number = cursor.getString(phoneIdx);  
                        text = phone_number;

                        Log.w(DEBUG_TAG, phone_number);
                    } else {  
                        Log.w(DEBUG_TAG, "No results");  
                    }  
                } catch (Exception e) {  
                    Log.e(DEBUG_TAG, "Failed to get phone data", e);  
                } finally {  
                    if (cursor != null) {  
                        cursor.close();  
                    }  
                    EditText phoneEntry = (EditText) findViewById(R.id.editText1);  
                    phoneEntry.setText(phone_number);  

                    MainActivity.setPhoneNumber(phone_number);
                    if (phone_number.length() == 0) {  
                        Toast.makeText(this, "No phone number found for contact.",  Toast.LENGTH_LONG).show();  
                    }
                }  
                break;  
            }  
        } else {  
            Log.w(DEBUG_TAG, "Warning: activity result not ok");  
        }  
    } 

    public void writeFile(){
        BufferedWriter bufferedWriter;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(new File(getFilesDir()+File.separator+"PhoneNumber.txt")));
            bufferedWriter.write(phone_number);
            bufferedWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public void time_up(View v){  //stops the alarm and ads 30 minutes to the timer
        Intent intent = new Intent(this, MyService.class);
        PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        System.out.println(x);
        alarm.cancel(pintent);      

        MainActivity.x = MainActivity.x +3600; 
        y = MainActivity.x/3600;
        z = y + " hours";
        SettingsActivity.hours.setText(z);

        MainActivity.notif = MainActivity.s.getText().toString();

        if (MainActivity.running){
            if(!(MainActivity.notif == "The Service was turned off because you adjusted the time. Please press 'on' to restart")){
                Toast.makeText(getBaseContext(), "Service must be stopped to adjust time. If you would like GPS Buddy to restart, please pres 'ON' now", Toast.LENGTH_LONG).show();
                MainActivity.s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
            }else{
                MainActivity.s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
            }
        }
    }
    public void time_down(View v){ //stops the alarm and subtracts 30 minutes to the timer and adjusts the displayed interval

        if(MainActivity.x == 3600){
            MainActivity.x = 3600;
            Toast.makeText(getBaseContext(), "I'm sorry. The lowest interval is 1 hour.", Toast.LENGTH_LONG).show();
        }else{
            Intent intent = new Intent(this, MyService.class);
            PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

            AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            System.out.println(x);
            alarm.cancel(pintent);
            MainActivity.x = MainActivity.x -3600; 
            y = MainActivity.x/3600;
            z = y + " hours";
            SettingsActivity.hours.setText(z);
            MainActivity.notif = MainActivity.s.getText().toString();
            if (MainActivity.running){
                if(!(MainActivity.notif == "The Service was turned off because you adjusted the time. Please press 'on' to restart")){
                    Toast.makeText(getBaseContext(), "Service must be stopped to adjust time. If you would like GPS Buddy to restart, please pres 'ON' now", Toast.LENGTH_LONG).show();
                    MainActivity.s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
                }else{
                    MainActivity.s.setText("The Service was turned off because you adjusted the time. Please press 'on' to restart");
                }
            }
        }
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();

    }
}

your problem is in this line:

smsManager.sendTextMessage(phoneNumber, null, message, null, null);

either phoneNumber or message contains an invalid value (empty or null). I'll add a link to the api here: [sendTextMessage](http://developer.android.com/reference/android/telephony/SmsManager.html#sendTextMessage(java.lang.String, java.lang.String, java.lang.String, android.app.PendingIntent, android.app.PendingIntent)) just try this with hardcoded values, and check if it works then. if so, you'll need to debug to ensure you get the right values when running that line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.