Hi all, I’m having a problem with a one-to-one mapping between a Contract class and a Vehicle, where trying to access the Vehicle associated with the Contract gives a null pointer exception.
When I try to access vehicleSold, it says:
Cannot invoke "com.example.vehicle_workshop.models.Vehicle.getPrice()" because the return value of "com.example.vehicle_workshop.models.SalesContract.getVehicleSold()" is null
Here are the following classes:
@Data
@MappedSuperclass
public abstract class Contract {
private String date;
private String customerName;
private String customerEmail;
@OneToOne
@PrimaryKeyJoinColumn(name = "vin")
private Vehicle vehicleSold;
}
Here’s an implementation:
@EqualsAndHashCode(callSuper = false)
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class SalesContract extends Contract {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long salesContractId;
private boolean financed;
// irrelevant methods not shown
}
Here’s the Vehicle:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Vehicle {
@Id
private String vin;
private int year;
private String make;
private String model;
private String vehicleType;
private String color;
private int odometer;
private double price;
}
I’ve checked that there’s valid entries to use, and that there’s a VIN in the SalesContract table that matches up to the Vehicle table