有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

线程“Thread6”kotlin中的java异常。UninitializedPropertyAccessException:lateinit属性尚未初始化

大家好,我正在用Spring Boot开发一个应用程序,尽管在一个线程中初始化JPA存储库时遇到了一些问题,生成了以下错误

Exception in thread "Thread-6" kotlin.UninitializedPropertyAccessException: lateinit property contactTypeAudioPathRepository has not been initialized
    at com.sopristec.sa_tas.controllers.ProvisioningController$createAudioWithAsync$executeP$1.run(ProvisioningController.kt:96)
    at java.lang.Thread.run(Thread.java:748)

我正在使用@Autowired启动它,这就是函数的外观

@Autowired
private  lateinit var contactTypeAudioPathRepository: ContactTypeAudioPathRepository


fun createAudioWithAsync(menuChoice: String, presentation: String) {

        val executeP = Thread{
            val message = "Press $menuChoice to save this phone number as $presentation. Or " +
                    "Press 9 to save as work number"


            val commandVoice = mutableListOf<String>("python","pythonScript/main.py",message);

            val buildAudio = ProcessBuilder(commandVoice).start()

            val getPath = buildAudio.inputStream.bufferedReader().use { it.readText() }

            if(getPath.isEmpty()){
                println("A python script failed")
            }
            println(getPath.replace("\n","").replace("att_sas_pyttsx3/",""))

            val fileName = getPath.replace("\n","")
            contactTypeAudioPathRepository.save(ContactTypeAudioPath(0,fileName))

           // Genera error --> contactTypeAudioPathRepository.add("mediaLink").subscribe()


        }.start()
//        executeP.start();
        //contactTypeAudioPathRepository.add("mediaLink").subscribe()

    }

这是存储库,正如您可以看到的JPA的Crudepository

@Repository
interface ContactTypeAudioPathRepository : CrudRepository<ContactTypeAudioPath,Int> {

}

谢谢


共 (1) 个答案

  1. # 1 楼答案

    您不需要使用lateinit自动连接存储库

    Spring允许您使用构造函数注入

    @Component
    class ProvisioningController(private val repository:ContactTypeAudioPathRepository) {
    
       fun createAudioWithAsync(menuChoice: String, presentation: String) {
           ....
       }
    }