File.exists提供空引用异常,而不是返回false。c# if(File.Exists(Application.persistentDataPath+“/users/”+Input.GetComponent().text+“.dat”))

File.exists提供空引用异常,而不是返回false。c# if(File.Exists(Application.persistentDataPath+“/users/”+Input.GetComponent().text+“.dat”)),c#,nullreferenceexception,system.io.file,C#,Nullreferenceexception,System.io.file,当该行找不到文件时,它总是导致空引用异常,而不是返回false。文件。Exists不是问题。考虑像这样修复代码 if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat")) if(Application.persistentDataPath!=null&&Input!=null&&Input.GetComponent()!=null&&In

当该行找不到文件时,它总是导致空引用异常,而不是返回false。

文件。Exists
不是问题。考虑像这样修复代码

if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))
if(Application.persistentDataPath!=null&&Input!=null&&Input.GetComponent()!=null&&Input.GetComponent().text!=null)
{
if(文件.Exists(Application.persistentDataPath+“/users/”+Input.GetComponent().text+“.dat”))

实际上不是,其中的代码给出了空引用异常,请调试您的代码以确定为什么Extract
Application.persistentDataPath
Input.GetComponent()
out调用,并在调试器中检查两者的值。当我单击错误消息时,它给出了这行代码的错误,并且没有任何其他代码会触发,除非再次执行此if语句,
File.Exists
不是问题,其中的代码是
NullReferenceException
您试图访问
null
对象的成员(属性、方法、字段)。因此,其中之一是
null
=>
应用程序
输入
GetComponent()
@RufusL我刚刚修复了它们。注释中的建议对于这个问题来说是不够的,但我发现asker并没有真正理解这个问题。您的最后一个条件应该是
Input.GetComponent()!=null
    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"))