4

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

칼인오
  • 143
  • 1
  • 2
  • 11
  • The `userService` variable is `null` because you haven't initialized it. Therefore `userService.save(...)` throws an NPE. – Stephen C Apr 16 '19 at 08:02
  • what do you mean by initialized?, isn't it supposed to be initialized by spring since its autowired?. Also my method for save already has the proper declaration to it though. I mean, its basically a photocopy of my previous sample project with the same code layout for saving. I don't get it of why this service has to be initialized, and if ever really have to initialized it, how to do though?. Im just used to autowiring the services and repos i have – 칼인오 Apr 16 '19 at 08:07
  • Well, apparently, your autowiring is not working! Because if you get an NPE in `save` as indicated by the stacktrace there is only one possible place it can occur, and only one possible cause. (Unless you are not showing us the real code.) – Stephen C Apr 16 '19 at 11:52
  • nah, its definitely working. I just had this converted to a RestController with inputs coming from a postman json input and the save function definitely works. Values are being passed to the jpa save method properly. This is through terminal input or basically just through predefined values doesnt just work at all. Any ideas how to fix this through terminal?. This is my first time seeing this error, and could not get around this thing although the error is a bit odd, cuz I know for sure that my values are being passed properly before and even know. :/ – 칼인오 Apr 23 '19 at 19:55
  • I don't understand your explanation your explanation of why you think it is working. The evidence in the stacktrace is unambiguous. If the `save` method is throwing an NPE, there is only one possible place it could be occurring. But if you don't believe me, add some code to your app to *test* the value of `userService` immediately before you call `userService.save()`. – Stephen C Apr 24 '19 at 00:07

1 Answers1

1

This will help you

<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>