I’m newbie Java-dev and I’m trying to make an JavaFX-application that uses Spring.
I have some problems with saving to PostgreSQL.
So, I have a registration form, I’m taking user data and trying to save it to PostgreSQL with Spring Data JPA.
I have hierarchy: com.raddan.Chat > Authorize > Controller(AuthController, RegController), Model(User), Repository(UserRepository), Service(UserDetailsImpl)
So what exactly is here: I press on Registration button and I have an error: userRepository is null.
I’ll be thankful if smone can help me with that.
import com.raddan.Chat.Authorize.Model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
@Component
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findById(Long id);
Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email);
}
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "users")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String email;
}
import com.raddan.Chat.Authorize.Model.User;
import com.raddan.Chat.Authorize.Repository.UserRepository;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
@Controller
@SpringBootApplication
public class RegController {
@FXML
private Button backToAuthButton;
@FXML
private TextField emailField;
@FXML
private AnchorPane emailPane;
@FXML
private Button nextButton1;
@FXML
private Button nextButton2;
@FXML
private PasswordField passwordField;
@FXML
private AnchorPane passwordPane;
@FXML
private Button regButton;
@FXML
private TextField usernameField;
@FXML
private AnchorPane usernamePane;
@Autowired
private UserRepository userRepository;
private String email;
private String username;
private String password;
@FXML
private void handleNextButton1() {
email = emailField.getText();
emailPane.setVisible(false);
usernamePane.setVisible(true);
}
@FXML
private void handleNextButton2() {
username = usernameField.getText();
usernamePane.setVisible(false);
passwordPane.setVisible(true);
}
@FXML
private void handleRegButton() {
password = passwordField.getText();
User user = new User();
user.setEmail(email);
user.setUsername(username);
user.setPassword(password);
userRepository.save(user);
emailField.clear();
usernameField.clear();
passwordField.clear();
showConfirmationDialog();
}
private void showConfirmationDialog() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Success!");
alert.setHeaderText(null);
alert.setContentText("You successfully registered!");
alert.showAndWait();
}
}```
raddan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1