Autowire mongotemplate is giving null value

Inorder to populate my mongoDB i have written a java file which contains :-
@Autowired
private static MongoTemplate mongoTemplate
But in the line 56 mongoTemplate.save(q, “Questions” ); I am getting nullPointerException.
I have debugged and came to know that mongoTemplate is null.

How to get a bean of mongoTemplate ?

I have also used @Component above the class file.

So,

You require a spring context in order to get the beans autopopulated.
If you are trying to do it via main function, then it will not work that way…

may be you will need to expose another api, which will use MongoTemplate.

How should i go for it? Which api?

I am stuck as i am unable to figure out how to import the values in my mongoDB

Finally i have populated the mongoDB but i am not getting the desired output during GET Request

{“questions”:[]}

This is the output that i am getting

In the below images i have noticed that my JSON is not correctly mapping with the POJO class.
Can anyone help me with that?


Even the options seem to be coming as empty right?

Do you have getters/setters for this class? It’s very hard for TAs to debug buildouts code - we can give some suggestion, but goal for buildouts is that you are able to write code and debug like a pet project.

I like the way you have debugged so far finding out the question entity has title populated but not other fields. Continue breaking down the problem further - find out why title is getting populated. One thing I see is title is a simple word, but questionId has two parts - question + Id. See if mongo stores data in a diff way

Hi, Thanks for the help i figured it out the error.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.