-4

I'd tried to set the latitude And longitude manually in application But, after run get me an error

Code

public class Common {

    public static Location NewYork;
}

Configuration

public void setLocation(){
    Common.NewYork.setLatitude(31.963158);  //error in this Line <--
    Common.NewYork.setLongitude(35.930359);
   fragmentAdapter();
}

Error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.Location.setLatitude(double)' on a null object reference
jguerinet
  • 1,429
  • 1
  • 14
  • 21
  • 5
    Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. Also, please create a [mcve] – Zoe Feb 22 '19 at 16:02
  • What is the error ? – Tamir Abutbul Feb 22 '19 at 16:04
  • java.lang.NullPointerException: Attempt to invoke virtual method 'void android.location.Location.setLatitude(double)' on a null object reference – Moayad Shloul Feb 22 '19 at 16:05

1 Answers1

-1

You never initialized the NewYork variable, so it is set to null when setLocation() is called. You can change this by doing:

public class Common {
    // Create new Location with an empty provider name, it's not necessary here
    public static Location NewYork = new Location(""); 
}

OR

public void setLocation() {
    Common.NewYork = new Location(""); 
    ...
}
jguerinet
  • 1,429
  • 1
  • 14
  • 21