ulou
ulou

Reputation: 5853

Can't connect with MongoDB

i have some problem with Java project, using Spring and MongoDB, code below.

I tried connect with database by jiraConfig.properties using jira.path and spring.data.mongodb.port, but it doesn't work and i can't find solution. Did i forgot about something?

public class JiraProjectBriefControllerImpl implements JiraProjectBriefController {



  @Autowired
    JiraProjectBriefRepository jiraProjectBriefRepository;

    @Override
    public void update() {
        JiraController jiraController = new JiraControllerImpl();

        List<ProjectBrief> projectBriefs = jiraController.getAllProjectsBrief();

         jiraProjectBriefRepository.save(projectBriefs);
    }

    @Override
    public List<ProjectBrief> getProjectsBrief() {
        return jiraProjectBriefRepository.findAll();
    }

    @Override
    public ProjectBrief findById(int id) {
        return jiraProjectBriefRepository.findById(id);
    }}

// second class

@Configuration
@EnableAutoConfiguration
public interface JiraIntegrationService {

    static void main(String[] args) {

        SpringApplication.run(JiraIntegrationService.class, args);

        JiraProjectBriefController jiraProjectBriefController = new JiraProjectBriefControllerImpl();

        jiraProjectBriefController.update();

        System.out.print(jiraProjectBriefController.getProjectsBrief());
    }

}

// Errors

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at pl.ie.service.JiraProjectBriefControllerImpl.update(JiraProjectBriefControllerImpl.java:23)
    at pl.ie.JiraIntegrationService.main(JiraIntegrationService.java:26)
    ... 6 more

Upvotes: 0

Views: 375

Answers (2)

duffymo
duffymo

Reputation: 308733

When you call new on an object, like you do in your main, that means Spring is out of the picture. The creation of the object and satisfying its dependencies are up to you.

You should not be calling new to create that object. Better to instantiate the Spring Bean factory and ask it to give you the instance you need with all its dependencies wired in.

This is a common misunderstanding by new Spring users. They call new and cannot understand why their Spring dependencies aren't wired.

Upvotes: 1

Andr&#233; Alves
Andr&#233; Alves

Reputation: 344

jiraProjectBriefRepository isn't initialized.

Upvotes: 0

Related Questions