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:
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.