Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

13 December 2011

Why findViewById(R.id.cozyUIElement) is null,even if i am DAMN SURE that i defined in xml file for UI ???

Case:
so you create your awesome UI in xml like and then in activity file you code that should change your progress bar and your program crashed and IDE said that

Error message looks like:
findViewById(R.id.cozyUIElement) is null

What went wrong ?
You need set content view before you can match them with your  class from view

Right way to do it is:

setContentView(R.layout.cozy);   <--- THIS MUST BE FIRST
cozyProgressBar = (ProgressBar) findViewById(R.id.cozyUIElement  ); <--THIS method need appear after setContentView(...)!

So check your onCreate() method and look are you using any  findViewById() method before  setContentView(R.layout.cozy)  if you found any,then you found your problem :)

Example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<ProgressBar
  android:id="@+id/cozyUIElement"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:max="100"
android:visibility="visible" />

</LinearLayout>


and then in your activity file

package ,imports and other spam
(...)
public class CozyActivity extends Activity{
    ProgressBar cozyProgressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.cozy);  
cozyProgressBar = (ProgressBar) findViewById(R.id.cozyUIElement  );
   cozyProgressBar.setProgress(20);
 }


29 September 2011

How to check is if an object property in JavaScript is undefined?

 If you not familiar with difference  between null (something is defined but doesn't have assign any valueno value) and undefined (  something was not defined) then look to my post below:
What is a difference between null and undefined

During learning you discover how amazing thing can be done with if statement... and how useful it is.
Then during your practice your skill on examples, you will discover is good to check if something doesn't have a value ... so you just write:


if (typeof example === null) {
  alert("booo!example doesn't have any value!");
} 

Job done ...  but you want check is something exists...  you heard about undefined... but as you know undefined is not a reserved word ,so you can't do the same away as above... what to do ? You actually can do it in similar way  you need to "compare" using string "undefined".
Let's see  a example for check object property exist.
 
if (typeof example === "undefined") {
  alert("example is undefined");
} 

It will be very useful for you. in  your deep relationship with javascript.

11 March 2011

What is differenece between null and undefined ?

When you start learning how to develop software , then you learned that null means lack of  value (and associated annoying error related to null called NullPointerException which cause people to madness and frenzy).

When you start learning javascript you discover that javascript is like other languages with some  freaks,so  i usually said that javascript is a language created by Monty Python.

Anyway.. back to topic, so ....
null means lack of value  and when you see null it can indicate that something does not contain a valid string, number,  boolean , array or ... object.

However Javascript loves conversion (that cause lots of bugs and problems when you develop your application  and this will be main reason why you will hate this language in many cases but loves by some geeks in some rare cases).
Why ?
Javascript will convert :
In a string context to null
In  a number context to 0
In boolean context to false

Keep this in mind , specially when you will do lots of comparisons.
For example compare null to undefined ,why ??

Let's explain what undefined means:

Undefined appear when
  • Something wasn't declared and you try to call this.  (like Object)
  •  Variable  has been declared but never had a value assigned
  •   property of Object which you refer to doesn't exist. 
Conversion .....in undefined will be even more crazy that in null.
In string context to null
In number context you will see NaN (which means Not A Number) (why ? this is another story which will explain in one of future posts)
In boolean context to false

Few interesting things on end of this post:

 Remeber:
  •  null is related as lack of  variable of something that exist 
  •  undefined means that something doesn't exist.

 In book : Javascript: The deinitive Guide i found that " Unlike null, undefined is not a reserved word in JavaScript."


And last .... people who are very sensitive in speaking/writing correctly always get mad because they said that "do not say/write  null value .. say/write null ... why ? because null means lack of value)

So if you have no f... idea why result of your comparison is weird ambiguous unexpected or not logical .. look to this post again. It can save your nervous system!