null

Should I throw a NullPointerException explicitly or let Java do it for me?

让人想犯罪 __ 提交于 2020-01-12 14:00:34
问题 As the title says, I am wondering what the best practice is regarding the throwing of NullPointerExceptions. Specifically, if I have an external library function that can return null in circumstances that I don't want to actually handle (see below for a specific example), as the null indicates a problem with the software. The question is, should I check the return value for null and throw the NullPointerException myself, or should I just let Java do the dirty work for me as soon as I try to

Should I throw a NullPointerException explicitly or let Java do it for me?

此生再无相见时 提交于 2020-01-12 14:00:33
问题 As the title says, I am wondering what the best practice is regarding the throwing of NullPointerExceptions. Specifically, if I have an external library function that can return null in circumstances that I don't want to actually handle (see below for a specific example), as the null indicates a problem with the software. The question is, should I check the return value for null and throw the NullPointerException myself, or should I just let Java do the dirty work for me as soon as I try to

Which Exception to throw when a method try to use a field that can be null? [duplicate]

拟墨画扇 提交于 2020-01-12 11:43:07
问题 This question already has answers here : Is there a built in .NET exception that indicates an illegal object state? (3 answers) Closed 2 years ago . I am actually working on a Framework development, which means require a really strong coding methodology. I am facing a problem where I do not know which System.Exception derivated class I need to throw. Basically the case is when I have a class with fields that can be optionnaly initialized by the constructor and that have methods using these

How to check if params[:some][:field] is nil?

ε祈祈猫儿з 提交于 2020-01-12 05:09:09
问题 I tried code, that plused a lot of people - How to test if parameters exist in rails, but it didn't work(): if ( params.has_key?([:start_date]) && params.has_key?([:end_date]) ) I think, that is because of complicated params and if I write this: if ( params.has_key?([:report][:start_date]) && params.has_key?([:report][:end_date]) ) gives me error can't convert Symbol into Integer this doesn't work too: if ( params[:report][:start_date] && params[:report][:end_date] ) gives me error: undefined

testing inequality with columns that can be null

泄露秘密 提交于 2020-01-12 04:45:11
问题 So, I asked a question this morning, which I did not phrase correctly, so I got a lot of responses as to why NULL compared to anything will give NULL/FALSE. My actual question was, what is the time honored fashion in which db guys test inequalities for two columns that can both be NULL. My question is the exact opposite of this question. The requirements are as follows, A and B are two columns: a) if A and B are both NULL, they are equal, return FALSE b) if A and B are both not NULL, then

Collections.emptyList() instead of null check?

本小妞迷上赌 提交于 2020-01-11 21:55:12
问题 If I have a rarely used collection in some class which may be instantiated many times, I may sometimes resort to the following "idiom" in order to save unnecessary object creations: List<Object> list = null; void add(Object object) { if (list == null) list = new ArrayList<Object>(); list.add(object); } // somewhere else if (list != null) for (Object object : list) ; Now I was wondering if I couldn't eliminate those null checks using Collections.emptyList() , however then I would have to alter

Unable to convert MySQL date/time value to System.DateTime.Couldn't store 0/0/0000 0:00:00 value?

时光毁灭记忆、已成空白 提交于 2020-01-11 13:28:27
问题 Me.TreatmentsTableAdapter.Fill(Me.UserDataSet1.treatments) This line is producing error: Unable to convert MySQL date/time value to System.DateTime. Couldn't store <0/0/0000 0:00:00> in TreatmentDateEdited Column. Expected type is DateTime. TreatmentDateEdited column is DateTime column that unfortunately contains some NULL values. I know that there is a problem in Dataset to convert NULL date to anything else, thus producing me this error. in connection string I have already added:

How to terminate a character pointer at a certain location in c?

こ雲淡風輕ζ 提交于 2020-01-11 12:25:07
问题 I'm trying to terminate a character pointer in c, at a specific location by setting the null terminator to it. for examples if I have a char pointer char *hi="hello"; I want it to be "hell" by setting the o to null. I have tried doing this with strcpy with something like strcpy(hi+4, "\0"); But it is not working. 回答1: "hello" is a string literal so it cannot modified, and in your code, hi points to the first element in such a literal. Any attempt to modify the thing it points to is undefined

How to test whether the value of a Java field gotten by reflection is null?

半城伤御伤魂 提交于 2020-01-11 12:21:32
问题 I have Field f = this.getClass().getFields()[0]; I need to know if f 's value in this is null or not. There are many methods like getInt() and getDouble() , but I have not found a method like Object getData() or isNull() . Is there such a method? 回答1: field.get(target) returns Object . So you can check if (field.get(this) == null) {..} If the field is primitive, it will get wrapped. int -> Integer , char -> Character , etc. 回答2: You need to get the field from the object then check if it is

PostgreSQL COPY CSV with two NULL strings

谁说胖子不能爱 提交于 2020-01-11 09:37:05
问题 I have a source of csv files from a web query which contains two variations of a string that I would like to class as NULL when copying to a PostgreSQL table. e.g. COPY my_table FROM STDIN WITH CSV DELIMITER AS ',' NULL AS ('N/A', 'Not applicable'); I know this query will throw an error so I'm looking for a way to specify two separate NULL strings in a COPY CSV query? 回答1: I think your best bet in this case, since COPY does not support multiple NULL strings, is to set the NULL string argument