Here is my servlet code

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Configuration
public class DoGet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Autowired
    private ProductRepository productRepository;


    public void init(ServletConfig config) throws ServletException {

super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
TODO Auto-generated method stub


String search = "%"+request.getParameter("search")+"%";
List<Product> findByProductNameLike = productRepository.findByProductNameLike(search);

PrintWriter writer = response.getWriter();
writer.write("<h1>Hello World How are You<h1>");

for(Product i: findByProductNameLike)
{
    writer.write(i.getProductId());
    writer.write(i.getProductName());
    writer.write(i.getImgPath());
}

    writer.write("<h1>Done<h1>");
 }

}

This is my productRepository

package com.servlet;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepository extends JpaRepository<Product, String>{
    
    public List<Product> findByProductNameLike(String produtName);

}

I have tried creating object of one other class since I thought ProductRepository is an interface so I might be getting an error.

I was expecting a proper output on the window, instead I am getting error message that productRepository is null on line

List<Product> findByProductNameLike = productRepository.findByProductNameLike(search);

Here is the proper error message:

Type Exception Report

Message Cannot invoke "com.servlet.ProductRepository.findByProductNameLike(String)" because "this.productRepository" is null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NullPointerException: Cannot invoke "com.servlet.ProductRepository.findByProductNameLike(String)" because "this.productRepository" is null
    com.servlet.DoGet.doGet(DoGet.java:40)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683)
    jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

I am using Apache Tomcat v10.0. I have looked at many tutorials and solutions to other questions, but still wasn't able to run my code in the desired way.

I wasn't able to post applicationContext.xml and web.xml due to spam issues.

1

There are 1 best solutions below

7
Loading On

I think the problem is that the DoGet is not a bean/component. So add @Component or @Configuration (maybe this was a typo here?) to it and then everything should be autowired correctly.