繁体   English   中英

线程“Thread-6”kotlin.UninitializedPropertyAccessException 中的异常:lateinit 属性尚未初始化

[英]xception in thread “Thread-6” kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized

大家好,我正在开发一个使用 Spring 引导的应用程序,尽管在生成以下错误的线程中初始化 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 启动它,这就是 function 的样子

@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 的 CrudRepository

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

}

谢谢

您不需要使用lateinit来自动装配您的存储库

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

@Component
class ProvisioningController(private val repository:ContactTypeAudioPathRepository) {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM