-1

please help me with this code iam getting this following error iam a newbie

--------- beginning of crash
07-08 05:12:32.223 2990-2990/com.parkaspot.najeeb.project E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parkaspot.najeeb.project, PID: 2990
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Double.toString()' on a null object reference
    at com.parkaspot.najeeb.project.RecentAdapter$1.onClick(RecentAdapter.java:76)
    at android.view.View.performClick(View.java:5637)
    at android.view.View$PerformClick.run(View.java:22429)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

i can send the whole project or the required files please help me with it And this is my program

`package com.parkaspot.najeeb.project;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
 import android.support.v4.app.ActivityCompat;
 import android.support.v7.widget.CardView;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;

 import com.github.ivbaranov.mli.MaterialLetterIcon;

 import java.util.ArrayList;
 import java.util.Random;

public class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder>{
private Context context;
private ArrayList<RecentData> mArrayList;
private LayoutInflater inflater;
private int[] mMaterialColors;
Location networkLocation;
Double lati,lngi;
private static final Random RANDOM = new Random();

public RecentAdapter(Context context, ArrayList<RecentData> list) {
    this.context = context;
    this.mArrayList = list;
    inflater = LayoutInflater.from(context);
    mMaterialColors = context.getResources().getIntArray(R.array.colors);
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    boolean networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);



    if (networkEnabled) {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }

    if (networkLocation!=null){
        lati = networkLocation.getLatitude();
        lngi = networkLocation.getLongitude();
    }

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.parking_lots_list,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    holder.name.setText(mArrayList.get(position).getName());
    holder.lotTotal.setText(mArrayList.get(position).getFilled()+"/"+ mArrayList.get(position).getTotal());
    holder.lotService.setText(mArrayList.get(position).getService()+""+" Wheeler");

    holder.nameIcon.setShapeColor(mMaterialColors[RANDOM.nextInt(mMaterialColors.length)]);
    holder.nameIcon.setLetter(mArrayList.get(position).getName());

    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(context,MainRouteActivity.class);
            i.putExtra("OriginLatt",lati.toString()); //this line shows error
            i.putExtra("OriginLong",lngi.toString());
            i.putExtra("destinLat",mArrayList.get(position).getLat());
            i.putExtra("destinLong",mArrayList.get(position).getLng());
            i.putExtra("bank",mArrayList.get(position).getName());
            context.startActivity(i);

        }
    });
}




@Override
public int getItemCount() {
    return mArrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView name,lotTotal,lotService;
    MaterialLetterIcon nameIcon;
    CardView cardView;

    public ViewHolder(View itemView) {
        super(itemView);

        nameIcon = itemView.findViewById(R.id.icon);
        nameIcon.setLetterSize(20);
        nameIcon.setShapeType(MaterialLetterIcon.Shape.CIRCLE);
        nameIcon.setLettersNumber(3);
        nameIcon.setInitialsNumber(3);

        name = itemView.findViewById(R.id.name);
        lotTotal = itemView.findViewById(R.id.lot_total);
        lotService = itemView.findViewById(R.id.lot_service);


        cardView = itemView.findViewById(R.id.card_view);

    }
}
}`

Emulator Pic App stop null pointer

brothers please help me ive been spending 3 days to solve this problem but iam unable to iam new to android programmking and doing a college project please help me fix it

1 Answers1

0

Why im seeing is that your gps won't get your location, and with that you always receive the null pointer,

this is the class that i use when i wanna obtain the geolocation

LocationDetector.kt

class LocationDetector(val context: Context) {

    val fusedLocationClient: FusedLocationProviderClient = FusedLocationProviderClient(context)
    var locationListener: LocationListener? = null

    interface LocationListener {
        fun locationFound(location: Location)
        fun locationNotFound(reason: String)
    }

    fun detectLocation() {

        //create request
        val locationRequest = LocationRequest()
        locationRequest.interval = 0L

        // check for permission
        val permissionResult = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)

        // if have permission, try to get location within 10 seconds
        if (permissionResult == android.content.pm.PackageManager.PERMISSION_GRANTED) {
            val timer = Timer()

            val locationCallback = object : LocationCallback() {
                override fun onLocationResult(locationResult: LocationResult) {
                    fusedLocationClient.removeLocationUpdates(this)
                    timer.cancel()
                    // return location
                    locationListener?.locationFound(locationResult.locations.first())
                }
            }

            timer.schedule(timerTask {
                fusedLocationClient.removeLocationUpdates(locationCallback)
                locationListener?.locationNotFound("Timeout")
            }, 10 * 1000) //10 seconds

            // make request
            fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, null)
        } else {
            // if no permission
            locationListener?.locationNotFound("No permission given")
        }
    }

and the way that you'll neeed

class MainActivity : AppCompatActivity(), LocationDetector.LocationListener {
    var isGPSRunning = false
    override fun locationFound(location: Location) {
   AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.latitude,location.latitude.toString())
    AppPreferencesSingleton(applicationContext).put(AppPreferencesSingleton.Key.longitude,location.longitude.toString())
}

override fun locationNotFound(reason: String) {
    when(isGPSEnabled()){
        true -> {
            println("Waiting for GPS fix")
        }
        false -> {
            if (!isGPSRunning) {
                isGPSRunning = true
                startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
            }
        }
    }
}

fun isGPSEnabled() = (getSystemService(Context.LOCATION_SERVICE) as LocationManager).isProviderEnabled(LocationManager.GPS_PROVIDER)

This is in KOTLIN but the adaptation is simple to do, please remember to set in your manifest the required permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
Luis Cardoza Bird
  • 1,265
  • 4
  • 24
  • 43