I`ve problem with converting String to Integer.
public SharedPreferences abc;
abc = getApplicationContext().getSharedPreferences("Trening",0);
Integer i = Integer.parseInt(abc.getString("T1",null).toString());
The Error is:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
I learn to program for 3 days and hope you can help me and explain how to fix it, so I can learn.
Sorry for my english. :)
If abc.getString("T1", null) returns null (this will happen if there is no Key-Value-Pair with the key "T1" in your SharedPreferences) then you try to get the String of null. This causes the NullPointerException. You should define another defaultValue which can be converted into a String.
Apart from that, you can leave toString() out because getString() returns a String:
Integer i = Integer.parseInt(abc.getString("T1", "-1"));
When you are using null as second argument in abc.getString("T1", null), actualy you are saying:
if "T1" value is not set, return null as default.
So you have parsInt(null) and it causes NullPointerExceprion.
You can replace abc.getString("T1", null) with abc.getString("T1", "0")
It will returns string "0" as default value of "T1" (if "T1" is null). So you have parsInt("0") that it works proprely.
So the problem basically is that the variable abc is null, therefore the getSharedPreferences call fails to retrieve a valid reference.
Related
Craslitycs send a crash event of null pointer exception
NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
that refers to mutablelist.addAll(it.list), where it.list is an object from an api response.
When I try to check if this can be null, (it.list != null), AndroidStudio suggest Condition 'it.list != null' is always 'true' so, why this crash? And how can i fix it?
Thanks
You can check your list is empty or not by using kotlin collections function.
Use below function
ArrayList.isNullOrEmpty()
Reference link:
Kotlin collection
I don't understand why Android Studio is not able to tell that, although SharedPreferences declares the defValue of getString as #Nullable, the current value is actually not null!
The result of the following call:
myFunction(getSharedPreferences().getString(MY_SETTING_NAME.name(), "This string is not null"))
will trigger a warning:
Argument might be null
How can it be? Since defValue is actually not null and we know it...
The Android framework does not use Kotlin contracts and cannot change the #Nullable annotation on the return value based on whether the defValue you pass is null or not.
As an alternative, you should consider using Kotlin's elvis operator and writing code such as:
myFunction(getSharedPreferences().getString(MY_SETTING_NAME.name(), null)
?: "This string is not null")
Which correctly evaluates as non-null.
I have a data class
data class Bean {
var value: String = ""
}
I got a Json from server:
{
"value":null
}
What I expect is since value is not optional, so the null cannot be assigned and value will remain as "". But using the debugger I found that value is null, and therefore some method I called on it throws the below exception:
fun matches(stringToMatch: String): Boolean {
return this.value.toLowerCase() == stringToMatch.toLowerCase()
}
kotlin.TypeCastException: null cannot be cast to non-null type java.lang.String
How can I make value remains ""?
You're using a JSON deserialization library (GSON?) that creates your objects by completely sidestepping the initialization you built into them. Most probably it uses a low-level, non-JDK call sun.misc.Unsafe.allocateObject() to allocate a raw data block for your object and then proceeds to populate it from parsed JSON.
The only way to truly fix this is using such a JSON library that understands Kotlin and can replicate its object initialization rules.
BTW your specific JSON example, which explicitly assigns null to a non-null property, should not use the default value, but throw a validation exception.
I'm am a newbie with Android development and using the following example to do something similar: https://stackoverflow.com/a/11626706/5724649
But am getting the following error: "Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference"when I try to set one of the radio button. Also mSource (which is an ArrayList) is null within getView(), so I see why I could get this error, but not sure as to how I should pass the arraylist to getView(). Please help.
The error is about you calling a method on an object which currently "null".
This will also throw the same error:
String a = null;
a.length(); // <<< This will cause an error, because a is null.
You should make sure the following line:
mSource = new ArrayList<RowObject>();
is running before any of these lines:
mSource.get(position).setFirstChecked(true);
mSource.get(position).setFirstChecked(false);
if (mSource.get(position).isFirstChecked()) {
I am just wondering what the hell is going on under the hood when the intent extra doesn't exist in the first place?
For example:
String remindOnString = intent.getStringExtra(NoteExtrasKey.EXTRA_NOTE_REMINDON);
if(remindOnString != null && !remindOnString.equals(""))
mRemindDateTime = Timestamp.valueOf(remindOnString);
It is quite strange to me the expression: remindOnString != null is true despite the fact that remindOnString receives null.
Here is the screenshot of the watches I set for this variable:
I don't know if I missed something from the documentation. But this is very strange to me. According to the tool-tip (Which I believe from the api-docs)
intent.getStringExtra(KEY) returns the string or null.
What kind of sorcery is this?
It returns value "null" but still a object .