user3648435
user3648435

Reputation: 471

react native:typeerror null is not an object (evaluating '_reactnative simdata.default.get sim info().DeviceId0)

I Developed an Aplication in Expo.And finally Need to get User DeviceId that doesn't Support in Expo. so I Had to eject my project to react native, But after ejecting When I tried to use react native sim data It Doesn't Work. This Error Appears Every Time:

typeerror null is not an object (evaluating '_reactnativesimdata.default.getsiminfo().DeviceId0)

What I Done: First of all I installed this module by this command:

npm i react-native-sim-data then linked that to myproject By this Command: react-native link react-native-sim-data This Is My Code:

import React from 'react';
import { PermissionsAndroid ,Button,View} from 'react-native';

import RNSimData from 'react-native-sim-data'

export default class App extends React.Component {




  render() {

    return (
    <View>

          <Button title="permission" onPress={()=>{   PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE);}}></Button>
          <Button title="Show Id" onPress={()=>{  var x=RNSimData.getSimInfo().deviceId0;
  if(x!=null)alert(x); else alert('nooooo');}}></Button>

    </View>
  )
}
}

Please Help Me, this problem made me crazy

Upvotes: 0

Views: 1628

Answers (1)

WebsByTodd
WebsByTodd

Reputation: 21

Are you trying to use this on an emulator that doesn't have a sim card? See https://github.com/pocesar/react-native-sim-data#caveats in the README for react-native-sim-data.

If you are running on an actual device with a real sim card and you're still seeing this error I would suggest modifying your button's event handler to null check some of the various steps in the process:

var buttonClickEventHandler = ()=> {  
  var simInfo = RNSimData.getSimInfo();
  if (!simInfo) {
    alert("no info") 
  } else {
    var deviceId = simInfo.deviceId0;
    if (!deviceId) {
      alert("no device Id");
    } else {
      alert(deviceId);
    }
  }
}

Upvotes: 1

Related Questions