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

Open
#5,837 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
android, react-native, typescript
Domain
mobile

Research direction

Start by tracing the Android mapPadding prop handling and the map lifecycle during the background-to-foreground transition described in the sample. Reproduce the crash with the provided TSX example under the new architecture, then verify that repeatedly unmounting and remounting the map no longer crashes when the map reference is null.

Written by the indexing model from the issue text.

Description

bug
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

Dominant language
TypeScript
Stars
16k
Forks
5k
Avg merge
9d 9h
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from react-native-maps/react-native-maps

All issues in react-native-maps/react-native-maps

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.