0

If I press the call button, I get an error, that is, the taxi does not call, but goes to another window.

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customers_map);

    customerLogoutButton = (Button) findViewById(R.id.cuslomer_logout_button);
    settingsButton = (Button)findViewById(R.id.cuslomer_settings_button);
    callTaxiButton = (Button)findViewById(R.id.cuslomer_order_button);
    callDriver = findViewById(R.id.call_to_driver);

    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    customerID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    CustomerDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Customers Requests");
    DriversAvailableRef = FirebaseDatabase.getInstance().getReference().child("Driver Available");
    DriversLocationRef = FirebaseDatabase.getInstance().getReference().child("Driver Working");

    txtName = (TextView)findViewById(R.id.driver_name);
    txtPhone = (TextView)findViewById(R.id.driver_phone_number);
    txtCarName = (TextView)findViewById(R.id.driver_car);
    driverPhoto = (CircleImageView)findViewById(R.id.driver_photo);
    relativeLayout = findViewById(R.id.rel1);

    relativeLayout.setVisibility(View.INVISIBLE);

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    settingsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CustomersMapActivity.this, SettingsActivity.class);
            intent.putExtra("type", "Customers");
            startActivity(intent);
        }
    });

    customerLogoutButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAuth.signOut();
            LogoutCustomer();
        }
    });

    callTaxiButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            if (requestType)
            {
                requestType = false;
                GeoFire geofire = new GeoFire(CustomerDatabaseRef);
                geofire.removeLocation(customerID);

                if(PickUpMarker !=null)
                {
                    PickUpMarker.remove();
                }
                if(driverMarker !=null)
                {
                    driverMarker.remove();
                }

                callTaxiButton.setText("Вызвать такси");
                if (driverFound!=null)
                {
                    DriversRef = FirebaseDatabase.getInstance().getReference()
                            .child("Users").child("Drivers").child(driverFoundID).child("CustomerRideID");

                    DriversRef.removeValue();

                    driverFoundID = null;
                }

                driverFound = false;
                radius = 1;


            }
            else {
                requestType = true;

                GeoFire geofire = new GeoFire(CustomerDatabaseRef);
                geofire.setLocation(customerID, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));

                CustomerPosition = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
                PickUpMarker = mMap.addMarker(new MarkerOptions().position(CustomerPosition).title("Я здесь").icon(BitmapDescriptorFactory.fromResource(R.drawable.user)));

                callTaxiButton.setText("Поиск водителя...");
                getNearbyDrivers();
            }


        }
    });
}

I think this error is from a realtime database. If you have a different opinion, write the your answer.

Error: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.drivetaxiuzbek, PID: 8915 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at com.example.drivetaxiuzbek.CustomersMapActivity$3.onClick(CustomersMapActivity.java:170) at android.view.View.performClick(View.java:7125) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

enter image description here

  • Are you sure that `lastLocation` is not null? – Alex Mamo Jul 02 '21 at 14:21
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Henry Twist Jul 02 '21 at 14:24

1 Answers1

0

Try to check if lastLocation is null before you run your code. Run it only when lastLocation is not null:

if(lastLocation!=null){
 GeoFire geofire = new GeoFire(CustomerDatabaseRef);
                geofire.setLocation(customerID, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));

                CustomerPosition = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
                PickUpMarker = mMap.addMarker(new MarkerOptions().position(CustomerPosition).title("Я здесь").icon(BitmapDescriptorFactory.fromResource(R.drawable.user)));

                callTaxiButton.setText("Поиск водителя...");
                getNearbyDrivers();
}
Tarik Huber
  • 7,061
  • 2
  • 12
  • 18