- Home /
Datatable.AsEnumerable()
Datatable.AsEnumerable() giving me the error NullReferenceException: Object reference not set to an instance of an object
using UnityEngine;
using System.Data;
using System.Linq;
using System;
public class CAS_Test : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
DataTable testTable = new DataTable("testTable");
Console.WriteLine(testTable.AsEnumerable());
}
}
Error
NullReferenceException: Object reference not set to an instance of an object System.Data.DataTableExtensions.AsEnumerable (System.Data.DataTable source) (at :0) CAS_Test.Start () (at Assets/CAS_Test.cs:12)
I have also tested with data in the data table. This happens only in Unity . I tested in with a c# compiler and the two lines works.
I need this to use Linq query.
What is the actual exception being logged. Are you sure it's that line? The docs seem to think that it doesn't throw that exception.
I mean I cant imagine there is a case in a .net provided class where construction fails. Especially in that manner (if it's even possible at all to set this to a null reference in the constructor (I am pretty sure it isn't)).
It is in that line. I have updated the question with complete code and error message.
Answer by harti177 · Jan 08, 2021 at 07:01 PM
Using ".Net 4.x" seems to resolve the issue.
The setting is under ProjectSetting -> Player -> OtherSettings -> Api Compatibility level
Your answer
![](https://koobas.hobune.stream/wayback/20220613022910im_/https://answers.unity.com/themes/thub/images/avi.jpg)