Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7020201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:17:16+00:00 2026-05-27T23:17:16+00:00

since I have not been using generics for some time, I’m quite confused at

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:17:16+00:00Added an answer on May 27, 2026 at 11:17 pm

    I don’t think you are trying to use the generics for the thing they were intended to use. Generics are a kind of “syntactic sugar” usable in compile-time only as an additional level of code validation. What you seem to want to achieve – correct me if I am wrong – is to provide the generic type to your POIReader class and then be able (in runtime) to access this type’s class object. Is this correct?

    The way you could use the generics in your example – but I am not sure this will be what you want to achieve – is for something like this:

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

    and then:

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

    This way, thanks to using generics the compiler will not allow you to pass anything that is not assignable to a MessageDTO class as the argument to POIReader‘s constructor – but you still must assign something to that value field in order to be able to call the getClass() method. This is because, due to type erasure of generics (an unfortunate backwards-compatibility thing), generics you provide are not available in runtime, only in compile-time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using TortoiseSVN in a Windows environment for quite some time. It seems
Since CCKeyDerivationPBKDF is not available until after iOS 5.0, people have suggested using the
i have been using playframework since 2 years. Framework has JPA on streoids feature.
I have been trying to solve this for 2 weeks and I have not
I am not sure my title is correct or not since I have no
Since vector elements are stored contiguously, I guess it may not have the same
Since I'm not an Php programmer I have to ask you, I have to
Since JavaScript is not derived from Java, why does it have Java in the
Its been a while since i have coded c++ and i have forgot an
I would like to publish a website using msbuild. Since I have a website

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.