Q:

Null Pointer exception when trying to access the parent layout

I have a relative layout with three buttons in it and I am trying to change the background of the relative layout dynamically. Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/mbi2"
            android:id="@+id/rlMain"
            android:layout_centerHorizontal="true">
    <Button android:text="Compose" 
            android:id="@+id/btnCompose" 
            android:layout_height="wrap_content"  
            android:layout_width="wrap_content"/>
    <Button android:text="Messages" 
            android:id="@+id/btnMessages" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnCompose"/>
    <Button android:text="More &gt;&gt;" 
            android:id="@+id/btnMore" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content" 
            android:layout_toRightOf="@id/btnMessages"/>                    
            </RelativeLayout>

Here's the java code which is giving me an error
            RelativeLayout llMain=null;
    try {
        llMain = (RelativeLayout) findViewById(R.id.rlMain);
        Resources res = getResources();
        Drawable drawMbi = res.getDrawable(drawBgId);
        llMain.setBackgroundDrawable(drawMbi);          
    } catch (Exception e) {
        e.printStackTrace();
    }

The error is
06-29 23:56:41.099: WARN/System.err(8487): java.lang.NullPointerException
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.loadMBIIntoView(ChickenWrapActivity.java:65)
06-29 23:56:41.099: WARN/System.err(8487):     at com.me2youmob.cwrap.ChickenWrapActivity.onCreate(ChickenWrapActivity.java:23)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
06-29 23:56:41.099: WARN/System.err(8487):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
06-29 23:56:41.108: WARN/System.err(8487):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:56:41.118: WARN/System.err(8487):     at android.os.Looper.loop(Looper.java:143)
06-29 23:56:41.118: WARN/System.err(8487):     at android.app.ActivityThread.main(ActivityThread.java:5068)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:56:41.118: WARN/System.err(8487):     at java.lang.reflect.Method.invoke(Method.java:521)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-29 23:56:41.118: WARN/System.err(8487):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-29 23:56:41.118: WARN/System.err(8487):     at dalvik.system.NativeStart.main(Native Method)

Another thing in this issue is that earlier I had the buttons within a linear layout which was then within a Relative Layout. I thought it was not able to reach the relative layout and remove the linear layout section. It still gives me the same error. 
Any idea what is going on here ?
NEW UPDATE:
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout rlM = (RelativeLayout) findViewById(R.id.rlMain);
    if (rlM == null)
    {
        Log.d("NULL CHECK","Layout is null");
    }
    else
    {
        Log.d("NULL CHECK","Layout is not null");
    }
    //spPreferences = getSharedPreferences(PREFS_NAME, 0);
    //loadMBIIntoView();
    //handleButtonClicks();
}

A:

The View.findViewById() function will only find views that are children of the current view you are in.
In this case, you're trying to obtain the main view either within one of the children views, or you have not used setContentView() within the Activity class yet.
If you are trying to set your background, I would highly suggest doing so from the Activity.

