我正在使用带有SPring数据的spring-boot,并且只是试图通过命令行创建一个简单的用户名创建方法,并使用哈希密码将密码发送给db。
我收到此错误,并说我的服务为空?当我查看如何传递我的价值观时,无论如何它们似乎都传递了正确的价值观。我只是不知道在何处以及为什么它会显示空指针异常,而我的JPA回购保存功能一直都在接收适当的属性。
我的实体是..
@Entity
@EntityScan
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String username;
@Column
private String password;
@Override
public String toString() {
return new StringJoiner(", ", Users.class.getSimpleName() + "[", "]")
.add("id=" + id)
.add("username='" + username + "'")
.add("password='" + password + "'")
.toString();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我的主要课程是
@SpringBootApplication
public class HashingexerApplication {
@Autowired
UserService userService;
public static void main(String[] args) {
SpringApplication.run(HashingexerApplication.class, args);
// Scanner input=new Scanner(System.in);
String username, password;
System.out.println("Enter a username: ");
// username = input.nextLine();
username = "josh";
System.out.println("Enter a password: ");
// password = input.nextLine();
password = "pass";
String salt = "_Hello_World";
System.out.println("Password before encryption: " + password);
System.out.println("Salt to be added is: " + salt);
String saltedPass = password + salt;
System.out.println("Unencrypted Salted Password is: " + password + salt);
String passwordEncrypt = BCrypt.hashpw(password, BCrypt.gensalt(12));
System.out.println("Encrypted Salted Password is: " + passwordEncrypt);
Users users = new Users();
users.setId(1);
users.setUsername(username);
users.setPassword(passwordEncrypt);
System.out.println("Object:: " + users);
HashingexerApplication d = new HashingexerApplication();
d.save(users);
}
public String save(Users users) {
System.out.println("method: " + users);
userService.save(users);
return "Done";
}
}
错误详细信息:
线程“ restartedMain”中的异常 java.lang.reflect.InvocationTargetException在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 由以下原因引起:java.lang.NullPointerException hashingexer.hashingexer.HashingexerApplication.save(HashingexerApplication.java:61) 在 hashingexer.hashingexer.HashingexerApplication.main(HashingexerApplication.java:51) ...还有5个
答案 0 :(得分:0)
这将为您提供帮助
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>