I have a controller class which is below. I have a TagRepository
interface which extends JPA repository which I am using to save TagReader
instance to my DB and it works fine when I use it in my controller class. However, when I try to use tagRepository
in another class and try to save my TagReader
object from there it throws a null pointer exception.
The following logic works fine.
@RestController
public class Controller {
@Autowired
TagRepository tagRepository;
@Autowired
Rfid6204Connection rfid6204Connection;
@RequestMapping(value = "/test")
public void testRepoController(){
String tagid = "0x3504ACE6E0040E5147D516A6";
String serial ="00333478";
String departure ="2017-12-22T12:16:58.857";
String type = "ISOC";
TagReader tagReader = new TagReader(tagid,serial,departure,type,"5");
tagRepository.save(tagReader);
}
}
The following logic throws a null pointer exception.
@component
public class Rfid6204Connection{
@Autowired
static TagRepository tagRepository;
public static void test(TagReader tag){
tagRepository.save(tag);
}
}
Can someone please tell me what the issue is?