1

1 つのセッターから常に NullPointerException が返されますが、その理由はわかりません。これは図です:

ここに画像の説明を入力

これはメインクラスです:

@Entity
@Access(AccessType.PROPERTY)
public class Registration extends DomainEntity {

    // Constructors -----------------------------------------------------------

    public Registration() {
        super();
    }

    // Attributes -------------------------------------------------------------

    private Date moment;
    private String announcement;

    @NotNull    
    @Temporal(TemporalType.TIMESTAMP)
    @Past
    @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
    public Date getMoment() {
        return moment;
    }

    public void setMoment(Date moment) {
        this.moment = moment;
    }

    @NotBlank
    public String getAnnouncement() {
        return announcement;
    }

    public void setAnnouncement(String announcement) {
        this.announcement = announcement;
    }

    // Relationships ----------------------------------------------------------

    private Payment payment;
    private Customer owner;

    //@Valid
    @OneToOne(mappedBy="registration", optional=true, cascade = CascadeType.ALL)
    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
        payment.setRegistration(this);
    }

    @NotNull
    @Valid
    @ManyToOne(optional = false)
    public Customer getOwner() {
        return owner;
    }

    public void setOwner(Customer owner) {
        this.owner = owner;
    }
}

これは依存クラスです:

@Entity
@Access(AccessType.PROPERTY)
public class Payment extends DomainEntity {

    // Constructors -----------------------------------------------------------

    public Payment() {
        super();
    }

    // Attributes -------------------------------------------------------------

    private Date moment;
    private Double fee;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm")
    public Date getMoment() {
        return moment;
    }

    public void setMoment(Date moment) {
        this.moment = moment;
    }

    @Digits(fraction=2, integer=2)
    @Min(0)
    public Double getFee() {
        return fee;
    }

    public void setFee(Double fee) {
        this.fee = fee;
    }

    // Relationships ----------------------------------------------------------

    private Registration registration;

    @OneToOne(optional = false, cascade=CascadeType.ALL)
    public Registration getRegistration() {
        return registration;
    }

    public void setRegistration(Registration registration) {
        this.registration = registration;
    }

}

これらのクラスを埋めるための情報は、form:formタグから取得されます。optional = false両方のクラスの OneToOne アノテーションに( ) を入れると、すべて正常に機能します。しかし、Registration クラスに入れるoptional=trueと、Registration クラスのセッター " payment.setRegistration(this)" が私に をくれますNullPointerExceptionが、登録を保存する前に画面に出力しているので、支払いが null ではないことは完全に確信しています。じゃあ何が問題なの?

編集:

リポジトリとサービス層のコードを追加:

リポジトリ:

@Repository
public interface RegistrationRepository extends JpaRepository<Registration, Integer> {

}

サービス:

@Service
@Transactional
public class RegistrationService {

    // Managed repository -----------------------------------------------------

    @Autowired
    private RegistrationRepository registrationRepository;

    // Supporting services ----------------------------------------------------

    @Autowired
    private CustomerService customerService;

    // Constructors -----------------------------------------------------------

    public RegistrationService() {
        super();
    }

    // Simple CRUD methods ----------------------------------------------------

    public Registration createRegistration() {
        Registration result;
        Date moment;
        Customer owner;

        moment = new Date(System.currentTimeMillis() - 1);
        owner = customerService.findByPrincipal();

        result = new Registration();    
        result.setMoment(moment);
        result.setOwner(owner);

        owner.addRegistration(result);

        return result;
    }

    public void save(Registration registration) {
        Assert.notNull(registration);

        registrationRepository.save(registration);
    }

    public void delete(Registration registration) {
        Assert.notNull(registration);
        Assert.isTrue(registration.getId() != 0);

        registrationRepository.delete(registration);        
    }

    // Business methods -------------------------------------------------------

    public Collection<Registration> findByPrincipal() {
        Collection<Registration> result;
        Customer customer;

        customer = customerService.findByPrincipal();
        result = customer.getRegistrations();

        return result;
    }

    public void registerPrincipal(Registration registration) {
        Assert.notNull(registration);

        Customer owner;
        Date paymentMoment;
        Date registrationMoment;

        owner = customerService.findByPrincipal();

        registration.setOwner(owner);

        save(registration);
    }

}
4

0 に答える 0