Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Guid & GENERIC ERROR: Object reference not set to an instance of an object. #6

Closed
daveriedstra opened this issue Mar 14, 2017 · 17 comments

Comments

Projects
None yet
2 participants
@daveriedstra
Copy link

commented Mar 14, 2017

After trying to apply some new model changes today, TypeGen was giving me the following errors. I'm not sure how to trace it down. Possibly related is that it still has a problem with System.Guid (first stack), but even after I overrode all those to TsType.String, TypeGen gave the second error.

(I know this is not much to go on, but I'm at wit's end. If you want me to look for particular things in our update to debug this, I'll certainly oblige.)

APPLICATION ERROR: Generated type must be either a C# class or enum. Error when generating type System.Guid
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateType(Type type)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateType(Type type)
   at TypeGen.Core.Generator.Generate(Type type)
   at TypeGen.Core.Generator.Generate(Assembly assembly)
   at TypeGen.Cli.Program.<>c__DisplayClass10_0.<Generate>b__0(IEnumerable`1 acc, Assembly assembly)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at TypeGen.Cli.Program.Generate(String projectFolder, String configPath, Boolean verbose)
   at TypeGen.Cli.Program.Main(String[] args)
GENERIC ERROR: Object reference not set to an instance of an object.
   at TypeGen.Core.Extensions.TypeExtensions.<>c__DisplayClass0_0.<GetInterface>b__0(Type i)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at TypeGen.Core.Extensions.TypeExtensions.GetInterface(Type type, String name)
   at TypeGen.Core.Business.TypeService.IsDictionaryType(Type type)
   at TypeGen.Core.Business.TypeService.IsCollectionType(Type type)
   at TypeGen.Core.Business.TypeService.GetFlatType(Type type)
   at TypeGen.Core.Business.TypeDependencyService.GetMemberTypeDependencies(Type type)
   at TypeGen.Core.Business.TypeDependencyService.GetTypeDependencies(Type type)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateNotMarked(Type type, String outputDirectory)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateType(Type type)
   at TypeGen.Core.Generator.GenerateTypeDependencies(Type type, String outputDir)
   at TypeGen.Core.Generator.GenerateClass(Type type, ExportTsClassAttribute classAttribute)
   at TypeGen.Core.Generator.GenerateType(Type type)
   at TypeGen.Core.Generator.Generate(Type type)
   at TypeGen.Core.Generator.Generate(Assembly assembly)
   at TypeGen.Cli.Program.<>c__DisplayClass10_0.<Generate>b__0(IEnumerable`1 acc, Assembly assembly)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at TypeGen.Cli.Program.Generate(String projectFolder, String configPath, Boolean verbose)
   at TypeGen.Cli.Program.Main(String[] args)
@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 14, 2017

The first error is expected, as you need to specify a TsType for types that can't be generated (or you don't want to generate).

The second error though should not be happening and there is clearly something wrong with the code.
I'll look into this and should be able to fix it in a couple of days. I'll post any updates in this thread (as "usual").

Thanks for your valuable feedback!

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 14, 2017

Okay, my misunderstanding about System.Guid, thanks for clearing that up, and thanks again for this project!

@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 14, 2017

You're welcome!

The second issue still happens, right? (null reference even if you set TsType.String for Guids)

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 14, 2017

Yep, correct.

@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 16, 2017

Unfortunately I'm not able to replicate the null reference error.

Could you paste the definition of the class being generated? You can change the class name or the names of the properties; I'm mostly concerned with the actual property types.

I'm suspecting the null reference can result from some other property (not the guid one), because this error should not occur if a member has TsType attribute (that's why an example would help).

Thanks

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 16, 2017

Several classes are being generated (our model is pretty complex, which makes TypeGen very valuable!), but I think these might be the sources of the problem. These classes are defined in the model:

public enum FooType
{
    A,
    B = 1,
    C = 2,
    D = 3,
    E = 4,
    F = 5
}

public class Foo
{
    public FooType Id { get; set; }

    public string A { get; set; }

    public string B { get; set; }

    public int C { get; set; }

    public List<Bar> MyBars { get; set; } = new List<Bar>();
}

public class Bar
{
    public int Id { get; set; }

    public int FooId { get; set; }
    public Foo Foo { get; set; }

    public int AId { get; set; }
    public A A { get; set; }

    public int B { get; set; }

    public List<C> MyCs { get; set; } = new List<C>();

    public List<D> MyDs { get; set; } = new List<D>();
}

and while FooType and Foo are generated (see below), Bar isn't.

/**
* This is a TypeGen auto-generated file.
* Any changes made to this file can be lost when this file is regenerated.
*/

export enum FooType {
    A = 0,
    B = 1,
    C = 2,
    D = 3,
    E = 4,
    F = 5,
}
/**
* This is a TypeGen auto-generated file.
* Any changes made to this file can be lost when this file is regenerated.
*/

import { FooType } from "./foo-type";
import { Bar } from "./bar"; // <-- this file isn't generated

export class Foo {
    id: FooType;
    a: string;
    b: string;
    c: number;
    myBars: Bar[];
}
@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 16, 2017

When I tried an example based on the classes above (I copy-pasted them), it generated ok.
Which leads me to believe that maybe the A, C or D classes have something that's causing the crash.

Can you paste their definitions as well? And perhaps their dependant classes too (if there are not very many)?

Can you also specify which of these types have ExportTs attributes?

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 16, 2017

Ok, here goes. Here are some of the definitions in C# (since some of them didn't successfully generate TS definitions). None of them have ExportTs attributes, they're all linked in as dependencies. Hopefully this is useful, it's impractical to copy/edit/paste in any more.

A is big and has many dependents but generated ok. It extends SuperA, which also generates fine.

C (not successfully generated):

public class C
{
    public int A { get; set; }

    public int B { get; set; }

    public bool C { get; set; }

    public bool D { get; set; }

    public EClass E { get; set; }

    public EClass D { get; set; }

    public List<FClass> Fs { get; set; } = new List<FClass>();

    public Bar Bar { get; set; }
    public int BarId { get; set; }
}

dependent EClass (generated ok):

public class EClass : SuperA<int>
{
    public const string A = "someUrl";

    [StringLength(250), Required]
    public GClass B { get; set; }
    
    [StringLength(250), Required]
    public string C { get; set; }
    
    [StringLength(250), Required]
    public string D { get; set; }
    
    public int E { get; set; }
    
    [StringLength(250), Required]
    public string F { get; set; }
    
    [StringLength(1000)]
    public string G { get; set; }
    
    public bool H { get; set; }
    
    [StringLength(10)]
    public string I { get; set; }

    public string Link 
    {
        get
        {
            return A + F + "/" + D + I;
        }
    }

    public List<FClass> Js { get; set; } = new List<FClass>();
}

mutual dependent FClass (generated ok):

public class FClass : SuperB<int>
{
    public FClass() { }

    public FClass(string val)
    {
        A = val;
    }

    [Display(Name = "FClass")]
    [StringLength(512)]
    public string A { get; set; }

    public HClass B { get; set; }

    public IClass C { get; set; }

    public D D { get; set; } // same D as type of Bar.MyDs
    public int? DId { get; set; }
}

D (not generated):

public class D : SuperB<int>, SuperC
{
    public DStatusEnum GetCurrentStatus(string a)
    {
    	// logic removed
    }

    [TsType(TsType.String)]
    public Guid? AId { get; set; }

    public JClass A { get; set; }

    [TsType(TsType.String)]
    public Guid? BId { get; set; }

    public int? CId { get; set; }
    public virtual DTypeClass C { get; set; } // DTypeClass is big with mostly string and int members

    public int? BarId { get; set; }
    public Bar Bar { get; set; }

    public string E
    {
        get
        {
        	// logic removed
        }
    }

    // A here is same class as in Bar definition
    public D Init(A a, FooType footype)
    {
    	// logic removed
    }

    public void G()
    {
        // logic removed
    }

    public string H()
    {
        // logic removed
    }
    public string I()
    {
        // logic removed
    }

    public DateTime? J { get; set; }
    public DateTime? K { get; set; }

    public IList<FClass> Fs { get; set; } = new List<FClass>();

    public DateTime? L
    {
        get;
        set;
    }

    public DateTime? M
    {
        get;
        set;
    }

    public NEnum N
    {
        get;
        set;
    }

    [TsType(TsType.String)]
    public Guid? OId { get; set; }
}
@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 17, 2017

I'm still not able to replicate the null reference.

However, based on the stack trace I've been able to track down the place which might cause an error.
Version 1.5.4 has an additional null check, which may solve the problem - please try it and let me know if it works.

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 20, 2017

Updating the NuGet package and the .exe/.dll in the project root to 1.5.4 resulted in the TypeGen namespace not being found during project build. Downgrading the NuGet package to 1.5.3 but leaving the .exe/.dll at 1.5.4 lets the project build normally and seems to get past the missing reference error when generating types, however, it's choking on an apparent System.DateTimeOffset which I can't track down. Returning .exe/.dll to 1.5.3 brings back the original object reference error.

@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 22, 2017

As for the namespace not being found - can you try running nuget locals -clear all in the windows console? This will clear NuGet cache, which may be the problem here (the upgrade to 1.5.4 "works on my machine").

In case of the System.DateTimeOffset problem - it looks like either some property is of System.DateTimeOffset type, or it tries to generate some system type which has System.DateTimeOffset somewhere in its definition. Can you check if you have TsType attribute on all system-related types? The only system type (except for the primitive types) that is ok to use with TypeGen is DateTime.

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 22, 2017

Unfortunately wiping the nuget cache didn't help the namespace reference issue. I'm not sure where to look or what to do next...

I'm having trouble tracking down the DateTimeOffset thing. It's probably on my end, so I'll keep looking. It would be really helpful to have a reference to the class TypeGen's trying to generate in the crash error statement for cases like this.

@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 22, 2017

The TypeGen namespaces issue sounds a bit worrying. Can you try if it works for a newly created project, to confirm if it's a TypeGen issue rather than some other problem?

About the DateTimeOffset - can you paste the exception output (i.e. what is the exception message + stack trace)? I'll try to add more accurate logging and release it as the next minor version.

Thanks

@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 23, 2017

I've just released 1.5.5.

What I've added is more accurate error logging - upon "application error" it should yield what type was being generated when the error occurred.

As for the namespace issue - I can't currently find what's wrong. Both 1.5.4 and 1.5.5 seem to be working with .NET Core 1.1/1.0 application when I test it locally (Visual Studio 2017 Community; Windows 10). Maybe try 1.5.5 and see if it still gives an error? Additionally, not much has changed in terms of code/configuration since 1.5.3, so it looks to me like some cache error or the like.

One last thing that comes to my mind is to delete TypeGen from the nuget packages folder (C:\Users\[user]\.nuget\packages) and install it again. Or try with a newly created project (as I mentioned in the previous post).

I'll keep the namespace issue in mind and I'll post here if I find a solution, but for the time being I'm going to assume that this is not TypeGen-specific error and is rather dependent on local environment/cache etc.

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 23, 2017

This is perfect -- thank you! The namespace reference issue is no longer present in 1.5.5, and everything works fine.

The error logging helped me trace the System.DateTimeOffset reference to Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUser which I wouldn't have caught otherwise.

Maybe the 1.5.4 issues have something to do with VS 2015 or with my configuration. My .nuget/packages/TypeGen/1.5.4 directory is flat -- there are no lib/ or tools/ directories and all of the DLLs and such I expect to see in those folders are right in 1.5.4/. The folders for 1.5.3 and 1.5.5 are properly structured, though. This 1.5.4 behaviour is the same for a new .NET Core project.

@jburzynski

This comment has been minimized.

Copy link
Owner

commented Mar 23, 2017

I'm glad to hear that!

I think I know what was the problem with 1.5.4... I must have confused the chocolatey package with the nuget package when uploading to NuGet repository (both are nuget packages, but with different folder structure). Sorry for the confusion...

I wonder why it worked when I downloaded it from NuGet though, but that's something I won't be giving too much thought to. I'll just try to be more careful next time :)

Cheers!

@daveriedstra

This comment has been minimized.

Copy link
Author

commented Mar 23, 2017

That's funny! I'm glad it's sorted out now. As always, thank you very much for this great package!

@jburzynski jburzynski closed this Apr 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.