I am trying to draw a polyline on a map
import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, SeekBar.OnSeekBarChangeListener { //Initialise variables
GoogleMap gMap;
SeekBar seekWidth, seekBlue, seekGreen, seekRed;
Button btClear, btDraw;
Polyline polyline = null;
List<LatLng> latLngList = new ArrayList<>();
List<Marker> markerList = new ArrayList<>();
PolylineOptions polylineOptions = null;
int red = 0, green= 0, blue=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Assign variable
seekWidth = findViewById(R.id.seek_width);
seekRed = findViewById(R.id.seek_red);
seekGreen = findViewById(R.id.seek_green);
seekBlue = findViewById(R.id.seek_blue);
btDraw=findViewById(R.id.bt_draw);
btClear=findViewById(R.id.bt_clear);
/*Initialise SupportMapFragment
SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.google_map);
supportMapFragment.getMapAsync(this);
btDraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Draw Polyline on Map
if (polyline!=null) polyline.remove();
//Create PolylineOptions
PolylineOptions polylineOptions = new PolylineOptions()
.addAll(latLngList).clickable(true);
polyline = gMap.addPolyline(polylineOptions);
setWidth();
}
});*/
//Initialise SupportMapFragment
SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.google_map);
supportMapFragment.getMapAsync(this);
btDraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Draw Polyline on Map
if (polyline!=null) polyline.remove();
if(polylineOptions!=null){
//Create PolylineOptions
polyline = gMap.addPolyline(polylineOptions);
setWidth();
}
}
});
btClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Clear All
if (polyline!=null) polyline.remove();
for(Marker marker: markerList) marker.remove();
latLngList.clear();
markerList.clear();
seekWidth.setProgress(3);
seekBlue.setProgress(0);
seekGreen.setProgress(0);
seekRed.setProgress(0);
}
});
seekRed.setOnSeekBarChangeListener(this);
seekGreen.setOnSeekBarChangeListener(this);
seekBlue.setOnSeekBarChangeListener(this);
}
private void setWidth() {
seekWidth.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//Get Seekbar Progress
int width = seekWidth.getProgress();
if (polyline != null) {
//Set Polyline Width
polyline.setWidth(width);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
/*@Override
public void onMapReady(GoogleMap googleMap) {
/*gMap= googleMap;
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Create MarkerOptions
MarkerOptions markerOptions = new MarkerOptions().position(latLng);
//Create Marker
Marker marker = gMap.addMarker(markerOptions);
//Add Latlng and Marker
latLngList.add(latLng);
markerList.add(marker);
}
});
}*/
public void onMapReady(GoogleMap googleMap) {
gMap= googleMap;
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
//Create MarkerOptions
MarkerOptions markerOptions = new MarkerOptions().position(latLng);
//Create Marker
Marker marker = gMap.addMarker(markerOptions);
//Add Latlng and Marker
latLngList.add(latLng);
markerList.add(marker);
polylineOptions = new PolylineOptions().addAll(latLngList).clickable(true);
}
});
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
switch (seekBar.getId()){
case R.id.seek_red:
red = i;
break;
case R.id.seek_green:
green = i;
break;
case R.id.seek_blue:
blue= i;
break;
}
if(polyline !=null){
//Set Polyline Color
polyline.setColor(Color.rgb(red,green,blue));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
THE ERROR IS AS FOLLOWS:
2019-11-29 13:39:40.391 20694-20694/com.example.poly E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.poly, PID: 20694 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.model.Polyline.setColor(int)' on a null object reference at com.example.poly.MainActivity.onProgressChanged(MainActivity.java:149) at android.widget.SeekBar.onProgressRefresh(SeekBar.java:93) at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:1327) at android.widget.ProgressBar.refreshProgress(ProgressBar.java:1382) at android.widget.ProgressBar.setProgressInternal(ProgressBar.java:1447) at android.widget.AbsSeekBar.trackTouchEvent(AbsSeekBar.java:850) at android.widget.AbsSeekBar.onTouchEvent(AbsSeekBar.java:760) at android.view.View.dispatchTouchEvent(View.java:9943) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358) at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810) at android.app.Activity.dispatchTouchEvent(Activity.java:3061) at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69) at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373) at android.view.View.dispatchPointerEvent(View.java:10163) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4434) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4302) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6313) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176) at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6284) at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6336)