Will I receive an error (Exception) on some devices if I set the second parameter of SharedPreferences.getString NULL?
SharedPreferences settings = ...
String data = settings.getString(_.PREFIX , null);
Will it cause an exception or an error on at least one device? Or I have to wrap this part of code in try-catch block?
If you are asking if you will get an exception if you set the second parameter to null, the answer is no (at least not unless you reference the result without first checking it is not null). The second parameter in the getString() method is the default value (i.e. the value that will be returned if there is nothing found for your prefix. So, it is perfectly acceptable to set null as your default value, as long as you realize (and account for) the fact that the value returned by your getString() could be null.
String data = settings.getString(_.PREFIX , null/Null here is default value/);
null - u can receive when your SraredPreferences have not this item(For example if u call/get this string before setting to this field any info or user clear cash of application from settings of device). I think it's can be normal situation, and u can remove "null" with some default value if you hope to got it(some emum field).
If u don't suppose get null validate data before using.
I thin'k your app must be ready get both variant, because user can change normal workflow.
Related
I have a situation where i have couple of fields i have to pass while calling the cosmos DB , but those fields may not always have values. Some of them might be null while passing them to the repository method. I am trying to do it like below
public interface CachedRepository{
#Query(value="select * from abc a where (#base=null or a.base=#base) and (#position=null or a.position=#position) and (#active=null or a.active = #active)")
List<BackList> getBackListOptions(#Param("base")String base,#Param("position")String position,#Param("active") String active);
The implementation class
latestDetails = repositoryA.getBackListOptions(p.getBase(),p.getPosition(),p.getActive().get(0));//active is a List and we are passing one value
I am trying to pass the request without the active parameter(i.e. active is null in the request) The exception i am getting is
Can not invoke "java.util.List.get(int)" because the return value of request.Pick.getActive() is null
The cosmos Table is
{
'_id":"a25777-j"
"empId": 2436,
"base":"JH",
"position":"HG",
"active":"J"
.........
}
And i am taking reference from this answer
How to write dynamic sql in Spring Data Azure Cosmos DB
Please let me know where i am doing it wrong
The code you have given should work fine if either null or an actual value is passed for any of the parameters. But in your case, for active, it is neither. Nothing is passed, not even null.
It seems from the comments in your code that "active" is an object of type List, and you are attempting to pass a value in that list to getBackListOptions, and extracting that value directly in the method call with p.getActive().get(0). But I think your implementation code is failing where p.getActive().get(0) is called, before getBackListOptions is even invoked. The error you are getting is not something that is returned from Cosmos Spring client, which I believe is doing what it should. You need to handle the values you are passing properly before passing them. I think you could handle this something like below.
String base = p.getBase(); //set the value here so it is clear where failure is
String position = p.getPosition(); //set the value here so it is clear where failure is
String active = p.getActive() == null ? null : p.getActive().get(0); //I think this line was failing before because p.getActive() was null, but this condition handles that case
latestDetails = repositoryA.getBackListOptions(base,position,active);//active is a List and we are passing one value
I'm trying to find an object using findViewById with a string using this line
Slots[i][j] = requireActivity().findViewById(getResources().getIdentifier(builder.toString(), "id", requireActivity().getPackageName()));
but when it runs, it always returns null. When I print the id that it uses it prints something like 2131231288 when the real id value is 1000072. I've checked to make sure that the string is correct and is the same as the id. Anyone know why this is happening?
Please have a look at the below code.
String textName=request.getParameter("textName");
String textadr1=request.getParameter("textadr1");
String textadr2=request.getParameter("textadr2");
String textcntry=request.getParameter("textcntry");
String textregNo=request.getParameter("textregNo");
AgentContact agentContact=new AgentContact();
AgentContact agentContact2=new AgentContact();
Agent agent=new Agent();
agent.setName(textName);
agent.setRegistrationNumber(textregNo);
agent.setDateOfDealStart(textDateDealing);
agent.setRegistrationDate(textregDate);
session.save(agent);
session.save(agentContact);
session.save(agentContact2);
session.getTransaction().commit();
In the above code, we get the values from a form, that is why request.getParameter() is used. However, user may or may not fill some fields like name, so the txtName will be empty.
However when this code is executed, I noticed Hibernate added "empty" string to these non specified Strings, instead of entering null. How can I set the code so it enters null values when the text are actually empty?
This is not a Hiberate issue.
See here:
Are empty fields in form of JSP null or ""?
You simply need to check for an empty String and act accordingly:
agent.setName(textName.equals("") ? null : textName);
The above is a somewhat simplistic solution however: what if the user enters a space? You can use methods of Apache Commons StringUtils for more robust checking:
e.g.
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isBlank(java.lang.CharSequence)
agent.setName(StringUtils.isBlank(textName) ? null : textName);
I am not getting to how to know to which layout it is point because instead of giving the full name like r.layout.activity_main it has given the id in the form of integer.
How to get the location if the integer id is given how to convert it?
use
getResources().getResourceName(int resid);
read more in that thread
To get the full id like R.id.your_id you have to make two calls:
getActivity().getResources().getResourceEntryName(int);
Which returns the identifier your_id
getActivity().getResources().getResourceTypeName(int);
Which returns the type of the resource like id, string, layout
As a note: Make sure the id with the corresponding value exists! Otherwise you will get a NotFoundException.
I want to check if an insert fails (due to unique=True index in the collection). If there is an error do something. Bellow is an example of my code.
DBCollection user...;
BasicDBObject Doc = new BasicDBObject(... );
String user_exists = user.insert(Doc).getError(); //insert the doc get error if any
if(user_exists!=null){ //any errors?
user.update(new BasicDBObject(...)); // error exists so do smthng
}
The above as it is does not work. I believe that the String user_exists is always null. How can I make the above work?
I have seen similar SO questions and mention the WriteConcern which can be passed in the insert(). E.g.
coll.insert(dbObj, WriteConcern.SAFE);
sources: SO question
or
Mongo docs
However I do not know which one field should I pass (SAFE, ACKNOWLEDGED, UNACKNOWLEDGED etc..) in order to get the error. Maybe I'm pointed in the wrong direction.
I do not wish to raise an exception just to check if there is an error returned by the insert operation.
If you are using WriteConcern.ACKNOWLEDGED (which I think is also SAFE) you don't need to pollute your code with error checking.
For ACKNOWLEDGED, the driver will automatically issue a getLastError command automatically and raise an exception if anything got wrong, for example duplicate index violation.
Starting from v2.10 of the Java Driver, the default Write Concern is ACKNOWLEDGED
EDIT
I do not wish to raise an exception just to check if there is an error returned by the insert operation.
You shouldn't do this, but in any case:
The insert method indeed returns WriteResult. If it's getError() is null, everything is OK, otherwise it returns something such as E11000 duplicate key error index:.... For this to work, you will have to use WriteConcern.UNACKNOWLEDGED