views:

542

answers:

3

I have application like where i can create dynamic tabs. and delete cross bar option on tabs. When I am trying to delete the tab I am getting error like

Microsoft JScript runtime error: 'null' is null or not an object and point to my Javascript code.

Here is my JS code.

  <script type="text/javascript">
        /* <![CDATA[ */
        function deleteTab(tabText)
        {
            var tabStrip = $find("<%= RadTabStrip1.ClientID %>");
            var multiPage = $find("<%= RadMultiPage1.ClientID %>");
            var tab = tabStrip.findTabByText(tabText);
            var pageView = tab.get_pageView();

            var tabToSelect = tab.get_nextTab();
            if (!tabToSelect)
                tabToSelect = tab.get_previousTab();

            tabStrip.get_tabs().remove(tab);
            multiPage.get_pageViews().remove(pageView);

            if (tabToSelect)
                tabToSelect.set_selected(true);
        }
        /* ]]> */
</script>

and in page lode

    if (!Page.IsPostBack)
    {
        RadTab tab = new RadTab();
        tab.Text = string.Format("New Page {0}", 1);
        RadTabStrip1.Tabs.Add(tab);

        RadPageView pageView = new RadPageView();
        pageView.Height = new Unit("50px");
        pageView.Width = new Unit("1300px");
        RadMultiPage1.PageViews.Add(pageView);

        BuildPageViewContents(pageView, RadTabStrip1.Tabs.Count);
        RadTabStrip1.SelectedIndex = 0;

    }
+2  A: 

You're not checking any of those function calls to see if they're actually returning something. One of them is returning null, but your code does not notice that and tries to use the result in a subsequent statement.

Try this in Firefox with Firebug and you'll probably get better error messages.

Pointy
I checked it. In firefox its working. But not in IE. what should i do now to work in IE?
Well, put in some `if` statements to check each return value, and if one comes back `null` put up an alert. Of course the real problem will be the code that's not finding an element (or whatever), but you won't know where to look until you find the method call that's going wrong.
Pointy
+1  A: 

This error can occur if you are trying to use an object which is null. In that code quite a lot of things can return null: $find, findTabByText, getPageView, get_nextTab, get_previousTab etc. I suggest you alert() everything before using it. That way you will find what is null.

korchev
A: 

$find can return null if you are trying to call it too early. Remember that ASP.NET AJAX controls are created during the Sys.Application.Init event. If you try to access them earlier (e.g. in the window.onload) the $find() will not work.

lingvomir
I ma using telerik controls. When out alert and see i am getting message like tab is null
so which one should i use now? How to delete the tab?
You should use something like the ASP.NET AJAX pageLoad() client method if you want to run scripts when the page is initially loaded. You can also use the specific control's OnClientLoad property to handle its own load event (if there is one).
lingvomir