Answers for "NullpointerException while autowire"

0

NullpointerException while autowire

@Controller
public class Controller {

  @Autowired
  MyService service;

  @GetMapping("/example")
  public String example() {
    service.doStuff();
  }
}

@Service
public class MyService() {

  @Autowired
  MyRepository repo;

  public void doStuff() {
    repo.findByName( "steve" );
  }
}

@Repository
public interface MyRepository extends CrudRepository<My, Long> {

  List<My> findByName( String name );
}
Posted by: Guest on May-07-2021
0

NullpointerException while autowire

@Controller
public class Controller {

  @GetMapping("/example")
  public String example() {
    MyService my = new MyService();
    my.doStuff();
  }
}

@Service
public class MyService() {

  @Autowired
  MyRepository repo;

  public void doStuff() {
    repo.findByName( "steve" );
  }
}

 

@Repository
public interface MyRepository extends CrudRepository<My, Long> {

  List<My> findByName( String name );
}
Posted by: Guest on May-07-2021

Code answers related to "NullpointerException while autowire"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language