Skip to content

Fix : UninitializedPropertyAccessException #6248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Ensure basicKvStoreFactory is always initialized before use
presenter?.setupBasicKvStoreFactory { BasicKvStore(this@UploadActivity, it) }

_binding = ActivityUploadBinding.inflate(layoutInflater)
setContentView(binding.root)

Expand Down Expand Up @@ -903,7 +906,6 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
// Save the user's choice to not show the dialog again
defaultKvStore.putBoolean("hasAlreadyLaunchedCategoriesDialog", true)
}
presenter!!.setupBasicKvStoreFactory { BasicKvStore(this@UploadActivity, it) }
presenter!!.checkImageQuality(0)
UploadMediaPresenter.isCategoriesDialogShowing = false
}
Expand Down
29 changes: 26 additions & 3 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class UploadPresenter @Inject internal constructor(

private val compositeDisposable = CompositeDisposable()

lateinit var basicKvStoreFactory: (String) -> BasicKvStore

private var basicKvStoreFactory: ((String) -> BasicKvStore)? = null
/**
* Called by the submit button in [UploadActivity]
*/
Expand Down Expand Up @@ -132,14 +131,38 @@ class UploadPresenter @Inject internal constructor(
basicKvStoreFactory = factory
}

/**
* Returns the current BasicKvStore factory or throws if not initialized.
*
* @throws IllegalStateException if basicKvStoreFactory has not been initialized.
*/
private fun getBasicKvStoreFactory(): (String) -> BasicKvStore {
return basicKvStoreFactory ?: throw IllegalStateException("basicKvStoreFactory has not been initialized")
}

/**
* Ensures that the BasicKvStore factory has been initialized before use.
*
* @throws IllegalStateException if the factory is null.
*/
private fun requireFactoryInitialized() {
val field = this::class.java.getDeclaredField("basicKvStoreFactory")
field.isAccessible = true
val value = field.get(this)
if (value == null) {
throw IllegalStateException("basicKvStoreFactory must be initialized before use. Please call setupBasicKvStoreFactory() before using presenter methods that require it.")
}
}

/**
* Calls checkImageQuality of UploadMediaPresenter to check image quality of next image
*
* @param uploadItemIndex Index of next image, whose quality is to be checked
*/
override fun checkImageQuality(uploadItemIndex: Int) {
requireFactoryInitialized()
repository.getUploadItem(uploadItemIndex)?.let {
presenter.setupBasicKvStoreFactory(basicKvStoreFactory)
presenter.setupBasicKvStoreFactory(getBasicKvStoreFactory())
presenter.checkImageQuality(it, uploadItemIndex)
}
}
Expand Down