0

Repository:

@Repository 
public interface NodeRepo extends Neo4jRepository<BaseNode, Long> {
 
     @Query("MATCH (n) WHERE n.nodeId=$nodeId RETURN n")
     BaseNode findByNodeId(String nodeId); 
}

Service:

@Component 
public class NodeQueryService {

     @Autowired
     NodeRepo nodeRepo;
 
     @Transactional
     public BaseNode fetchPNodeById(String id){
         return nodeRepo.findByNodeId(id);
     } 
}

I am using this service in a class where i used

@Autowired
NodeQueryService nodeQueryService;

But it is giving me this error services.NodeQueryService.fetchPNodeById(String)" because "this.nodeQueryService" is null

What should i do to make it work ?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Can you please add the import statements as well to the original post. I guess your @Repository annotation is not the spring stereotype annotation and it can be imported from a different library. It is just a possibility and I cannot surely say unless I see the import statements. So please double check it. thanks! – Sankalpa Wijewickrama Aug 11 '22 at 09:43
  • First the `@Repository` annotation is useless on a Spring Data repository it doesn't add anything. Second the fact you get a nullpointer isb ecause the class using the `NodeQueryService` isn't spring managed (at least not the instance you are using). Marking as a duplicate of https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null. Check also https://deinum.biz/2020-07-03-Autowired-Field-Null/ – M. Deinum Aug 11 '22 at 11:23

0 Answers0