-2

I am not asking what is null pointer exception here.I have debugged the project and saw where my null pointer is coming.My flow for project is:

enter image description here

My Api is:

package com.cache.api;

@Path("/myresource")
public class MyResource  {

    @Inject
    private CacheHelper cacheHelper;

    @Inject
    private DtoPlan dtoPlan;

    @GET
    @Path("/putAllInDto")
    @Produces(MediaType.APPLICATION_JSON)
    public String putAllInDto() throws Exception {

       CachePlan cf=CacheFactory.getPlan(PlanChooser.DTO);
       cf.putInCache(); //data are loaded in DtoPlanImpl Class

        MemberDTO md=cf.getMemberDtos();  //data comes from here with cf object

    //    MemberDTO md1=dtoPlan.getMemberDto();  //java.lang.NullPointerException when I use dtoPlan.memberDto();

//
//        System.out.println(md.getMemberName());
        return "insertedindto";
    }


}

The cachePlan interface is:

package com.cache.plan;

    public interface CachePlan {

        public  void putInCache() throws ExecutionException, InterruptedException ;

        public MemberDTO getMemberDtos() throws ExecutionException, InterruptedException ;

        }

My DtoPlanImpl.java class is.Here I needed to implement new interface DtoPlan.

package com.cache.impl;


    @Stateless
    public class DtoPlanImpl implements CachePlan,DtoPlan
    {

        private CacheManager cacheManager;

        private Cache<String,MemberDTO> memberCache;

        private static final String CACHE_MEMBER_DTO_PARAMETER="cache_member_dto_type";

        private MemberService memberService = CDI.current().select(MemberService.class).get();


        public DtoPlanImpl(){
            System.getProperties().setProperty("java -Dnet.sf.ehcache.use.classic.lru", "true");
            cacheManager= CacheManagerBuilder
                    .newCacheManagerBuilder().build();
            cacheManager.init();
        }


        @Override
        public void putInCache()  {
            System.out.println("putting data from dto");

            memberCache = cacheManager
                    .createCache("cacheOfMemberCache", CacheConfigurationBuilder
                            .newCacheConfigurationBuilder(
                                    String.class,MemberDTO.class,
                                    ResourcePoolsBuilder.heap(1000000000)).withExpiry(Expirations.timeToLiveExpiration
                                    (Duration.of(60000,
                                    TimeUnit.SECONDS))));

            memberCache.put(CACHE_MEMBER_DTO_PARAMETER,memberService.getMemberDTO());

            MemberDTO mD=memberCache.get(CACHE_MEMBER_DTO_PARAMETER);
            System.out.println(mD.getMemberName());

         }

        @Override
        public MemberDTO getMemberDtos() {
            MemberDTO mD=memberCache.get(CACHE_MEMBER_DTO_PARAMETER);
            return mD;
        }


        @Override
        public MemberDTO getMemberDto() throws ExecutionException, InterruptedException {
            MemberDTO mD=memberCache.get(CACHE_MEMBER_DTO_PARAMETER);
             return mD;
        }


    }

My DtoPlan interface is:

public interface DtoPlan {

    public MemberDTO getMemberDto() throws ExecutionException, InterruptedException;


}

So what is the problem is that when i use CacheFactory cf as:

MemberDTO md=cf.getMemberDtos();  //data comes from here with cf object

But when I use:

 MemberDTO md1=dtoPlan.getMemberDto();  //java.lang.NullPointerException 

I debugged the program and visualized from where the null pointer excpetion is coming: Its coming from this Class DtoPlanImpl at line MemberDTO mD=memberCache.get(CACHE_MEMBER_DTO_PARAMETER);:

@Override
    public MemberDTO getMemberDto() throws ExecutionException, InterruptedException {
        MemberDTO mD=memberCache.get(CACHE_MEMBER_DTO_PARAMETER);
         return mD;
    }

The memberCache is being null here where I call from dtoPlan.getMemberDto();, but when I call from cf.getMemberDtos();,I am getting data.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Is `dtoPlan` being injected? If so, how? A NPE at `MemberDTO md1=dtoPlan.getMemberDto()` can only occur if `dtoPlan` is `null`! This means that this question does qualify as a duplicate of [what-is-a-nullpointerexception-and-how-do-i-fix-it](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Prashant Sep 17 '19 at 04:41
  • yes i debugged and saw dtoPlan Proxy for view class: com.cache.plan.DtoPlan of EJB: DtoPlanImpl constructed:true methodHandler={ProxyMethodHandler} – Ashwin Karki Sep 17 '19 at 04:45
  • If that is the case, your NPE is not really coming from this call: `dtoPlan.getMemberDto()` - – Prashant Sep 17 '19 at 04:46
  • but this also appeared dtoPlan=Cannot find local variable 'dtoPlan' – Ashwin Karki Sep 17 '19 at 04:48
  • Your code does not show a local variable `dtoPlan`. – Prashant Sep 17 '19 at 04:50
  • its being injected or not I am not sure – Ashwin Karki Sep 17 '19 at 04:50
  • `@Inject` is just a specification. There needs to be a provider of dependency injection so that the injection works. – Prashant Sep 17 '19 at 04:51
  • yes i have used CDI,and provided the beans configuration.The @Inject is working fine for other interface. – Ashwin Karki Sep 17 '19 at 04:54
  • its being injected but my memberCache is getting null when i debugged – Ashwin Karki Sep 17 '19 at 04:56

1 Answers1

0

You are initializing memberCache in putInCache() method instead of initializing in constructor. With your design, you need to put something in the cache before you can retrieve something. So either initialize it in the constructor or put something in it.

Also note that by default Guice returns a new instance each time it supplies a value. So you may be using one instance while putting stuff into the cache, hence initializing memberCache, but using another instance while retrieving data, in which memberCache is null. You can use @Singleton annotation to inject the same instance of DtoPlan everywhere.

And looks like you are creating a new instance of cache every time you need to put something in it. So you lose the previously stored values. This whole code looks flawed.

Kartik
  • 7,677
  • 4
  • 28
  • 50
  • where should i use singleton annotation? can u please show me ? – Ashwin Karki Sep 17 '19 at 04:58
  • i just need to instantiate cache once,if i put in constructor it will be initialized every time when i will make the object or refresh the web page – Ashwin Karki Sep 17 '19 at 05:00
  • That's why you need singleton dependency injection. You can put that annotation on `DtoPlanImpl` class. But it's highly likely that you'll still face issues. You'll need to fix your `putInCache` method and constructor and maybe other things. I think this question is too broad. You should read more about Java. – Kartik Sep 17 '19 at 05:03
  • yes you were right i put the membercache in constructor and NPE has gone,but my cache needs to only initialize once,I am getting problem there,when I will put inside contructor,then if I hit the api for 100 times then cache will load data for 100 times. – Ashwin Karki Sep 17 '19 at 05:04