在使用springboot连接mysql时报错java.lang.NullPointerException: null
@lombok.Data
public class Data {
private String SvrId;
private int LocalId;
private String PrefixId;
private String createtime;
private int msgtype;
private int subtype;
private int isSender;
private String content;
private String from_wxid;
private String from_name;
private String final_from_wxid;
private String final_from_name;
private String to_wxid;
private String to_name;
private String filepath;
}
------------------------------------------------------------------------------------------------------------------------------------
import java.util.List;
public interface DataOperation {
// 添加一条数据
public void addData(Data data);
// 删除一条数据
public void deleteData(Data data);
// 修改一条数据
public void updateData(Data data);
// 查询所有数据
public List<Data> selectAllData();
// 查询单个数据
public Data selectLocalId(int LocalId);
}
------------------------------------------------------------------------------------------------------------------------------------
package com.example.springtest0609.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class DataOperationImp implements DataOperation{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addData(Data data) {
}
@Override
public void deleteData(Data data) {
}
@Override
public void updateData(Data data) {
}
@Override
public List<Data> selectAllData() {
return null;
}
@Override
public Data selectLocalId(int LocalId) {
System.out.println(jdbcTemplate.queryForMap("select * from Data where LocalId=70992"));
Data data=new Data();
data.setLocalId(12312);
return data;
}
}
在Test里运行是正常的
import com.example.springtest0609.dao.Data;
import com.example.springtest0609.dao.DataOperation;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springtest0609ApplicationTests {
@Autowired
private DataOperation dataOperation;
@Test
void contextLoads() {
// 查询1001学生的信息
Data student_test=dataOperation.selectLocalId(123);
System.out.println(student_test);
}
}
但是在方法里使用会出现以下报错,无法直接调用
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taskadapter.redmineapi.RedmineException;
import dao.Data;
import dao.DataOperation;
import lombok.extern.slf4j.Slf4j;
import org.hyperic.sigar.SigarException;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import service.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;
import static service.XiaomiSport.XiaomiSport;
@Slf4j
@Controller
public class MyWebSocketClient extends WebSocketClient {
public long startTime = System.currentTimeMillis(); // 获取开始时间
@Autowired
private DataOperation dataOperation;
public MyWebSocketClient() throws URISyntaxException {
super(new URI("ws://127.0.0.1:2023"));
}
@Override
public void onOpen(ServerHandshake serverHandshake) {
log.info("打开websocket连接");
}
@Override
public void onMessage(String s) {
try {
Data student_test=dataOperation.selectLocalId(123);
System.out.println(student_test);
} catch (SigarException | IOException | RedmineException e) {
e.printStackTrace();
}
}
@Override
public void onClose(int i, String s, boolean b) {
log.error("连接关闭{}", s);
}
@Override
public void onError(Exception e) {
log.error("连接错误{}", e);
}
//发送消息
public void sendMessage(String message) {
this.send(message);
log.info("on reply for:{}", message);
}
java.lang.NullPointerException: null
at controller.MyWebSocketClient.ReceiveMessage(MyWebSocketClient.java:71)
at controller.MyWebSocketClient.onMessage(MyWebSocketClient.java:43)
at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:462)
at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:696)
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:367)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:212)
at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:389)
at java.lang.Thread.run(Thread.java:748)
我想要直接在方法里调用,自动查询数据/插入数据,麻烦帮忙看看