I print the fotonew key and it sends a null value. But if urlfoto is empty or not, if (urlfoto! = Null &&! Urlfoto.equals (" ")) goes to this section. Normally he should go to else. The urlfoto value goes to the part with a full urlfoto value in the case of null but if else. I want to check if the key I pulled is empty or full.
SharedPreferences sharedPrefNew = getContext().getSharedPreferences("sharedPref",Context.MODE_PRIVATE);
urlfoto = sharedPrefNew.getString("fotonew", "");
if(urlfoto!= null && !urlfoto.equals(""))
{
...
}
else {
System.out.println("boş");
...
}
You already declared default value of SharedPreference on Code.
urlfoto = sharedPrefNew.getString("fotonew", "");
This means if "fotonew" is empty or null sharedPrefNew returns a value ""
this line:
sharedPrefNew.getString("fotonew", "")
is responsible for taking value from shared pref. First parameter is a key, second one is a default value that will be returned if key is not found.
In your case, if there is no object stored for key "fotoNew" an empty string will be returned.
You can return null :
sharedPrefNew.getString("fotonew", null);
Hope that helps.
Check below code
if (sharedPrefNew.getString("fotonew", "") != null && !sharedPrefNew.getString("fotonew", "").isEmpty()){
//your code here
}
Related
I am checking all these at splashscreen activity,
So I am giving this,
SharedPreferences spf = getSharedPreferences("myprfs", Context.MODE_PRIVATE);
SharedPreferences.Editor spe = spf.edit();
String name = spf.getString("name","");
String id = spf.getString("id","");
String class = spf.getString("class","");
String roll = spf.getString("roll","");
spe.commit();
if((spe != null) && (name != null) && (id != null) && (class != null) && (roll != null)){
Intent i = new Intent(Startpage.this,Welcome.class);
startActivity(i);
Startpage.this.finish();
}
else{
Intent i = new Intent(Startpage.this,MainActivity.class);
startActivity(i);
Startpage.this.finish();
}
when I open app startup page directly moves to welcome page,
but I want to move splashscreen page to MainActivity when there is no values saved in shared-preferences
I followed this to get shared-preference values
can any one suggest me how to give condition in this kind
spf.getString("name","");
return as default value a empty string.
Idea:
why not use
spf.contains("name")
that return true or false?
Alternatively you can check if string is empty or null with TextUtils.isEmpty()
method.
String name = spf.getString("name","");
This method creates a shared pref if it doesn't exist already with the default value as the second argument. So in your case, shared pref are saved with empty string as value when user visited the app for first time.
Just change your default value to null since your checks are on null value and it will work -
String name = spf.getString("name",null);
you are putting null check on the string but while reading from the shared preferences you are setting them to empty string if the value is not found in the shared prefs. change your checks to String.isEmpty() in your if condition.
because null and ""are different,
You should use android.text.TextUtils.isEmpty(CharSequence str)instead of str != null
Another way is :
String name = spf.getString("name",null); ,and then like if (Str != null)
For me,i always usee the first way i said to determine whether String is empty.
Hope it helps you :)
This question already has answers here:
Check whether a String is not Null and not Empty
(35 answers)
Closed 6 years ago.
May occur any exception in coding for checking string is null or not ? Please help.
String code ;
if (!code.equals(null)) {
}
else
{
}
Here is how you can check if String value is null or not
if(code != null){
}else{
}
You can not us !code.equals(null) because, equals is used to compare same object type. null is not any object type and code is String. If you consider null as String, then you can use !code.equals("null")
String can be checked like this:
if(code.equals("null") || code.equals("")) {
// code to do when string is null.
}
else {
// code to do when string is not null.
}
equals() is use to check the similarity of two String, and == or != is use to check the condition. In your case you are checking the similarity of string.
if (!code.equals(null)) {
//code checks that String code is equal to null or not
}
else
{
}
another
if (code != null) {
//code checks if code is not equals to null (condition checking)
}
else
{
}
There are many ways to check if String is empty in Java, but what is the right way of doing it? right in the sense of robustness, performance and readability. If robustness is your priority then using equals() method or Apache commons StringUtils is the right way to do this check. If you don't want to use third party library and happy of doing null check by yourself, then checking String's length is the fastest way and using isEmpty() method from String is most readable way. By the way, don't confuse between empty and null String, if your application treat them same, then you can consider them same otherwise they are different, as null may not be classified as empty. Here are three examples of checking String is empty or not by using JDK library itself.
Read more Here
You can't use .equals(null) to make a null check, because the API description for Object#equals states that:
For any non-null reference value x, x.equals(null) should return false.
Not only would this be a useless check (since it always returns false), it would also throw a NullPointerException if code actually was null, because a null value does not define an equals method.
Object x = null;
boolean isNull = x.equals(null); // NullPointerException on .equals
The only practical way to do a null check is to use:
if (code != null) {
}
If you want to check whether string is empty i.e. null or "" then use
if(TextUtils.isEmpty(code)){
}else{
}
equals checks the value exists in string.
public void buttonClick(View v)
{
EditText sd = (EditText)findViewById(R.id.sd);
EditText desc = (EditText)findViewById(R.id.description);
Spinner type = (Spinner)findViewById(R.id.type);
Spinner priority = (Spinner)findViewById(R.id.priority);
Set<String> set = new HashSet();
if(!validate(sd))
sd.setError("This field cannot be empty");
else if(!validate(desc))
desc.setError("This field cannot be empty");
else
{
set.add(type.getSelectedItem().toString());
set.add(priority.getSelectedItem().toString());
set.add(desc.getText().toString());
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(sd.getText().toString(),set);
editor.commit();
Log.d("MyTag",pref.getAll().toString());
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
private boolean validate(EditText t1)
{
if(t1.getText().length()==0)
return false;
else
return true;
}
Here in the above code, I am storing the details in the SharedPreferences with StringSet. The key is sd string and the values are desc, type and `priority'. I created a Set and put the key and the corresponding values in the set. How do I fetch the values for a given key?
The method pref.getStringSet(key, Set<String>) expects both key and the values. The key value is known but what should be put in the second argument? Tried null but it expects a type of Set.
The second parameter in pref.getStringSet(key, Set<String>) is a default value in case the preference has never been set before.
Check the documentation:
Parameters
key The name of the preference to retrieve.
defValues Values to return if this preference does not exist.
So if it has already been set, the previously assigned value is returned. Otherwise defValues is returned.
You can go ahead and put null there. The following compiles and works for me:
HashSet<String> hashSet = hashSet = (HashSet<String>) sp.getStringSet("names", null);
The second value is the default value. If Lint gives you an error when you put null, just tell Lint to ignore that error.
The second argument is a default value, if the shared preference you try to get doesn't exist or, you put a wrong name as first argument it will be set to the default value passed as second argument.
The key value is a label wich you will use to get that specific shared preference, for example:
editor.putString("name_of_your_preference", value_of_preference);
and then you could retrieve that string with:
pref.getString("name_of_your_preference", default_value);
you could use NULL only as default value
I am getting some data from server and setting the values in a getter and setter class. When the values seems to be null i am getting following values in my getter setter class
Tag [seatNo=null, Value=null, player1=null, player2=null, player3=null, player4=null, player5=null ]
The above value is been printed in Logcat
after getting above values i am checking an if condition because of which the app crashes
if (bet.getSeatNo() != null || !bet.getSeatNo().isEmpty() || !bet.getSeatNo().equals("null"))
{
}
how to check it is null or not ?
First you need to check if the value is not empty.
if(!TextUtils.isEmpty(bet.getSeatNo())){
//Check your condition here
}
Try doing this way:
if (bet.getSeatNo() != null)
{
if(!bet.getSeatNo().isEmpty() && !bet.getSeatNo().equals("null"))
{
}
}
Before checking the field empty/null, check whether "bet" object is null or not.
NullPointerException occurs while accessing fields of null object.
if (bet.getSeatNo() != null || !bet.getSeatNo().isEmpty() || !bet.getSeatNo().equals("null"))
If bet.getSeatNo() != null is false, only then the second !bet.getSeatNo().isEmpty() is evaluated. But since the first only evaluates to false if getSeatNo() returns null, the second conditional must NPE. Notice the problem?
Use && instead of || so that the short-circuiting stops on first false and not on the first true.
My JSON stream can be different each time. For example sometime it can include a "Song" field and sometime not.
I am getting this fields value asText ? How to tell Jackson to get this value as an Empty String if it is not defined ?
Example
"Content": "MusicContent",
"Song": "Track_1",
if try node.get("Song").asText() it will give "Track_1"
"Content": "MusicContent",
Now , if i try to get node.get("Song") it gives null pointer exception. I want to get an empty string when calling asText().
How can i do that ?
Thanks
You could check for null before calling the asText() on the node. i would probably do it like this :
if (node.get("Song") != null){
myString = node.get("Song").asText();
} else {
myString = "";
}
Or in a fancy way like this :
myString = ((node.get("Song")!=null) ? node.get("Song").asText() : "");