【发布时间】:2019-07-02 14:04:17
【问题描述】:
尝试使用反向获取位置名称。我正在使用 Unity 的 JSONutility 来解析从 google API 获得的 JSON 字符串。信息没有存储在我的本地对象中,所以我总是得到一个空引用异常。
为了检索信息,我使用了 UnityWebRequest 类。我将信息写入一个简单的文本文件以检查请求是否实际被发送,并且信息被写入文本文件,但是当我想用它在游戏中创建一些东西时,它不起作用。
我使用http://json2csharp.com/ 基于我的 JSON 文件创建了我的 C# 类。
我在这里验证了 JSON 文件 https://jsonlint.com/
//This is the c# file I created out of the JSON format gotten from google
//It is validated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RootObject
{
public PlusCode plus_code;//This doesn't get created at all
public List<Result> results;//This creates a list of size 11, however the list is always empty and I don't know why.
public string status;//For some reason this is the only variable that gets populated
public static RootObject CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<RootObject>(jsonString);
}
}
public class PlusCode
{
public string compound_code;
public string global_code;
}
public class AddressComponent
{
public string long_name;
public string short_name;
public List<string> types;
}
public class Northeast
{
public double lat;
public double lng;
}
public class Southwest
{
public double lat;
public double lng;
}
public class Bounds
{
public Northeast northeast;
public Southwest southwest;
}
public class Location
{
public double lat;
public double lng;
}
public class Northeast2
{
public double lat;
public double lng;
}
public class Southwest2
{
public double lat;
public double lng;
}
public class Viewport
{
public Northeast2 northeast;
public Southwest2 southwest;
}
public class Geometry
{
public Bounds bounds;
public Location location;
public string location_type;
public Viewport viewport;
}
public class PlusCode2
{
public string compound_code;
public string global_code;
}
public class Result
{
public List<AddressComponent> address_components;
public string formatted_address;
public Geometry geometry;
public string place_id;
public List<string> types;
public PlusCode2 plus_code;
}
这就是我试图调用它的地方。
public void ShowStaticMap()
{
//Grab GEO Data
StartCoroutine(FetchGeoData());
newCase.date = DateTime.Today.ToString();
StartCoroutine(GetLocationName());//This runs.
}
IEnumerator GetLocationName()
{
UnityWebRequest www = UnityWebRequest.Get(geoUrl);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
var TestRootObject = RootObject.CreateFromJSON(www.downloadHandler.text).plus_code.compound_code;// This is line 147. Results in a nullreference exception.
/*
If I am to try this line, it will work without a problem.
RootObject.CreateFromJSON(www.downloadHandler.text).status;
*/
Debug.Log(TestRootObject);//There is nothing here.
}
}
NullReferenceException:对象引用未设置为对象的实例 est.UI.CreateCaseScreen+d__19.MoveNext () (在 Assets/Esteban/Scripts/ScreenTypes/CreateCaseScreen.cs:147) UnityEngine.SetupCoroutine.InvokeMoveNext(System.Collections.IEnumerator 枚举器,System.IntPtr returnValueAddress)(在 C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
【问题讨论】:
标签: c# unity3d coroutine reverse-geocoding