1

I keep looking at my codes all day along, I can't find the cause. The repository (@Repository) is working just fine, it's the @Service field that I keep failing get it autowired, I keep struggling since everything looks fine

enter image description here

The dispatched servlet:

<mvc:annotation-driven />
<context:component-scan base-package="com" /> 

com.repository

Repo.java

package com.repository;
import com.domain.Student;
import java.util.List;

public interface Repo { public List<Student> getAllSiswa();  }

RepoImplement.java

import com.domain.Student;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class RepoImplement implements Repo {
 List<Student> ls = new ArrayList<>();

public RepoImplement(){  
        Student s1 = new Student();
        s1.setNama("paul");

        Student s2 = new Student();
        s2.setNama("robert");

        ls.add(s1);
        ls.add(s2);
}

    @Override
    public List<Student> getAllSiswa() {    
       return this.ls;
    }
}

package com.service;

Serve.java

import com.domain.Student;
import java.util.List;

public interface Serve {
    void changeName(String namaBaru);
    public List<Student> newList();
}

I'm suspecting there's something wrong over here

ServeImplement.java

import com.domain.Student;
import com.repository.Repo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ServeImplement implements Serve {
    @Autowired
    public Repo repo;  
    List<Student> s = repo.getAllSiswa(); // THIS IS SUSPECTING ME.

    @Override
    public void changeName(String namaBaru) {
        s.get(0).setNama(namaBaru); // get first Student, then update its name.
      }
    @Override
    public List<Student> newList() {
        return this.s;
    }
}

controller2.java (it's the mapping request for serving student)

package com.controlller;
import com.domain.Student;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.service.Serve;

@Controller
public class controller2 {

    @Autowired
    public Serve serv;

    @RequestMapping("/changename")
    public ModelAndView sdaf() {
        serv.changeName("New name");
        List<Student> list = serv.newList();
        return new ModelAndView("page2", "out", list);
    }
}

The error:

Error creating bean with name 'controller2': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.service.Serve com.controlller.controller2.serv; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serveImplement' defined in file [C:\Users\hans\Documents\NetBeansProjects\WebApplication2\build\web\WEB-INF\classes\com\service\ServeImplement.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.service.ServeImplement]: Constructor threw exception; nested exception is java.lang.NullPointerException

5
  • Ya. repo could be null. Do you see a NPE in the stacktrace? Dec 28, 2015 at 9:47
  • @VinodMadyalkar, thanks for the response, yes it's null pointing on List<Student> s = repo.getAllSiswa();, but I can't find why, since it looks that it gets wired correctly.. Dec 28, 2015 at 9:50
  • 1
    Put repo.getAllSiswa(); in a method marked as @PostConstruct Dec 28, 2015 at 9:51
  • @VinodMadyalkar, in the RepoImplement right after @Override ?? Dec 28, 2015 at 9:55
  • No. In ServeImplement :) Dec 28, 2015 at 10:00

2 Answers 2

4

You need to understand how autowiring, or in fact Java in general, works.

Spring must create an instance of your ServImpl class, and populate the field repo with the repository bean. It does that using reflection, but what it does is basically equivalent to the following code:

ServImpl s = new ServImpl();
s.repo = theRepoBean;

So, you see that s.repo becomes non-null after the constructor has been executed. And, while executing the constructor, the following line of code is executed:

List<Student> s = repo.getAllSiswa();

At that moment, repo hasn't been initialized yet. So it's null. So you get a NullPointerException.

Use constructor injection instead of field injection, or use a @PostConstruct annotated method.

And please, make your fields private instead of public.

That said, the goal of a repository is normally to get data from a database. And the students can be modified,deleted or created in the database. So initializing a field of your service and returning always the same list defeats the whole point of having a repo. You should call repo.getAllSiswa() from newList(), every time it's called, to get the latest, up-to-date, list of students.

5
  • Yes this is what came to my mind for a second.., but let me first try it out. Dec 28, 2015 at 9:57
  • I'm sorry if it's funny question, but the Student is initialized in the RepoImplement constructor then @Autowired public Repo repo; there, won't be null. Or I'm missing something.. Dec 28, 2015 at 10:06
  • You seem to be missing all my answer. Read it again. What is null is the repo field in the service. It has nothing to do with students.
    – JB Nizet
    Dec 28, 2015 at 10:07
  • 1
    @AngryDumbassSlapper - See, when the construction of ServImpl is happening, repo is not initialized and hence will be null. You are calling a method on it, so you get NPE. Dec 28, 2015 at 10:10
  • 1
    Yes it's working now, I put @Autowired above the constructor.and initialized the List inside it .. thank you so much...guys (and @VinodMadyalkar thanks bro..) Dec 28, 2015 at 10:17
1

Because Autowired on field happens right after construction, you need to change your code in order to get it to work.

This should works:

@Service
public class ServeImplement implements Serve {

    public Repo repo;
    List<Student> s;

    @Autowired
    public ServeImplement(Repo repo) {
        this.repo = repo;
        s = repo.getAllSiswa();
    }

    @Override
    public void changeName(String namaBaru) {
        s.get(0).setNama(namaBaru); // get first Student, then update its name.
    }

    @Override
    public List<Student> newList() {
        return this.s;
    }

}

Moreover, using Autorwired annotation on constructor allow you to mark your field as final if the instance shouldn't have to change.

1
  • Thank you @Fabian.., yes solution works (it's the concrete implemention of JBNized answer).. Dec 28, 2015 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.