user2746466
user2746466

Reputation: 381

Serviceclass implementing the jparepository always returns null in spring boot app

I am new to java and spring boot. I have created a simple spring app which fetches student details from the database using the JPArepository. Following is the studentDetais entity:

package com.example.webcustomertracker.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "StudentDetails")
public class StudentDetails {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY) 
	private Integer StudentID;
	private String Name;
	private String Surname;
	private String City;
	
	public StudentDetails() {}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getSurname() {
		return Surname;
	}

	public void setSurname(String surname) {
		Surname = surname;
	}

	public String getCity() {
		return City;
	}

	public void setCity(String city) {
		City = city;
	}

	public StudentDetails(String name, String surname, String city) {
		Name = name;
		Surname = surname;
		City = city;
	}

	@Override
	public String toString() {
		return "StudentDetails [Name=" + Name + ", Surname=" + Surname + ", City=" + City + "]";
	}
	
	

}

Following is the JPARepo:

  package com.example.webcustomertracker.data; 
  
  import org.springframework.data.jpa.repository.JpaRepository;

  import com.example.webcustomertracker.entity.StudentDetails;
  
  public interface StudentDetailsRepository extends JpaRepository<StudentDetails, Integer> 
  { 
	   
  }
 

Following is the service class:

package com.example.webcustomertracker.data;

import java.util.Optional;

import com.example.webcustomertracker.entity.StudentDetails;

public interface StudentDetailsService {

	public abstract Optional<StudentDetails> getStudentDetails(int StudentId);
}

Following is the service class implementation

package com.example.webcustomertracker.data;

import java.util.Optional;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.example.webcustomertracker.entity.StudentDetails;

@Component
public class StudentDetailsDataAccess implements StudentDetailsService {

	private StudentDetailsRepository studentDetailsRepository;
	
	public StudentDetailsDataAccess(StudentDetailsRepository theStudentDetailsRepository) {
		this.studentDetailsRepository = theStudentDetailsRepository;				
	}

	@Transactional
	public Optional<StudentDetails> getStudentDetails(int StudentId) {
		// TODO Auto-generated method stub
		
		Optional<StudentDetails> objStud =  this.studentDetailsRepository.findById(StudentId);
		
		
		
		return objStud;
	}
	
	
	
}

Following is the main class which boots the spring framework. I am just trying to call one of the functions of service but the service instance is coming as null and it is failing to execute.

package com.example.webcustomertracker;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.example.webcustomertracker.data.StudentDetailsDataAccess;
import com.example.webcustomertracker.data.StudentDetailsService;
import com.example.webcustomertracker.entity.StudentDetails;

@SpringBootApplication
public class WebCustomerTrackerApplication {
    
    @Autowired
 	private StudentDetailsService studentDetailsService;

	public Optional<StudentDetails> getTheStudentDetails(int id) {
		return studentDetailsService.getStudentDetails(id);
	}

	public static void main(String[] args) {
		SpringApplication.run(WebCustomerTrackerApplication.class, args); 
		
		Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);

	}

	
}

Following is the error I am getting after running the code:

Exception in thread "main" java.lang.NullPointerException
	at com.example.webcustomertracker.WebCustomerTrackerApplication.getTheStudentDetails(WebCustomerTrackerApplication.java:20)
	at com.example.webcustomertracker.WebCustomerTrackerApplication.main(WebCustomerTrackerApplication.java:26)

Upvotes: 0

Views: 135

Answers (2)

ArunKumar M N
ArunKumar M N

Reputation: 1266

Another solution would be,

    @SpringBootApplication
public class WebCustomerTrackerApplication {

    @Autowired
    private StudentDetailsService studentDetailsService;

    public Optional<StudentDetails> getTheStudentDetails(int id) {
        return studentDetailsService.getStudentDetails(id);
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(WebCustomerTrackerApplication.class, args); 
        Thread.sleep(1500);
        Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);

    }

}

here it is waiting until the application is started(all components are loaded). by this time you won't get null pointer exception since service object created.

Upvotes: 0

ArunKumar M N
ArunKumar M N

Reputation: 1266

Autowire in Controller layer.

Say you have any controller called IndexController auto wire there.

for example

    StudentDetailsService studentService;
    @Autowired
    public IndexController(StudentDetailsService studentService){
Optional<StudentDetails> objStudent = new studentService.getTheStudentDetails(11);
}

Upvotes: 1

Related Questions