1 (edited by AppFreak 2018-05-21 18:03:46)

Topic: Null Error on AddMarker

I am using multiple scripts to access the map and originally I had the map/marker definition in each script as private, but when I did a OnMarkerClick there appeared to be7 bleed over between the scripts. I could tell this because each script added a different colored marker teture but when I clicked a specific color it waould use code from a differnt script. Hopefully this is somewhat clear.

What I did was move all the map code to one script and access this using an Instance of that script MapControls.Instance.SetMarkers... for example.

Now when I use one script to call the other to do an AddMarker, I get a Null Error. I have debugs that show all the fields contain the right data so not sure what is goin on. Below is the error then the code including debug results:


========== Error Begin

NullReferenceException: Object reference not set to an instance of an object
MapControls.SwitchFunctions (System.String func) (at Assets/_Scripts/MapControls.cs:148)
MapControls.Awake () (at Assets/_Scripts/MapControls.cs:59)


========== Error End



========== MapControls Script Code

Map/Marker Definition:

private OnlineMaps map;
   
private OnlineMapsMarker marker;

Add Markers Script:

public void SetMarkers(double tempLng, double tempLat, Texture2D tempTex, string tempLabel) {


    Debug.Log(" ");
       
    Debug.Log("SetMarkers - lng: " + tempLng + " lat: " + tempLat);

    Debug.Log("SetMarkers - Texture: " + tempTex.name);

    Debug.Log("SetMarkers - Label: " + tempLabel);


    //// Actual Debug Results:
    SetMarkers - lng: -120.071841 lat: 39.26079

    SetMarkers - Texture: MarkerTextures_Blue

    SetMarkers - Label: Hwy 267 at Brockway Summit

    ////

    map.AddMarker(tempLng, tempLat, tempTex, tempLabel);


}

========== End Of MapControls Script Code



========== Calling Script Code

    double tempLng = double.Parse(tempRow.longitude.ToString());

    double tempLat = double.Parse(tempRow.latitude.ToString());


    Debug.Log("LoadCADistrict - lng: " + tempLng + " lat: " + tempLat);

    Debug.Log("LoadCADistrict - Texture: " + markerTexture.name);

    Debug.Log("LoadCADistrict - Label: " + tempRow.locationName);


    //// Actual Debug Results:
    LoadCADistrict - lng: -120.071841 lat: 39.26079

    LoadCADistrict - Texture: MarkerTextures_Blue

    LoadCADistrict - Label: Hwy 267 at Brockway Summit

    ////

    MapControls.Instance.SetMarkers(tempLng, tempLat, markerTexture, tempRow.locationName);



========== End Of Calling Script Code



Update: I Changed the call to feed actual data to the MapControls.SetMarkers function and now I ge the follwoing error:

======== Begin Error

NullReferenceException: Object reference not set to an instance of an object
MapControls.SetMarkers (Double tempLng, Double tempLat, UnityEngine.Texture2D tempTex, System.String tempLabel) (at Assets/_Scripts/MapControls.cs:193)
CCTVFileLoader.LoadCADistrict () (at Assets/_Scripts/CCTVFileLoader.cs:851)
CCTVFileLoader.StartMeUpCCTV () (at Assets/_Scripts/CCTVFileLoader.cs:86)
CCTVFileLoader.Start () (at Assets/_Scripts/CCTVFileLoader.cs:78)

========= End Error

Re: Null Error on AddMarker

Hello.

If you are asking about errors in your script, attach the entire script.
I do not know what's in line 148 and 193 of your script.

Kind Regards,
Infinity Code Team

Re: Null Error on AddMarker

OnlineMaps.instance is not available in Awake and OnEnable.
This is available in Start or later.

There are many ways to solve this problem:
1. Make the map field public, and set the field using the inspector.
2. Make a map property instead of the field that will return OnlineMaps.instance.
3. Call OnlineMaps.instance directly.
4. Make MapControl.Init method, which will be called at the beginning of CCTVFileLoader.Start, and will set the correct reference to the map.

Kind Regards,
Infinity Code Team

Re: Null Error on AddMarker

Alex,

Thanks for the info! I used option 1 and set it as public and put the map into the inspector and that solved the issue!

Cheers,

Neil