https://kotlinlang.org logo
#android
Title
# android
b

bringoff

11/28/2017, 6:51 AM
Welp, I’ve just tried to update support library to 27.0.1 version from 26.1.0 and got a whole bunch of nullability errors because a lot of methods like
getActivity
in Fragment got
@Nullable
annotation. But what should I do now in following situations? For example, I build DialogFragment. I need a context in
onCreateDialog
method. To do something like this:
Copy code
return MaterialDialog.Builder(context)
                .title(R.string.invitation_to_team_dialog_title)
                .content(buildDescriptionText())
                .build()
onCreateDialog
method must return NonNull Dialog instance so I can’t use
context?.let
call. And I really doubt context may be null on this stage (as well as activity in Fragment’s
onCreateView
method, I suppose?). So I forced to use ugly
!!
call. @jw as I know from your twitter these annotations were your endeavor. So, from your opinion, what should be an intended workaround in this situation? I don’t really like double exclamation point. It looks like very Java-ish way to care about nullability, sort of “I don’t care, whatever”
1
g

gildor

11/28/2017, 7:40 AM
Copy code
val context = context ?: error("Context is null")
return MaterialDialog.Builder(context)
       .title(R.string.invitation_to_team_dialog_title)
                .content(buildDescriptionText())
                .build()
p

Paul Woitaschek

11/28/2017, 8:43 AM
That's too much boilderplate for me
If you just do context!! you will get an error that is not less meaningfull
g

gildor

11/28/2017, 8:46 AM
agree, but if “I don’t really like double exclamation point” explicit error handling could be solution. Particularly for this case I prefer
!!
, but for cases when you can just return from function just easier to do that:
Copy code
val context = context ?: return
p

Paul Woitaschek

11/28/2017, 9:30 AM
Yeah it depends
Everyone does that but I think it's really error prone to just skip a part of a code if something was different than you expected
I mean if you access the context in onResume and it's null something is so wrong that continuing will only make things worser and you see bugs that should be even more impossible
So I have lots of !! and if they blow me up, I either made an error or just didn't understand my own code 😛
👍 1
5 Views