1

When I am trying to update a field of table using @Query(value = "Update ",nativeQuery=true) is throwing null pointer exception.

I have tried many approaches which I can find in stack overflow, but no one solves my problem.

AirCraftRoute.java

@Builder
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "aircraft_route")
public class AirCraftRoute implements Serializable
{
    @EmbeddedId
    private AirCraftRoutePK pk = new AirCraftRoutePK();

    @ManyToOne
    @MapsId("airCraftId")
    @JoinColumn(name = "aircraft_id")
    private AirCraft airCraft;

    @ManyToOne
    @MapsId("routeId")
    @JoinColumn(name = "route_id")
    private Route route;

    @Column(name = "journey_date")
    private Date journeyDate;

    @Column(name = "departure_time")
    private Time departureTime;

    @Column(name = "arrival_time")
    private Time arrivalTime;

    @Column(name = "fare")
    private float fare;

    @Column(name = "availability")
    private int availability;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AirCraftRoute)) return false;
        AirCraftRoute that = (AirCraftRoute) o;
        return Float.compare(that.getFare(), getFare()) == 0 &&
                Objects.equals(getAirCraft(), that.getAirCraft()) &&
                Objects.equals(getRoute(), that.getRoute()) &&
                Objects.equals(getJourneyDate(), that.getJourneyDate()) &&
                Objects.equals(getDepartureTime(), that.getDepartureTime()) &&
                Objects.equals(getArrivalTime(), that.getArrivalTime());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getAirCraft(), getRoute(), getJourneyDate(), getDepartureTime(), getArrivalTime(), getFare());
    }
}

BookingService.java

@Service
public class BookingService implements IBookingService {

    @Autowired
    private BookingRepository bookingRepository;

    private AirCraftRouteRepository airCraftRouteRepository;

    @Transactional
    @Override
    public Booking bookFlight(Booking booking){
//        Long airCraftId = booking.getAirCraftRoute().getId().getAirCraftId().getAircraftId();
//        Long routeId = booking.getAirCraftRoute().getId().getRouteId().getRouteId();
        booking.setBookingReferenceNumber("##YTRJTG");
        booking.setPnrNumber("PNRYTFRYHG");

        int result = airCraftRouteRepository.updateAvailability();
        System.out.println(result);
        //bookingRepository.save(booking);
        return booking;
    }
}

AirCraftRouteRepository.java

@Repository
public interface AirCraftRouteRepository extends JpaRepository<AirCraftRoute,Integer> {

    //@Lock(LockModeType.PESSIMISTIC_WRITE)
    @Modifying
    @Transactional
    @Query(value = "UPDATE AirCraftRoute ar SET ar.availability = 5 where ar.fare = 2000",nativeQuery = true)
    int updateAvailability();
}

Exception

@Query(value = "UPDATE AirCraftRoute ar SET ar.availability = 5 where ar.fare = 2000",nativeQuery = true)
int updateAvailability();

This is throwing null pointer exception. The error says:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.abc.gds.service.BookingService.bookFlight(BookingService.java:27) ~[main/:na]

Can someone please help?

1
  • Is not missing @Autowired annotation for airCraftRouteRepository? Commented Sep 12, 2019 at 21:43

1 Answer 1

1

A good guess would be BookingService.java:27 is this line

int result = airCraftRouteRepository.updateAvailability();

Your airCraftRouteRepository is null since you didnt Autowired it

@Autowired
private AirCraftRouteRepository airCraftRouteRepository;
Sign up to request clarification or add additional context in comments.

2 Comments

Ops my bad...It works...Very trivial mistake. I am getting other error now as:- "Illegal attempt to set lock mode on a native SQL query" Any help is highly appreciated. Thanks
It's better to Update your question or ask another question. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.