Kotlin - IllegalArgumentException: Parameter specified as non-null is null
I am passing data to some fragment in bundle and while receiving it throws the exception. This error occurs while restoring Fragment's state.
Error occurs in Intrinsics.checkParameterIsNotNull when createFromParcel is called. This happen with all non-nullable fields in Model.
Caused by java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.b.h.b, parameter realtorImageUrl
at com.android.app.ui.common.model.Property.(Unknown Source:16)
at com.android.app.ui.common.model.Property$Creator.createFromParcel(Unknown Source:637)
at android.os.Parcel.readParcelable(Parcel.java:2797)
at android.os.Parcel.readValue(Parcel.java:2691)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3058)
at android.os.BaseBundle.unparcel(BaseBundle.java:257)
at android.os.BaseBundle.getInt(BaseBundle.java:961)
at me.yokeyword.fragmentation.SupportFragmentDelegate.onCreate(SourceFile:93)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:48)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.Fragment.restoreChildFragmentState(SourceFile:1444)
at android.support.v4.app.Fragment.onCreate(SourceFile:1415)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:47)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.FragmentController.dispatchCreate(SourceFile:184)
at android.support.v4.app.FragmentActivity.onCreate(SourceFile:355)
at android.support.v7.app.AppCompatActivity.onCreate(SourceFile:84)
at me.yokeyword.fragmentation.SupportActivity.onCreate(SourceFile:38)
at com.android.app.ui.home.HomeActivity.onCreate(SourceFile:47)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
Kotlin 1.1.4, Android Extensions plugin provides Parcelable implementation generator using @Parcelize.
PropertyListFragment.kt
override fun showPropertyDetails(property: Property)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property) = PropertyDetailsFragment().withArgs
putParcelable(INT_EXTRA_PROPERTY, property)
...
What do I need to do to fix the issue?
|
show 8 more comments
I am passing data to some fragment in bundle and while receiving it throws the exception. This error occurs while restoring Fragment's state.
Error occurs in Intrinsics.checkParameterIsNotNull when createFromParcel is called. This happen with all non-nullable fields in Model.
Caused by java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.b.h.b, parameter realtorImageUrl
at com.android.app.ui.common.model.Property.(Unknown Source:16)
at com.android.app.ui.common.model.Property$Creator.createFromParcel(Unknown Source:637)
at android.os.Parcel.readParcelable(Parcel.java:2797)
at android.os.Parcel.readValue(Parcel.java:2691)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3058)
at android.os.BaseBundle.unparcel(BaseBundle.java:257)
at android.os.BaseBundle.getInt(BaseBundle.java:961)
at me.yokeyword.fragmentation.SupportFragmentDelegate.onCreate(SourceFile:93)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:48)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.Fragment.restoreChildFragmentState(SourceFile:1444)
at android.support.v4.app.Fragment.onCreate(SourceFile:1415)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:47)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.FragmentController.dispatchCreate(SourceFile:184)
at android.support.v4.app.FragmentActivity.onCreate(SourceFile:355)
at android.support.v7.app.AppCompatActivity.onCreate(SourceFile:84)
at me.yokeyword.fragmentation.SupportActivity.onCreate(SourceFile:38)
at com.android.app.ui.home.HomeActivity.onCreate(SourceFile:47)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
Kotlin 1.1.4, Android Extensions plugin provides Parcelable implementation generator using @Parcelize.
PropertyListFragment.kt
override fun showPropertyDetails(property: Property)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property) = PropertyDetailsFragment().withArgs
putParcelable(INT_EXTRA_PROPERTY, property)
...
What do I need to do to fix the issue?
stackoverflow.com/questions/49323455/…
– Nilesh Rathod
Oct 29 '18 at 5:53
No, you can't be sure. error is that yourrealtorImageUrlyou specified as non-null gets null for some reason. you might took it as lateinit var which is still not initialized when accessing.
– Jeel Vankhede
Oct 29 '18 at 5:53
@NileshRathod The link which you have provided is the different issue. When accessing java classes in kotlin all the variables are null so you need to use "?" when accessing any java variable. I don't know how parameter in model got null.
– Rajesh
Oct 29 '18 at 6:05
1
@EpicPandaForce I mean to say data is not null when it's displayed. I want to know the reason why it got null.
– Rajesh
Oct 29 '18 at 7:00
1
The problem is may be with Json parsing, Whenever JsonResponse does not have RealtorImageUrl, it will consider it as null
– Lalit Jadav
Nov 16 '18 at 13:02
|
show 8 more comments
I am passing data to some fragment in bundle and while receiving it throws the exception. This error occurs while restoring Fragment's state.
Error occurs in Intrinsics.checkParameterIsNotNull when createFromParcel is called. This happen with all non-nullable fields in Model.
Caused by java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.b.h.b, parameter realtorImageUrl
at com.android.app.ui.common.model.Property.(Unknown Source:16)
at com.android.app.ui.common.model.Property$Creator.createFromParcel(Unknown Source:637)
at android.os.Parcel.readParcelable(Parcel.java:2797)
at android.os.Parcel.readValue(Parcel.java:2691)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3058)
at android.os.BaseBundle.unparcel(BaseBundle.java:257)
at android.os.BaseBundle.getInt(BaseBundle.java:961)
at me.yokeyword.fragmentation.SupportFragmentDelegate.onCreate(SourceFile:93)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:48)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.Fragment.restoreChildFragmentState(SourceFile:1444)
at android.support.v4.app.Fragment.onCreate(SourceFile:1415)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:47)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.FragmentController.dispatchCreate(SourceFile:184)
at android.support.v4.app.FragmentActivity.onCreate(SourceFile:355)
at android.support.v7.app.AppCompatActivity.onCreate(SourceFile:84)
at me.yokeyword.fragmentation.SupportActivity.onCreate(SourceFile:38)
at com.android.app.ui.home.HomeActivity.onCreate(SourceFile:47)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
Kotlin 1.1.4, Android Extensions plugin provides Parcelable implementation generator using @Parcelize.
PropertyListFragment.kt
override fun showPropertyDetails(property: Property)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property) = PropertyDetailsFragment().withArgs
putParcelable(INT_EXTRA_PROPERTY, property)
...
What do I need to do to fix the issue?
I am passing data to some fragment in bundle and while receiving it throws the exception. This error occurs while restoring Fragment's state.
Error occurs in Intrinsics.checkParameterIsNotNull when createFromParcel is called. This happen with all non-nullable fields in Model.
Caused by java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.b.h.b, parameter realtorImageUrl
at com.android.app.ui.common.model.Property.(Unknown Source:16)
at com.android.app.ui.common.model.Property$Creator.createFromParcel(Unknown Source:637)
at android.os.Parcel.readParcelable(Parcel.java:2797)
at android.os.Parcel.readValue(Parcel.java:2691)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3058)
at android.os.BaseBundle.unparcel(BaseBundle.java:257)
at android.os.BaseBundle.getInt(BaseBundle.java:961)
at me.yokeyword.fragmentation.SupportFragmentDelegate.onCreate(SourceFile:93)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:48)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.Fragment.restoreChildFragmentState(SourceFile:1444)
at android.support.v4.app.Fragment.onCreate(SourceFile:1415)
at me.yokeyword.fragmentation.SupportFragment.onCreate(SourceFile:47)
at android.support.v4.app.Fragment.performCreate(SourceFile:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(SourceFile:3194)
at android.support.v4.app.FragmentController.dispatchCreate(SourceFile:184)
at android.support.v4.app.FragmentActivity.onCreate(SourceFile:355)
at android.support.v7.app.AppCompatActivity.onCreate(SourceFile:84)
at me.yokeyword.fragmentation.SupportActivity.onCreate(SourceFile:38)
at com.android.app.ui.home.HomeActivity.onCreate(SourceFile:47)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.-wrap11(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
Kotlin 1.1.4, Android Extensions plugin provides Parcelable implementation generator using @Parcelize.
PropertyListFragment.kt
override fun showPropertyDetails(property: Property)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property) = PropertyDetailsFragment().withArgs
putParcelable(INT_EXTRA_PROPERTY, property)
...
What do I need to do to fix the issue?
edited Nov 12 '18 at 13:30
Rajesh
asked Oct 29 '18 at 5:50
RajeshRajesh
11k44070
11k44070
stackoverflow.com/questions/49323455/…
– Nilesh Rathod
Oct 29 '18 at 5:53
No, you can't be sure. error is that yourrealtorImageUrlyou specified as non-null gets null for some reason. you might took it as lateinit var which is still not initialized when accessing.
– Jeel Vankhede
Oct 29 '18 at 5:53
@NileshRathod The link which you have provided is the different issue. When accessing java classes in kotlin all the variables are null so you need to use "?" when accessing any java variable. I don't know how parameter in model got null.
– Rajesh
Oct 29 '18 at 6:05
1
@EpicPandaForce I mean to say data is not null when it's displayed. I want to know the reason why it got null.
– Rajesh
Oct 29 '18 at 7:00
1
The problem is may be with Json parsing, Whenever JsonResponse does not have RealtorImageUrl, it will consider it as null
– Lalit Jadav
Nov 16 '18 at 13:02
|
show 8 more comments
stackoverflow.com/questions/49323455/…
– Nilesh Rathod
Oct 29 '18 at 5:53
No, you can't be sure. error is that yourrealtorImageUrlyou specified as non-null gets null for some reason. you might took it as lateinit var which is still not initialized when accessing.
– Jeel Vankhede
Oct 29 '18 at 5:53
@NileshRathod The link which you have provided is the different issue. When accessing java classes in kotlin all the variables are null so you need to use "?" when accessing any java variable. I don't know how parameter in model got null.
– Rajesh
Oct 29 '18 at 6:05
1
@EpicPandaForce I mean to say data is not null when it's displayed. I want to know the reason why it got null.
– Rajesh
Oct 29 '18 at 7:00
1
The problem is may be with Json parsing, Whenever JsonResponse does not have RealtorImageUrl, it will consider it as null
– Lalit Jadav
Nov 16 '18 at 13:02
stackoverflow.com/questions/49323455/…
– Nilesh Rathod
Oct 29 '18 at 5:53
stackoverflow.com/questions/49323455/…
– Nilesh Rathod
Oct 29 '18 at 5:53
No, you can't be sure. error is that your
realtorImageUrl you specified as non-null gets null for some reason. you might took it as lateinit var which is still not initialized when accessing.– Jeel Vankhede
Oct 29 '18 at 5:53
No, you can't be sure. error is that your
realtorImageUrl you specified as non-null gets null for some reason. you might took it as lateinit var which is still not initialized when accessing.– Jeel Vankhede
Oct 29 '18 at 5:53
@NileshRathod The link which you have provided is the different issue. When accessing java classes in kotlin all the variables are null so you need to use "?" when accessing any java variable. I don't know how parameter in model got null.
– Rajesh
Oct 29 '18 at 6:05
@NileshRathod The link which you have provided is the different issue. When accessing java classes in kotlin all the variables are null so you need to use "?" when accessing any java variable. I don't know how parameter in model got null.
– Rajesh
Oct 29 '18 at 6:05
1
1
@EpicPandaForce I mean to say data is not null when it's displayed. I want to know the reason why it got null.
– Rajesh
Oct 29 '18 at 7:00
@EpicPandaForce I mean to say data is not null when it's displayed. I want to know the reason why it got null.
– Rajesh
Oct 29 '18 at 7:00
1
1
The problem is may be with Json parsing, Whenever JsonResponse does not have RealtorImageUrl, it will consider it as null
– Lalit Jadav
Nov 16 '18 at 13:02
The problem is may be with Json parsing, Whenever JsonResponse does not have RealtorImageUrl, it will consider it as null
– Lalit Jadav
Nov 16 '18 at 13:02
|
show 8 more comments
3 Answers
3
active
oldest
votes
Since the realtorImageUrl is defined not null, so kotlin does not permit to set it to null. Therefore the only possible way to do that is to set the null value by reflection. Json helper libraries are based on annotation processing and using reflection for setting value to the fields, so the problem definitely comes from them.
- First of all, I highly recommend you to use google
Gson. - Second, be sure that the json string is not null or empty after configuration changes.
- Third, be sure that you are using
moshi-kotlin(not simplemoshi) in dependencies and adding an instance ofKotlinJsonAdapterFactoryto themoshibuilder.
build.gradle:
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
Test:
val json = ""RealtorImageUrl": "http://www.gstatic.com/tv/thumb/persons/667736/667736_v9_ba.jpg""
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory()) // com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
.build()
val jsonAdapter = moshi.adapter(Property::class.java) as JsonAdapter<Property>
val property = jsonAdapter.fromJson(json) as Property
supportFragmentManager.beginTransaction()
.replace(R.id.container, PropertyDetailsFragment.newInstance(property))
.commit()
It is better to use withArguments from anko to put arguments for the fragment:
PropertyDetailsFragment.kt:
class PropertyDetailsFragment : Fragment()
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val property = arguments?.getParcelable(KEY_EXTRA_PROPERTY) as Property
val realtorImageUrl = property.realtorImageUrl
companion object
private const val KEY_EXTRA_PROPERTY = "KEY_EXTRA_PROPERTY"
fun newInstance(property: Property) = PropertyDetailsFragment().withArguments(
KEY_EXTRA_PROPERTY to property
)
@Rajesh: What is a library with@Jsonannotation? I have tested it using googleGsonand no problem happened in restoring fragment state like phone rotation.
– aminography
Nov 12 '18 at 13:58
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
1
Ok, you have defined therealtorImageUrlnot null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.
– aminography
Nov 14 '18 at 6:35
|
show 6 more comments
Well, this might not be the perfect solution but it can solve your issue. You can make your property nullable and just add a null check whenever you access them.
Just initialize you variable like this
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
PropertyListFragment.kt
override fun showPropertyDetails(property: Property?)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
And in PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property?) = PropertyDetailsFragment().withArgs
property?.let
putParcelable(INT_EXTRA_PROPERTY, property)
...
The problem is not nullity ofproperty,realtorImageUrlmakes the issue.
– aminography
Nov 15 '18 at 16:48
add a comment |
Can u try using @field:Json(name = "RealtorImageUrl") and check if it works then?
Also, check if you have added proper factory KotlinJsonAdapterFactory()
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53039487%2fkotlin-illegalargumentexception-parameter-specified-as-non-null-is-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since the realtorImageUrl is defined not null, so kotlin does not permit to set it to null. Therefore the only possible way to do that is to set the null value by reflection. Json helper libraries are based on annotation processing and using reflection for setting value to the fields, so the problem definitely comes from them.
- First of all, I highly recommend you to use google
Gson. - Second, be sure that the json string is not null or empty after configuration changes.
- Third, be sure that you are using
moshi-kotlin(not simplemoshi) in dependencies and adding an instance ofKotlinJsonAdapterFactoryto themoshibuilder.
build.gradle:
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
Test:
val json = ""RealtorImageUrl": "http://www.gstatic.com/tv/thumb/persons/667736/667736_v9_ba.jpg""
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory()) // com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
.build()
val jsonAdapter = moshi.adapter(Property::class.java) as JsonAdapter<Property>
val property = jsonAdapter.fromJson(json) as Property
supportFragmentManager.beginTransaction()
.replace(R.id.container, PropertyDetailsFragment.newInstance(property))
.commit()
It is better to use withArguments from anko to put arguments for the fragment:
PropertyDetailsFragment.kt:
class PropertyDetailsFragment : Fragment()
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val property = arguments?.getParcelable(KEY_EXTRA_PROPERTY) as Property
val realtorImageUrl = property.realtorImageUrl
companion object
private const val KEY_EXTRA_PROPERTY = "KEY_EXTRA_PROPERTY"
fun newInstance(property: Property) = PropertyDetailsFragment().withArguments(
KEY_EXTRA_PROPERTY to property
)
@Rajesh: What is a library with@Jsonannotation? I have tested it using googleGsonand no problem happened in restoring fragment state like phone rotation.
– aminography
Nov 12 '18 at 13:58
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
1
Ok, you have defined therealtorImageUrlnot null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.
– aminography
Nov 14 '18 at 6:35
|
show 6 more comments
Since the realtorImageUrl is defined not null, so kotlin does not permit to set it to null. Therefore the only possible way to do that is to set the null value by reflection. Json helper libraries are based on annotation processing and using reflection for setting value to the fields, so the problem definitely comes from them.
- First of all, I highly recommend you to use google
Gson. - Second, be sure that the json string is not null or empty after configuration changes.
- Third, be sure that you are using
moshi-kotlin(not simplemoshi) in dependencies and adding an instance ofKotlinJsonAdapterFactoryto themoshibuilder.
build.gradle:
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
Test:
val json = ""RealtorImageUrl": "http://www.gstatic.com/tv/thumb/persons/667736/667736_v9_ba.jpg""
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory()) // com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
.build()
val jsonAdapter = moshi.adapter(Property::class.java) as JsonAdapter<Property>
val property = jsonAdapter.fromJson(json) as Property
supportFragmentManager.beginTransaction()
.replace(R.id.container, PropertyDetailsFragment.newInstance(property))
.commit()
It is better to use withArguments from anko to put arguments for the fragment:
PropertyDetailsFragment.kt:
class PropertyDetailsFragment : Fragment()
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val property = arguments?.getParcelable(KEY_EXTRA_PROPERTY) as Property
val realtorImageUrl = property.realtorImageUrl
companion object
private const val KEY_EXTRA_PROPERTY = "KEY_EXTRA_PROPERTY"
fun newInstance(property: Property) = PropertyDetailsFragment().withArguments(
KEY_EXTRA_PROPERTY to property
)
@Rajesh: What is a library with@Jsonannotation? I have tested it using googleGsonand no problem happened in restoring fragment state like phone rotation.
– aminography
Nov 12 '18 at 13:58
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
1
Ok, you have defined therealtorImageUrlnot null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.
– aminography
Nov 14 '18 at 6:35
|
show 6 more comments
Since the realtorImageUrl is defined not null, so kotlin does not permit to set it to null. Therefore the only possible way to do that is to set the null value by reflection. Json helper libraries are based on annotation processing and using reflection for setting value to the fields, so the problem definitely comes from them.
- First of all, I highly recommend you to use google
Gson. - Second, be sure that the json string is not null or empty after configuration changes.
- Third, be sure that you are using
moshi-kotlin(not simplemoshi) in dependencies and adding an instance ofKotlinJsonAdapterFactoryto themoshibuilder.
build.gradle:
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
Test:
val json = ""RealtorImageUrl": "http://www.gstatic.com/tv/thumb/persons/667736/667736_v9_ba.jpg""
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory()) // com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
.build()
val jsonAdapter = moshi.adapter(Property::class.java) as JsonAdapter<Property>
val property = jsonAdapter.fromJson(json) as Property
supportFragmentManager.beginTransaction()
.replace(R.id.container, PropertyDetailsFragment.newInstance(property))
.commit()
It is better to use withArguments from anko to put arguments for the fragment:
PropertyDetailsFragment.kt:
class PropertyDetailsFragment : Fragment()
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val property = arguments?.getParcelable(KEY_EXTRA_PROPERTY) as Property
val realtorImageUrl = property.realtorImageUrl
companion object
private const val KEY_EXTRA_PROPERTY = "KEY_EXTRA_PROPERTY"
fun newInstance(property: Property) = PropertyDetailsFragment().withArguments(
KEY_EXTRA_PROPERTY to property
)
Since the realtorImageUrl is defined not null, so kotlin does not permit to set it to null. Therefore the only possible way to do that is to set the null value by reflection. Json helper libraries are based on annotation processing and using reflection for setting value to the fields, so the problem definitely comes from them.
- First of all, I highly recommend you to use google
Gson. - Second, be sure that the json string is not null or empty after configuration changes.
- Third, be sure that you are using
moshi-kotlin(not simplemoshi) in dependencies and adding an instance ofKotlinJsonAdapterFactoryto themoshibuilder.
build.gradle:
implementation 'com.squareup.moshi:moshi-kotlin:1.8.0'
Test:
val json = ""RealtorImageUrl": "http://www.gstatic.com/tv/thumb/persons/667736/667736_v9_ba.jpg""
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory()) // com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
.build()
val jsonAdapter = moshi.adapter(Property::class.java) as JsonAdapter<Property>
val property = jsonAdapter.fromJson(json) as Property
supportFragmentManager.beginTransaction()
.replace(R.id.container, PropertyDetailsFragment.newInstance(property))
.commit()
It is better to use withArguments from anko to put arguments for the fragment:
PropertyDetailsFragment.kt:
class PropertyDetailsFragment : Fragment()
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
val property = arguments?.getParcelable(KEY_EXTRA_PROPERTY) as Property
val realtorImageUrl = property.realtorImageUrl
companion object
private const val KEY_EXTRA_PROPERTY = "KEY_EXTRA_PROPERTY"
fun newInstance(property: Property) = PropertyDetailsFragment().withArguments(
KEY_EXTRA_PROPERTY to property
)
edited Nov 15 '18 at 19:18
answered Nov 12 '18 at 13:46
aminographyaminography
6,46521835
6,46521835
@Rajesh: What is a library with@Jsonannotation? I have tested it using googleGsonand no problem happened in restoring fragment state like phone rotation.
– aminography
Nov 12 '18 at 13:58
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
1
Ok, you have defined therealtorImageUrlnot null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.
– aminography
Nov 14 '18 at 6:35
|
show 6 more comments
@Rajesh: What is a library with@Jsonannotation? I have tested it using googleGsonand no problem happened in restoring fragment state like phone rotation.
– aminography
Nov 12 '18 at 13:58
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
1
Ok, you have defined therealtorImageUrlnot null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.
– aminography
Nov 14 '18 at 6:35
@Rajesh: What is a library with
@Json annotation? I have tested it using google Gson and no problem happened in restoring fragment state like phone rotation.– aminography
Nov 12 '18 at 13:58
@Rajesh: What is a library with
@Json annotation? I have tested it using google Gson and no problem happened in restoring fragment state like phone rotation.– aminography
Nov 12 '18 at 13:58
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I am using Moshi library. Is there any difference when using Anko library?
– Rajesh
Nov 13 '18 at 4:30
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
I think the problem comes from moshi library. I have updated the answer.
– aminography
Nov 13 '18 at 5:45
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
Can you explain why there is problem with moshi library?
– Rajesh
Nov 14 '18 at 5:28
1
1
Ok, you have defined the
realtorImageUrl not null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.– aminography
Nov 14 '18 at 6:35
Ok, you have defined the
realtorImageUrl not null and kotlin does not permit to set it null. The only possible way to do that is to set the null value by reflection. As annotation processor json libraries set values to the fields using reflection, so the problem definitely comes from them.– aminography
Nov 14 '18 at 6:35
|
show 6 more comments
Well, this might not be the perfect solution but it can solve your issue. You can make your property nullable and just add a null check whenever you access them.
Just initialize you variable like this
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
PropertyListFragment.kt
override fun showPropertyDetails(property: Property?)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
And in PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property?) = PropertyDetailsFragment().withArgs
property?.let
putParcelable(INT_EXTRA_PROPERTY, property)
...
The problem is not nullity ofproperty,realtorImageUrlmakes the issue.
– aminography
Nov 15 '18 at 16:48
add a comment |
Well, this might not be the perfect solution but it can solve your issue. You can make your property nullable and just add a null check whenever you access them.
Just initialize you variable like this
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
PropertyListFragment.kt
override fun showPropertyDetails(property: Property?)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
And in PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property?) = PropertyDetailsFragment().withArgs
property?.let
putParcelable(INT_EXTRA_PROPERTY, property)
...
The problem is not nullity ofproperty,realtorImageUrlmakes the issue.
– aminography
Nov 15 '18 at 16:48
add a comment |
Well, this might not be the perfect solution but it can solve your issue. You can make your property nullable and just add a null check whenever you access them.
Just initialize you variable like this
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
PropertyListFragment.kt
override fun showPropertyDetails(property: Property?)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
And in PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property?) = PropertyDetailsFragment().withArgs
property?.let
putParcelable(INT_EXTRA_PROPERTY, property)
...
Well, this might not be the perfect solution but it can solve your issue. You can make your property nullable and just add a null check whenever you access them.
Just initialize you variable like this
Property.kt
@Parcelize
data class Property(
...
@Json(name = "RealtorImageUrl")
val realtorImageUrl: String
...
) : Parcelable
PropertyListFragment.kt
override fun showPropertyDetails(property: Property?)
(parentFragment as PropertySearchResultFragment).start(
PropertyDetailsFragment.newInstance(property)
)
And in PropertyDetailsFragment.kt
class PropertyDetailsFragment
...
companion object
fun newInstance(property: Property?) = PropertyDetailsFragment().withArgs
property?.let
putParcelable(INT_EXTRA_PROPERTY, property)
...
answered Nov 15 '18 at 14:49
Birju VachhaniBirju Vachhani
359113
359113
The problem is not nullity ofproperty,realtorImageUrlmakes the issue.
– aminography
Nov 15 '18 at 16:48
add a comment |
The problem is not nullity ofproperty,realtorImageUrlmakes the issue.
– aminography
Nov 15 '18 at 16:48
The problem is not nullity of
property, realtorImageUrl makes the issue.– aminography
Nov 15 '18 at 16:48
The problem is not nullity of
property, realtorImageUrl makes the issue.– aminography
Nov 15 '18 at 16:48
add a comment |
Can u try using @field:Json(name = "RealtorImageUrl") and check if it works then?
Also, check if you have added proper factory KotlinJsonAdapterFactory()
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
add a comment |
Can u try using @field:Json(name = "RealtorImageUrl") and check if it works then?
Also, check if you have added proper factory KotlinJsonAdapterFactory()
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
add a comment |
Can u try using @field:Json(name = "RealtorImageUrl") and check if it works then?
Also, check if you have added proper factory KotlinJsonAdapterFactory()
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
Can u try using @field:Json(name = "RealtorImageUrl") and check if it works then?
Also, check if you have added proper factory KotlinJsonAdapterFactory()
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
answered Nov 16 '18 at 16:28
Matej VukosavMatej Vukosav
433512
433512
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53039487%2fkotlin-illegalargumentexception-parameter-specified-as-non-null-is-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
stackoverflow.com/questions/49323455/…
– Nilesh Rathod
Oct 29 '18 at 5:53
No, you can't be sure. error is that your
realtorImageUrlyou specified as non-null gets null for some reason. you might took it as lateinit var which is still not initialized when accessing.– Jeel Vankhede
Oct 29 '18 at 5:53
@NileshRathod The link which you have provided is the different issue. When accessing java classes in kotlin all the variables are null so you need to use "?" when accessing any java variable. I don't know how parameter in model got null.
– Rajesh
Oct 29 '18 at 6:05
1
@EpicPandaForce I mean to say data is not null when it's displayed. I want to know the reason why it got null.
– Rajesh
Oct 29 '18 at 7:00
1
The problem is may be with Json parsing, Whenever JsonResponse does not have RealtorImageUrl, it will consider it as null
– Lalit Jadav
Nov 16 '18 at 13:02