I'm using Flyway to attempt to create and then seed a database when the Spring Boot web app starts up. Flyway successfully creates the database tables in the first migration, but fails to populate them in the 2nd because of a NullPointerException.
Here is the migration code, called V2_seed_database.java, located in package db.migration:
package db.migration;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import net.tekknow.medaverter.db.seeds.AppointmentSeeder;
public class V2__seed_database extends BaseJavaMigration {
    public void migrate(Context context) {
        AppointmentSeeder appointmentSeeder = new AppointmentSeeder();
        appointmentSeeder.seed();
    }
}
Here is the AppointmentSeeder code:
package net.tekknow.medaverter.db.seeds;
import org.json.JSONArray; 
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.tekknow.medaverter.domain.Appointment;
import net.tekknow.medaverter.service.AppointmentService;
@Service
public class AppointmentSeeder {
    @Autowired
    AppointmentService appointmentService;
    @PostConstruct
    public void seed() {
        String json = "[" +
            "{\"id\":1,\"patient_id\":1,\"dateTime\":\"10/29/2010\",\"physician_id\":1,\"lab_id\":1,\"note_id\":0}" +
        "]";
        org.json.JSONArray appointments = new JSONArray(json);
        for (int i=0; i<appointments.length(); i++) {
            JSONObject appointment = appointments.getJSONObject(i);
            Appointment dbAppointment = new Appointment();
            dbAppointment.setId(appointment.getInt("id"));
            dbAppointment.setPatientId(appointment.getInt("patient_id"));
            dbAppointment.setDateTime(appointment.getString("dateTime"));
            dbAppointment.setPhysicianId(appointment.getInt("physician_id"));
            dbAppointment.setLabId(appointment.getInt("lab_id"));
            dbAppointment.setNoteId(appointment.getInt("note_id"));
            appointmentService.save(dbAppointment);
        }
    }
}   
Here is the AppointmentRepository code:
package net.tekknow.medaverter.db;
import org.springframework.data.jpa.repository.JpaRepository;
import net.tekknow.medaverter.domain.Appointment;
public interface AppointmentRepository extends JpaRepository<Appointment,Integer> {
}
And here is the AppointmentService code:
package net.tekknow.medaverter.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.tekknow.medaverter.db.AppointmentRepository;
import net.tekknow.medaverter.domain.Appointment;
@Service
@Transactional
public class AppointmentService {
    @Autowired
    AppointmentRepository repo;
    public void save(Appointment appointment) {
        System.out.println("AppointmentService.save: appointment="+appointment.toString());
        repo.save(appointment);  //its failing here
    }  
}
Here is the Appointment bean:
package net.tekknow.medaverter.domain;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
@Entity
@Table(name = "appointments")
public class Appointment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @NotBlank
    @Column(unique = true)
    private int patient_id;
    @Size(max = 32)
    private String date_time;
    private int physician_id;
    private int lab_id;
    private int note_id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
... other getters and setters, truncated for brevity
When I run the program, the migration starts but fails on this line:
    repo.save(appointment);  //its failing here
with the following error message:
Caused by: java.lang.NullPointerException: null at net.tekknow.medaverter.service.AppointmentService.save(AppointmentService.java:32) ~[classes/:na]
Just before the line of code that fails, I output the contents of the object that it says is null, and its not null:
AppointmentService.save: appointment=id:1, patient_id:1, date_time:10/29/2010, physician_id:1, lab_id:1, note_id:0
Any suggestions?
 
     
    