So you have to be careful calling "toString()" on a potential null
object.
Especially if you plan to cast it to a double.
Note* Your code also creates an array of doubles from a single entry in the
EditText so additional checking of the text format before casting it
should be done as well to ensure it is a double. You can take the lazy way and wrap it in a try/catch
to tell them it isn't formatted properly if you would like.
Handling Number format exceptions means you need to confirm that it is a double before you use it. You can use a nice utility or you can write a simple helper method. Guava has a helper method, Apache has helper methods or you can write a regex pattern matcher like here. The nice thing about regex is that you aren't relying on a try/catch to identify an issue.
How to check that a string is parseable to a double?
But for sake of thoroughness you could do something like:
private boolean isDouble(String value) {
try {
Double.parseDouble(value);
return true;
} catch (Exception ex) {
return false;
}
}
You have a few options for handling your null.
1) Check for null in a "if / else" statement.
double grade[];
if (myText.getText() != null && isDouble(myText.getText().toString()) {
grade = {Double.parseDouble(myText.getText().toString())}
} else {
//it's null, don't use it
}
2) check for null inline
if (isDouble(myText.getText().toString()) {
double grade[] = myText.getText() == null ? {0.0, 0.0} : {Double.parseDouble(myText.getText().toString())}
}
3) Allow null as an option
double grade[] = myText.getText() == null ? null : {Double.parseDouble(myText.getText().toString())}
4) Cheat and use a Try/Catch (if you don't want to master handling this the best ways, and just want to make sure it doesn't crash and you can notify the user of a data formatting issue, you can simply wrap a Try/Catch around all your code and say "not formatted properly" each time, but that is not very descriptive and possibly not helpful enough, just fyi.
5) Or if you are willing to use Kotlin you can make great extension methods for each.
fun EditText.doubleArrayOrDefault(): ArrayList<Double> {
return if (this.getText() != null && isDouble(myText.getText().toString())) {
{Double.parseDouble(myText.getText().toString())}
} else {
{0.0, 0.0} // or whatever you want your default to be
}
}
6) Lastly, you could simply use DataBinding which would always be my personal preference. Update your gradle to enable DataBining and accept the inevitable that it's the greatest thing to happen since sliced break ;).
in the Activity you would have
var myObservabeText: ObservableField<String> = ObservableField("defaultTextIfAny")
in the onCreate you would set the variable
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_my)
mBinding.activity = this
}
in the XML you would update it to be
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="activity" type="com.package.a35.activities.MyActivity" />
</data>
<EditText
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={activity.myObservableText}" />
Of course you can also add a JVMStatic to an adapter to handle binding to an array of double as well, if you want to write a nice adapter that is pretty straight forward as well.
Of course this is all pseudo and not typed in the actual IDE, but it will be very close, so hopefully this will help you with options.
Best of luck, hopefully one of these options fits your needs nicely.
Happy Coding.