Null pointer exception when working with generics in java
since I have not been using generics for some time, I'm quite confused at this example.

I have the following base abstract class:

    public abstract class ExcelReader<T>{
         protected T type;
         protected GenericResolver resolver;
         
         public ExcelReader(){
            super();
            resolver=ResolverFactory.createResolver(type.getClass()); 
         }
    }

now my subclass is the following:

    public class POIReader<T> extends ExcelReader<T>{
         
    }
    //...Implementation of methods ommited for brevity

Now in my service I'm creating a new object in the following way:

   

    ExcelReader<MessageDTO> reader=new POIReader<MessageDTO>();

However, when the ExcelReader constructor is called the type attribute has null, and in consecuence throwing a NullPointer exception when creating the resolver. 

I think you can get the idea of what I'm trying to do with the code fragments above, and I have seen examples using an attribute field to save the Parametized Class type.

However I'm quite confused in why I'm getting null in the type attribute, and how could I avoid it.
Thanks a lot.
   
    
Null pointer exception when working with generics in java
===

since I have not been using generics for some time, I'm quite confused at this example.

I have the following base abstract class:

    public abstract class ExcelReader<T>{
         protected T type;
         protected GenericResolver resolver;
         
         public ExcelReader(){
            super();
            resolver=ResolverFactory.createResolver(type.getClass()); 
         }
    }

now my subclass is the following:

    public class POIReader<T> extends ExcelReader<T>{
         
    }
    //...Implementation of methods ommited for brevity

Now in my service I'm creating a new object in the following way:

   

    ExcelReader<MessageDTO> reader=new POIReader<MessageDTO>();

However, when the ExcelReader constructor is called the type attribute has null, and in consecuence throwing a NullPointer exception when creating the resolver. 

I think you can get the idea of what I'm trying to do with the code fragments above, and I have seen examples using an attribute field to save the Parametized Class type.

However I'm quite confused in why I'm getting null in the type attribute, and how could I avoid it.
Thanks a lot.
   
    