0

I try to use @Autowired to use my service in @ClientEndpoint

but there is a java.lang.NullPointerException

here's my code

P2PService.java

public interface P2PService {

    void send(Session session, String msg);
}

P2PServiceImpl.java

@Service
public class P2PServiceImpl implements P2PService {
    @Override
    public void send(Session session, String msg) {
        session.getAsyncRemote().sendText(msg);
    }
}

Firstly I use this code

@Component
@ClientEndpoint
public class P2pClient {
    private Session session;
    public void connectServer(String url) {
            try {
                WebSocketContainer container = ContainerProvider.getWebSocketContainer();
                URI uri = URI.create(url);

                this.session = container.connectToServer(P2pClient.class, uri);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    @Autowired
    private P2PService p2PService;


    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        System.out.println(this);
        System.out.println(P2pClient.p2PService);
    }

and after find reference I use this

    private static P2PService p2PService;
    @Autowired
    public static void setP2PService(P2PService p2PService) {
        P2pClient.p2PService = p2PService;
    }

and console output is

com.p2ptest.P2pClient@71e7a66b
null

I just use this code to start the project

P2pClient p2pClient = new P2pClient();
p2pClient.connectServer(url);

Exception

java.lang.NullPointerException
    at com.p2ptest.P2pClient.onOpen(P2pClient.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:65)
    at org.apache.tomcat.websocket.pojo.PojoEndpointClient.onOpen(PojoEndpointClient.java:45)
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:509)
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:197)
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:154)
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:171)
    at com.p2ptest.P2pClient.connectServer(P2pClient.java:38)
    at com.Hello.main(Hello.java:22)

I don't know what I can do

  • 1
    The autowiring cannot work when the class containing the `@Autowired` annotation is instantiated using `new`, i.e. `P2pClient p2pClient = new P2pClient();` is where the problem sits. It is neccessary to get the `p2pClient` from application context. – Michal Feb 28 '20 at 10:14
  • This question already has an answer: https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null – Michal Feb 28 '20 at 10:16

0 Answers0