コンポーネント内でAutowiredされているサービスを使用しようとすると、私のSpring Web Appはjava.lang.NullPointerException
を返します。@Component内の@Autowiredサービスがnullポインタ例外を返します。
私は既に@component
を使用しているので、hereのソリューションを試しましたが、それは私のためには機能しませんでした。ここで
は私の完全な例外です:
java.lang.NullPointerException
at ir.yasharne.rpi.websocket.MyStompSessionHandler.handleFrame(MyStompSessionHandler.java:81)
at org.springframework.messaging.simp.stomp.DefaultStompSession.invokeHandler(DefaultStompSession.java:448)
at org.springframework.messaging.simp.stomp.DefaultStompSession.handleMessage(DefaultStompSession.java:399)
at org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.handleMessage(WebSocketStompClient.java:342)
at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleMessageFrame(AbstractClientSockJsSession.java:271)
at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.handleFrame(AbstractClientSockJsSession.java:213)
at org.springframework.web.socket.sockjs.client.WebSocketTransport$ClientSockJsWebSocketHandler.handleTextMessage(WebSocketTransport.java:162)
at org.springframework.web.socket.handler.AbstractWebSocketHandler.handleMessage(AbstractWebSocketHandler.java:43)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.handleTextMessage(StandardWebSocketHandlerAdapter.java:110)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.access$000(StandardWebSocketHandlerAdapter.java:42)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:81)
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter$3.onMessage(StandardWebSocketHandlerAdapter.java:78)
at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:395)
at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:495)
at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:294)
at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133)
at org.apache.tomcat.websocket.WsFrameClient.processSocketRead(WsFrameClient.java:95)
at org.apache.tomcat.websocket.WsFrameClient.resumeProcessing(WsFrameClient.java:209)
at org.apache.tomcat.websocket.WsFrameClient.access$300(WsFrameClient.java:31)
at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.doResumeProcessing(WsFrameClient.java:186)
at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:163)
at org.apache.tomcat.websocket.WsFrameClient$WsFrameClientCompletionHandler.completed(WsFrameClient.java:148)
at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
at sun.nio.ch.Invoker$2.run(Invoker.java:218)
at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
ライン81はMyStompSessionHandler.java
でmoduleService.saveModule(module);
です:
@Component
public class MyStompSessionHandler extends StompSessionHandlerAdapter {
@Autowired
private ModuleService moduleService;
@Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
session.subscribe("/topic/messages", this);
}
@Override
public Type getPayloadType(StompHeaders headers) {
return ServerMessage.class;
}
@Override
public void handleFrame(StompHeaders headers, Object payload) {
ServerMessage message = (ServerMessage) payload;
if (message.getTopic().equals("newModule")){
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Object> readValue = mapper.readValue(message.getMessage(), Map.class);
int id = (int) readValue.get("id");
boolean programmable = (boolean) readValue.get("programmable");
float value = Float.parseFloat(readValue.get("value").toString());
Module module = new Module();
module.setId(id);
module.setProgrammable(programmable);
module.setValue(value);
moduleService.saveModule(module);
} catch (IOException e) {
System.out.println("HandlerFrame: ");
e.printStackTrace();
}
}
}
@Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
exception.printStackTrace();
}
}
と私のWebAppConfiguration.java
:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "ir.yasharne.rpi")
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registery){
registery.addResourceHandler("/static/**").addResourceLocations("/static/");
}
}
ModuleServiceImpl.java
:
@Service("moduleService")
@Transactional
public class ModuleServiceImpl implements ModuleService {
@Autowired
private ModuleDao moduleDao;
@Override
public void saveModule(Module module) {
moduleDao.saveModule(module);
}
}
どうすれば修正できますか?
ところで、私はAutowireとコントローラの内部でmoduleService
を使用しても問題ありません
'ModuleServiceImpl'のパッケージは何ですか? – Berger
@Berger 'package ir.yasharne.rpi.service.impl; ' – Yashar
私は同じ問題を抱えていました。コンストラクタにパラメータを渡してそれらを解決しました。 –