I'm using spring-boot with SPring data.And just trying to create a simple create of username with hashed password to db via command line.
I'm getting this error and says that my service is null?. As I was looking of how my values are being passed, they seem to be all passing the correct values anyway. I just don't get where and why it says null pointer exception, all the while I my JPA repo save function is receiving the proper attributes.
My Entity is..
@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;
}
}
And my main class is.
@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";
}
}
Error Details:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException 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.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: java.lang.NullPointerException at hashingexer.hashingexer.HashingexerApplication.save(HashingexerApplication.java:61) at hashingexer.hashingexer.HashingexerApplication.main(HashingexerApplication.java:51) ... 5 more