c# - File.exists 给出一个空引用异常而不是返回 false。C#
问题描述
if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))
此行在找不到文件时始终会导致空引用异常,而不是返回 false。
解决方案
File.Exists
不是问题。考虑像这样修复您的代码
if (Application.persistentDataPath != null && Input != null && Input.GetComponent<Text>() != null && Input.GetComponent<Text>().text != null)
{
if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))
推荐阅读
- python - PyGame 视频不同步
- node.js - 如何从 axios post API 请求中获取特定字段
- json - 将列表转储到 JSON 文件会在列表 [["x", "y","z"]] 中创建列表,为什么?
- sed - sed -n 函数在同一行重复调用
- azure - 在 Azure ARM 模板中执行复杂的脚本
- ruby-on-rails - Rails 在具有 has_many 关系的模型上使用“where”
- javascript - 如何制作高阶函数来处理其他函数并使它们可被递归`while`函数使用?
- r - 使用 FCC API 将 Lat/Lon 转换为县代码
- javascript - Vue的事件@click在下拉菜单中不起作用
- javascript - 有没有办法在函数调用中更改对对象的原始引用