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 7929055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:56:48+00:00 2026-06-03T19:56:48+00:00

I have the following Java code – import java.lang.reflect.Field; public class AnnotationTest { public

  • 0

I have the following Java code –

import java.lang.reflect.Field;

public class AnnotationTest
{
    public @interface Size
    {
        int size();
        int location();
    }

    @Size(size = 40, location = 85)
    private String firstName;

    @Size(size = 1, location = 21)
    private String middleInitial;

    @Size(size = 50, location = 115)
    private String lastName;

    public static void main(String[] args)
    {
        AnnotationTest t = new AnnotationTest();

        Class<? extends AnnotationTest> classInstance = t.getClass();

        for (Field f : classInstance.getDeclaredFields())
        {
            Size s = f.getAnnotation(Size.class); 
            int size = s.size(); // this is line 29
            int location = s.location();

            System.out.println("size = "+ size);
            System.out.println("location = "+location);
        }

    }
}

The error I get is

Exception in thread "main" java.lang.NullPointerException
    at com.stackoverflowx.AnnotationTest.main(Demo.java:125

How to access the annotation fields correctly?

  • 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-06-03T19:56:50+00:00Added an answer on June 3, 2026 at 7:56 pm

    By default, annotations aren’t available at runtime. You need to add @Retention(RetentionPolicy.RUNTIME) to your annotation definition to make it available for runtime processing. For example:

    @Retention(RetentionPolicy.RUNTIME)
    public @interface Size {
    

    In practice, you should also check whether or not the field actually has a given annotation with Field.isAnnotationPresent before actually trying to get the annotation off of the field.

    Also, it’s also good practice to specify what types of elements your annotation belongs on with @Target. So your example would then be:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Size {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following Java code: import java.util.Arrays; import java.util.Collections; public class Test {
I have the following Java code: public class A { private int var_a =
Say suppose I have the following Java code. public class Example { public static
I have following class in Java code: public class CHRTreeCreator extends IndexCreator { ...
I have the following Java code: public static BufferedImage createImage(byte[] data, int width, int
I have the following java code: public class CheckInnerStatic { private static class Test
I have the following Java code: import java.util.concurrent.*; class Foo{ static Semaphore s =
I have the following (Java) code: public class TestBlah { private static final String
I have the following Java code: public class FirstActivity extends Activity implements OnClickListener {

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.