null

Check if object exists in JavaScript

坚强是说给别人听的谎言 提交于 2019-12-17 14:59:11
问题 How do I verify the existence of an object in JavaScript? The following works: if (!null) alert("GOT HERE"); But this throws an Error: if (!maybeObject) alert("GOT HERE"); The Error: maybeObject is not defined. 回答1: You can safely use the typeof operator on undefined variables. If it has been assigned any value, including null, typeof will return something other than undefined. typeof always returns a string. Therefore if (typeof maybeObject != "undefined") { alert("GOT THERE"); } 回答2: There

Why does null need an explicit type cast here? [duplicate]

戏子无情 提交于 2019-12-17 13:44:23
问题 This question already has answers here : Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate] (9 answers) Conditional operator cannot cast implicitly? (3 answers) Closed 6 years ago . The following code does not compile: //int a = ... int? b = (int?) (a != 0 ? a : null); In order to compile, it needs to be changed to int? b = (a != 0 ? a : (int?) null); Since both b = null and b = a are legal, this doesn't make sense to me. Why do we have to cast the null into

Android Intent.getStringExtra() returns null

北城余情 提交于 2019-12-17 11:17:30
问题 This is how strings are being added to Extras: Intent i = new Intent(); i.putExtra("Name", edt_name.getText()); i.putExtra("Description", edt_desc.getText()); i.putExtra("Priority", skb_prior.getProgress()); setResult(RESULT_OK, i); finish(); This is how I try to extract them in onActivityResult() : String name = data.getStringExtra("Name"); String desc = data.getStringExtra("Description"); int prior = data.getIntExtra("Priority", 50); But after the second code block name and desc are null 's

Mark parameters as NOT nullable in C#/.NET?

血红的双手。 提交于 2019-12-17 10:46:08
问题 Is there a simple attribute or data contract that I can assign to a function parameter that prevents null from being passed in C#/.NET? Ideally this would also check at compile time to make sure the literal null isn't being used anywhere for it and at run-time throw ArgumentNullException . Currently I write something like ... if (null == arg) throw new ArgumentNullException("arg"); ... for every argument that I expect to not be null . On the same note, is there an opposite to Nullable<>

What is the difference between null and empty?

大城市里の小女人 提交于 2019-12-17 10:31:15
问题 I am new to the concept of empty and null. Whilst I have endeavoured to understand the difference between them, I am more confused. I came across an article at http://www.tutorialarena.com/blog/php-isset-vs-empty.php however I still don't see when you would use isset and empty when validating forms. Seeing that I don't grasp the difference, I don't want to be using the incorrect functions as well as not be able to use the functions in other areas. Can someone give examples that will help me

Redefining NULL

半世苍凉 提交于 2019-12-17 10:15:19
问题 I'm writing C code for a system where address 0x0000 is valid and contains port I/O. Therefore, any possible bugs that access a NULL pointer will remain undetected and at the same time cause dangerous behaviour. For this reason I wish to redefine NULL to be another address, to for example an address that isn't valid. If I accidentally access such an address I will get a hardware interrupt where I can handle the error. I happen to have access to stddef.h for this compiler, so I can actually

How to tell if a string is not defined in a Bash shell script

♀尐吖头ヾ 提交于 2019-12-17 10:07:46
问题 If I want to check for the null string I would do [ -z $mystr ] but what if I want to check whether the variable has been defined at all? Or is there no distinction in Bash scripting? 回答1: I think the answer you are after is implied (if not stated) by Vinko's answer, though it is not spelled out simply. To distinguish whether VAR is set but empty or not set, you can use: if [ -z "${VAR+xxx}" ]; then echo VAR is not set at all; fi if [ -z "$VAR" ] && [ "${VAR+xxx}" = "xxx" ]; then echo VAR is

What reason is there to use null instead of undefined in JavaScript?

旧城冷巷雨未停 提交于 2019-12-17 08:17:14
问题 I've been writing JavaScript for quite a long time now, and I have never had a reason to use null . It seems that undefined is always preferable and serves the same purpose programmatically. What are some practical reasons to use null instead of undefined ? 回答1: Null and undefined are essentially two different values that mean the same thing. The only difference is in the conventions of how you use them in your system. As some have mentioned, some people use null for meaning "no object" where

Why and how are these two $null values different?

蓝咒 提交于 2019-12-17 07:48:19
问题 Apparently, in PowerShell (ver. 3) not all $null 's are the same: >function emptyArray() { @() } >$l_t = @() ; $l_t.Count 0 >$l_t1 = @(); $l_t1 -eq $null; $l_t1.count; $l_t1.gettype() 0 IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array >$l_t += $l_t1; $l_t.Count 0 >$l_t += emptyArray; $l_t.Count 0 >$l_t2 = emptyArray; $l_t2 -eq $null; $l_t2.Count; $l_t2.gettype() True 0 You cannot call a method on a null-valued expression. At line:1 char:38 + $l

getActionView() of my MenuItem return null

萝らか妹 提交于 2019-12-17 07:42:21
问题 I just would like to tweak the View of an ActionBar MenuItem by code. Unfortunately, it seems that getActionView always return null! My code: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = this.getSupportMenuInflater(); inflater.inflate(R.menu.folder, menu); return super.onCreateOptionsMenu(menu); } public boolean onPrepareOptionsMenu(final Menu menu) { MenuItem menuFolder = menu.findItem(R.id.menu_folder); Log.i("", "* onPrepareOptionsMenu *" + menuFolder);