Skip to content

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setPadding(int, int, int, int)' on a null object reference #5837

@bradstiff

Description

@bradstiff

Summary

The logic applies the mapPadding prop calls map.setPadding. If map is null, app crashes.

Reproducible sample code

import React, {useEffect, useState} from 'react';
import {AppState} from 'react-native';
import MapView, {PROVIDER_GOOGLE} from 'react-native-maps';

export default function App() {
  const [appState, setAppState] = useState(AppState.currentState); // 'active' | 'background' | 'inactive'
  useEffect(() => {
    const sub = AppState.addEventListener('change', next => setAppState(next));
    return () => sub.remove();
  }, []);

  if (appState === 'background') {
    return null;
  }

  return (
      <MapView
        style={{flex: 1}}
        provider={PROVIDER_GOOGLE}
        mapPadding={{bottom: 160}}
        initialRegion={{
          latitude: 42,
          longitude: 30,
          latitudeDelta: 1,
          longitudeDelta: 1,
        }}
      />
  );
}

Steps to reproduce

Use the new architecture, and set map padding via props. Include logic that unmounts the map when the app is backgrounded (uncommon, but used in some cases to release memory).

Background the app. Resume the app. Repeat until the crash occurs.

Expected result

Don't crash.

Actual result

Crashes.

React Native Maps Version

1.26.20

What platforms are you seeing the problem on?

Android

React Native Version

0.83.1

What version of Expo are you using?

Not using Expo

Device(s)

Samsung Galaxy S24

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions