Home > other >  C # information of objects, an error Object reference not set to an instance of an Object
C # information of objects, an error Object reference not set to an instance of an Object

Time:09-18

 using UnityEngine; 
Using System. The Collections;
Using System. IO;
Using UnityEditor;
Public class JSON: MonoBehaviour {
[MenuItem (" Tools/CreateJSON ")]
The static void WriteJson ()
{
String path=Application. DataPath + "/PartMessage. Json";
GameObject obj=Selection. ActiveGameObject;
If (obj. Name!="Part")
{
Throw new System. Exception (" Selection Model ");
}
DateVO date=new DateVO ();
For (int I=1; I & lt;=obj. Transform. ChildCount; I++)
{
PartVO part=new PartVO
{
Pos=obj. Transform. GetChild (I). LocalPosition,
Rots=obj. Transform. GetChild (I). LocalEulerAngles,
Scale=obj. Transform. GetChild (I). LocalScale
};
Date. With parts. The Add (part);//this line have problems
}
String s=JsonUtility. ToJson (date);
File. WriteAllText (path, s);
AssetDatabase.Refresh();
}

CodePudding user response:

The current problem is a null pointer exception, this exception in appeared, there will be detailed information display is which lines appear,
So see this exception information, can be completely accurate positioning line to the code
Find the error code lines after judgment: object. The method, or is that object. The properties and so on, are all could trigger the exception,
With 20 lines, for example, it is a hypothesis, which is wrong line need your console access:
The original code is:
Pos=obj. Transform. GetChild (I). LocalPosition,
Where
Obj. The transform, is attributes for the transform object obj, if obj is null, cause this exception
Obj. Transform. GetChild (I) is the obj. Transform the transform object access GetChild method, if the transform is null, this exception, of course, the current situation, can't be established, because if, in the obj. The transform is triggered
Obj. Transform. GetChild (I) localPosition is obj. The transform. The GetChild (I) access to transform object access localPosition attribute, if the transform is null, this exception,

So, a null reference exception, caused the possibility of more, need to deal with the

CodePudding user response:

In code, suspicious objects are:
Date. With parts. The Add (part);//if there are any problems
Date. Parts of the object should be a List, but it does not instantiate objects, should be a statement, the lack of instantiation,
  • Related