zoontek / react-native-permissions

An unified permissions API for React Native on iOS, Android and Windows.
MIT License
4.1k stars 836 forks source link

[Android-only] check method: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference #797

Closed mljlynch closed 1 year ago

mljlynch commented 1 year ago

Bug summary

I recently moved my permissions checker to a context from a hook, so that the state would persist on update.

Here is my code

const PermissionsContext = React.createContext<PermissionsContextType>({
  ...defaultValues,
  ...defaultSetters,
  ...defaultCallbacks,
});
export const PermissionsProvider: FunctionComponent<
  PermissionsProviderProps
> = ({ children }) => {
  const [cameraPermissionStatus, setCameraPermissionStatus] =
    useState<PermissionStatus>();

  const updateCameraPermissionStatus = useCallback(async () => {
    const newPermissionStatus = await check(getOSSpecificPermission('camera'));
    setCameraPermissionStatus(newPermissionStatus);
    return newPermissionStatus;
  }, []);

  useEffect(() => {
    updateCameraPermissionStatus();
  }, [updateCameraPermissionStatus]);

  return (
    <PermissionsContext.Provider
      value={{
        cameraPermissionStatus,
        setCameraPermissionStatus,
      }}>
      {children}
    </PermissionsContext.Provider>
  );
};

When building via CI (so not deploying via USB connection and wifi), the android app repeatedly crashes on load with an error:

NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

Any help would be greatly appreciated 🙏🏼

Library version

3.6.1

Environment info

System:
    OS: macOS 13.4.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 1.62 GB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    Watchman: 2023.07.10.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.12.1 - /Users/Jake/.gem/ruby/3.1.3/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4
    Android SDK: Not Found
  IDEs:
    Android Studio: 4.2 AI-202.7660.26.42.7351085
    Xcode: 14.3.1/14E300c - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.16.1 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.2 => 17.0.2 
    react-native: 0.68.6 => 0.68.6 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to reproduce

Listed in the description

Reproducible sample code

Listed in the description

zoontek commented 1 year ago

This package does not call Double doubleValue method once, I doubt the issue came from this library. I'm closing this because of that (+ there's no clear way to reproduce the issue)

Note that you could simplify your code a bit:

import * as React from 'react';
import {Platform} from 'react-native';
import {check, PERMISSIONS, PermissionStatus} from 'react-native-permissions';

const PermissionsContext = React.createContext<{camera?: PermissionStatus}>({});

export const usePermissions = () => React.useContext(PermissionsContext);

export const PermissionsProvider: React.FC<{children: React.ReactNode}> = ({children}) => {
  const [camera, setCamera] = React.useState<PermissionStatus>();
  const permissions = React.useMemo(() => ({camera}), [camera]);

  React.useEffect(() => {
    check(Platform.select({android: PERMISSIONS.ANDROID.CAMERA, default: PERMISSIONS.IOS.CAMERA}))
      .then((status) => setCamera(status))
      .catch((_error) => setCamera('unavailable'));
  }, []);

  return <PermissionsContext.Provider value={permissions}>{children}</PermissionsContext.Provider>;
};

To understand the need to memo your provider value: https://stackoverflow.com/a/62231233

mljlynch commented 1 year ago

This repros with the cleaned up code. I will try to get you a repro in the sample app.