- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Spring Boot 技术的新手。我正在将 JPA 存储库与 MySQL DB 结合使用。我收到 NullPointer 异常错误。
@Repository
@Transactional
public interface UserRepository extends JpaRepository<User, Integer> {
@Query(value="SELECT * FROM User u WHERE u.isUserLoggedIn = 1", nativeQuery = true)
List<User> findUserLastLoggedIn();
}
我的 table
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT,
`_id` varchar(30) DEFAULT NULL,
`email` varchar(40) NOT NULL,
`password` varchar(300) NOT NULL,
`salt` varchar(100) DEFAULT NULL,
`status` int(11) NOT NULL DEFAULT (1),
`insert_date` datetime DEFAULT (now()),
`last_login_time` datetime DEFAULT (now()),
`name` varchar(100) DEFAULT NULL,
`surname` varchar(100) DEFAULT NULL,
`alias_name` varchar(100) DEFAULT NULL,
`access_token` varchar(100) DEFAULT NULL,
`refresh_token` varchar(100) DEFAULT NULL,
`is_user_logged_in` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci;
用户实体如下:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String _id;
private String email;
private String password;
private String salt;
private int status;
private Date insertDate;
private Date lastLoginTime;
private String name;
private String surname;
private String aliasName;
private String accessToken;
private String refreshToken;
private int isUserLoggedIn;
//getter and setter ...
}
我的 StackTrace:
java.lang.NullPointerException: null
at me.sikke.UserController.findUserLastLoggedIn(UserController.java:111) ~[classes/:na]
at me.sikke.WalletController.createWallet(WalletController.java:25) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_191]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
my repository is here我无法理解这个问题。您可以在此处查看我的界面
我的 UserController 类
@RestController
public class UserController {
@Autowired
UserRepository sikkeRespository;
@RequestMapping(value = "/getAccessToken", method = RequestMethod.POST)
public ResponseEntity<String> getAccessToken(@RequestBody LoginUser loginUser) {
List<User> userList = sikkeRespository.findUserLastLoggedIn();
if (userList != null && userList.size() > 0) {
return new ResponseEntity<String>("There are currently active users.
Please exit and try again.", HttpStatus.OK);
}
if (SikkeHelper.checkLoginUser(loginUser)) {
String username = loginUser.getEmail();
String password = loginUser.getPassword();
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("grant_type", "password"));
urlParameters.add(new BasicNameValuePair("username", username));
urlParameters.add(new BasicNameValuePair("password", password));
try {
String jsonString = ConnectionInfo.call(SikkeConstant.accessTokenUrl, urlParameters);
User user = new User();
if (jsonString != null) {
JSONObject jsonObj = new JSONObject(jsonString);
String status = jsonObj.getString("status");
if (SikkeConstant.STATUS_SUCCESS.equals(status)) {
System.out.println("Login Succeed : " +
jsonObj.getString("email"));
user.setPassword(password);
SikkeHelper.fillUserFromJson(jsonObj, user);
User userExisting =
sikkeRespository.findByEmail(username);
userExisting.setIsUserLoggedIn(1);
if (userExisting != null) {
userExisting.setAccessToken(user.getAccessToken());
userExisting.setRefreshToken(user.getRefreshToken());
sikkeRespository.save(userExisting);
} else {
sikkeRespository.save(user);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
throw new SikkeException(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
throw new SikkeException(e.getMessage());
}
}
return new ResponseEntity<String>("Session successfully opened.",
HttpStatus.OK);
}
@RequestMapping(value = "/registerUser", method = RequestMethod.POST)
public ResponseEntity<String> registerUser(@RequestBody LoginUser loginUser)
{
if (SikkeHelper.checkLoginUser(loginUser)) {
String username = loginUser.getEmail();
String password = loginUser.getPassword();
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("email", username));
urlParameters.add(new BasicNameValuePair("password", password));
urlParameters.add(new BasicNameValuePair("password_confirm",
password));
try {
String jsonString =
ConnectionInfo.call(SikkeConstant.registerUserUrl, urlParameters);
User user = new User();
if (jsonString != null) {
JSONObject jsonObj;
jsonObj = new JSONObject(jsonString);
String status = jsonObj.getString("status");
if (SikkeConstant.STATUS_SUCCESS.equals(status)) {
JSONObject userJson = jsonObj.getJSONObject("user");
user.setPassword(password);
SikkeHelper.fillUserFromJson(userJson, user);
sikkeRespository.save(user);
} else if (SikkeConstant.STATUS_ERROR.equals(status)) {
System.out.println(jsonObj.getString("message"));
}
}
} catch (JSONException e) {
System.out.println(e.getMessage());
throw new SikkeException(e.getMessage());
} catch (IOException e) {
throw new SikkeException(e.getMessage());
}
}
return new ResponseEntity<String>("User created.", HttpStatus.OK);
}
@RequestMapping(value = "/logOut", method = RequestMethod.GET)
public ResponseEntity<String> logOut() {
int effectedRow = sikkeRespository.logoutAllUser();
return new ResponseEntity<String>("Logout done successfully.",
HttpStatus.OK);
}
public List<User> findUserLastLoggedIn() {
List<User> userList = sikkeRespository.findUserLastLoggedIn();
return userList;
}
}
List<User> userList = userController.findUserLastLoggedIn();
钱包 Controller 中的以下行调用 UserController 中的 findUserLastLoggedIn 方法
我的钱包 Controller
@RestController
public class WalletController {
@Autowired
WalletRepository walletRepository;
@Autowired
UserController userController;
@RequestMapping(value = "/createWallet")
public ResponseEntity<String> createWallet() {
Wallet wallet = new Wallet();
userController = new UserController();
List<User> userList = userController.findUserLastLoggedIn();
if (userList == null) {
return new ResponseEntity<>("Active user not found. You must login
to the system.", HttpStatus.OK);
}
if (userList.size() == 1) {
User user = null;//userList.get(0);
wallet.setwOwnerId(Integer.parseInt(user.get_id()));
WalletKey walletKey = WalletKey.getWalletKeys();
wallet.setwPubKey(walletKey.getPublicKey());
wallet.setwZeugma(walletKey.getPrivateKey());
wallet.setwNumber(walletKey.getWalletNumber());
walletRepository.save(wallet);
return new ResponseEntity<>("Wallet created.", HttpStatus.OK);
}
return new ResponseEntity<String>("Wallet creation failed",
HttpStatus.OK);
}
}
@斯帕拉
删除 userController = new UserController 行后更新了 WalletController
我的代码哪里有问题,你能帮我吗?
最佳答案
当您使用nativeQuery = true
时,它将在 native 运行您的查询,因此您应该以这种方式编写查询:
@Query(value="SELECT * FROM User u WHERE u.is_user_logged_in = 1", nativeQuery = true)
List<User> findUserLastLoggedIn();
另一种方法是不使用nativeQuery
:
@Query(value="SELECT u FROM User u WHERE u.isUserLoggedIn = 1")
List<User> findUserLastLoggedIn();
请删除您的 WalletController 中的这一行:
userController = new UserController();
这会使您在 @Autowired
字段上出现 NullPointerException
了解更多信息请访问here
@RestController
public class WalletController {
@Autowired
WalletRepository walletRepository;
@Autowired
UserRepository userRepository;
@RequestMapping(value = "/createWallet")
public ResponseEntity<String> createWallet() {
Wallet wallet = new Wallet();
List<User> userList = userRepository.findUserLastLoggedIn();
if (userList == null) {
return new ResponseEntity<>("Active user not found. You must login
to the system.", HttpStatus.OK);
}
if (userList.size() == 1) {
User user = null;//userList.get(0);
wallet.setwOwnerId(Integer.parseInt(user.get_id()));
WalletKey walletKey = WalletKey.getWalletKeys();
wallet.setwPubKey(walletKey.getPublicKey());
wallet.setwZeugma(walletKey.getPrivateKey());
wallet.setwNumber(walletKey.getWalletNumber());
walletRepository.save(wallet);
return new ResponseEntity<>("Wallet created.", HttpStatus.OK);
}
return new ResponseEntity<String>("Wallet creation failed",
HttpStatus.OK);
}
}
关于java - Spring Boot JPA 按条件选择获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53632998/
是否有某种方法可以使用 JPA 或 Hibernate Crtiteria API 来表示这种 SQL?或者我应该将其作为 native 执行吗? SELECT A.X FROM (SELECT X,
在查询中, select id,name,feature,marks from (....) 我想删除其 id 在另一个 select 语句中存在的那些。 从 (...) 中选择 id 我是 sql
我想响应用户在 select 元素中选择一个项目。然而这个 jQuery: $('#platypusDropDown').select(function () { alert('You sel
这个问题在这里已经有了答案: SQL select only rows with max value on a column [duplicate] (27 个回答) 关闭8年前。 我正在学习 SQL
This question already has answers here: “Notice: Undefined variable”, “Notice: Undefined index”, and
我在 php 脚本中调用 SQL。有时“DE”中没有值,如果是这种情况我想从“EN”中获取值 应该是这样的,但不是这样的 IF (EXISTS (SELECT epf_application_deta
这可能是一个奇怪的问题,但不知道如何研究它。执行以下查询时: SELECT Foo.col1, Foo.col2, Foo.col3 FROM Foo INNER JOIN Bar ON
如何在使用 Camera.DestinationType.FILE_URI. 时在 phonegap camera API 中同时选择或拾取多个图像我能够一次只选择一张图像。我可以使用 this 在
这是一个纯粹的学术问题。这两个陈述实际上是否相同? IF EXISTS (SELECT TOP 1 1 FROM Table1) SELECT 1 ELSE SELECT 0 相对 IF EXIS
我使用 JSoup 来解析 HTML 响应。我有多个 Div 标签。我必须根据 ID 选择 Div 标签。 我的伪代码是这样的 Document divTag = Jsoup.connect(link
我正在处理一个具有多个选择框的表单。当用户从 selectbox1 中选择一个选项时,我需要 selectbox2 active 的另一个值。同样,当他选择 selectbox2 的另一个值时,我需要
Acme Inc. Christa Woods Charlotte Freeman Jeffrey Walton Ella Hubbard Se
我有一个login.html其中form定义如下: First Initial Plus Last Name : 我的do_authorize如下: "; pri
$.get( 'http://www.ufilme.ro/api/load/maron_online/470', function(data
我有一个下拉列表“磅”、“克”、“千克”和“盎司”。我想要这样一种情况,当我选择 gram 来执行一个函数时,当我在输入字段中输入一个值时,当我选择 pounds 时,我想要另一个函数来执行时我在输入
我有一个 GLSL 着色器,它从输入纹理的 channel 之一(例如 R)读取,然后写入输出纹理中的同一 channel 。该 channel 必须由用户选择。 我现在能想到的就是使用一个 int
我想根据下拉列表中的选定值生成输入文本框。 Options 2 3 4 5 就在这个选择框之后,一些输入字段应该按照选定的数字出现。 最佳答案 我建议您使用响应式(Reac
我是 SQL 新手,我想问一下如何根据首选项和分组选择条目。 +----------+----------+------+ | ENTRY_ID | ROUTE_ID | TYPE | +------
我有以下表结构: CREATE TABLE [dbo].[UTS_USERCLIENT_MAPPING_USER_LIST] ( [MAPPING_ID] [int] IDENTITY(1,1
我在移除不必要的床单时遇到了问题。我查看了不同的论坛并将不同的解决方案混合在一起。 此宏删除工作表(第一张工作表除外)。 Sub wrong() Dim sht As Object Applicati
我是一名优秀的程序员,十分优秀!