Spring Boot中的java.lang.NullPointerException

时间:2017-07-02 19:35:25

标签: spring spring-boot

我在第一个Spring Boot应用程序中尝试使用Spring Data。

Person.java:

public interface PersonRepository extends CrudRepository<Person, Long> {
}

PersonRepository.java:

public interface PersonService {
    void add(String name);
}

PersonService.java:

@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonRepository personRepository;

    @Transactional
    public void add(String name) {
        Person person = new Person();
        person.setName(name);
        personRepository.save(person);
    }
}

PersonServiceImpl:

@SpringBootApplication
public class PersonController {
    @Autowired
    PersonService personService;

    public static void main(String args[]) {
        new PersonController().testAdd();
    }

    void testAdd() {
        personService.add("Nick");
    }

}

PersonConroller.java:

Exception in thread "main" java.lang.NullPointerException
    at com.art.controller.PersonController.testAdd(PersonController.java:17)
    at com.art.controller.PersonController.main(PersonController.java:13)

错误:

{{1}}

为什么我在testAdd()中有NullPointerException?请解释

0 个答案:

没有答案