Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Pushing a text string onto an array causes error..

I am running Android 2.2 with Titanium mobile 1.51.

I have an application scoped array in my app.js file "textArray". As part of the creation of the CreateWindow (main window for the application) I pass the array as a property to the CreateWindow function.

        var textArray = new Array();

    var RootWindow = Titanium.UI.createWindow({  
        title:'Root Window',
        url:urlToLoad,
                textArrayIn:textArray
    });

Internally following code logic that wishes to add an entry to the array from one of the views used in the application, I use the property of the window and assign by reference to variable. Upon running the "push" method on the array, I get the following error:

12-28 16:43:33.029: ERROR/KrollCallback(24355): (kroll$4) [713,305148] ECMA Error evaluating source, invocation: [callMethod Network.HTTPClient.(anonymous) org.appcelerator.titanium.kroll.KrollCallback@43f72b08], message: TypeError: Cannot find default value for object. (app://menus/display_item.js#60)
12-28 16:43:33.029: ERROR/KrollCallback(24355): org.mozilla.javascript.EcmaError: TypeError: Cannot find default value for object. (app://menus/display_item.js#60)
12-28 16:43:33.029: ERROR/KrollCallback(24355):     at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3761)
12-28 16:43:33.029: ERROR/KrollCallback(24355):     at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3739)
12-28 16:43:33.029: ERROR/KrollCallback(24355):     at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3767)
12-28 16:43:33.029: ERROR/KrollCallback(24355):     at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3779)
12-28 16:43:33.029: ERROR/KrollCallback(24355):     at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:839)
12-28 16:43:33.029: ERROR/KrollCallback(24355):     at org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:758)

The code that breaks is as follows:

In a separate function that displays some ui controls, input text is attempted to be stored into the array as follows:

var win = Titanium.UI.currentWindow;
var textArrayIn = win.textArrayIn;

textArrayIn.push(newText);

I have traced adding an array element at the application level and output the array length (1) prior to the "push" call and also traced the value in the textArrayIn[0] position. Both worked as expected, so Array looks fine to access (i.e. apparently no memory issues)

I also tested pushing pure text 'test' etc. onto the textArrayIn array which triggered the same error.

When I create a separate local array as follows the push works fine, so it appears to be some scoping issue:

var key = 'test';
var newArray = new Array();
newArray.push(key);

Any help on this would be really appreciated.

J.

— asked December 28th 2010 by john gorman
  • array
  • default
  • initialization
  • javascript
  • object
  • push
  • scope
  • typeerror
  • variable
1 Comment
  • you should also include the platform android/iOS

    — commented December 28th 2010 by Aaron Saunders

2 Answers

  • Hi,

    Wold you can describe sdk version?

    i create this code and works correctly:

    app.js:

    
    var textArray = [];
    textArray.push('a');
    textArray.push('b');
    textArray.push('c');
    
    var RootWindow = Titanium.UI.createWindow({  
        title:'Root Window',
            url:'a.js',
        textArrayIn:textArray
    });
    
    RootWindow.open();
    

    a.js:

    
    var win = Titanium.UI.currentWindow;
    var textArrayIn = win.textArrayIn;
    
    alert(textArrayIn);
    
    textArrayIn.push('newText');
    
    alert(textArrayIn);
    
    — answered December 28th 2010 by Adriano Paladini
    permalink
    2 Comments
    • I am using Titanium 1.51 and setup to use SDK 2.2

      — commented December 28th 2010 by john gorman
    • Actually to clarify, I am creating a tabgroup, then adding a tab to the group. The tab uses the window as in the example I gave as follows:

              var tabRootWindow = Titanium.UI.createWindow({  
              title:'Window Title',
              url:urlToLoad,
              textArrayIn:textArray
          });
      
          var tabObj = Titanium.UI.createTab({  
              icon:iconForTab,
              title:'Tab Title',
              window:tabRootWindow
          });
      

      The .push() method is actually called form a function() within the calling .js file (a.js in your example) - i.e. it is using it as a global here (not the nicest, but it's a port I am working on). I also tried passing the references to the method also but same error. Thanks for the help.

      — commented December 28th 2010 by john gorman
  • Hi, I'm having a similar problem in Android (Galaxy Tab, 2.2) when parsing xml to json.

    The code below works fine in iOS but fails in Android with error: TypeError: Cannot find function push in object .(app://getAccounts.js#111)

    Line #111 is: obj[nodeName].push(xmlToJson(item));
    (6th line bottom-up below)

    Full function below, any clues much appreciated!

    function xmlToJson(xml) {
    
      // Create the return object
      var obj = {};
    
      if (xml.nodeType == 1) { // element
        // do attributes
        if (xml.attributes.length > 0) {
        obj["@attributes"] = {};
          for (var j = 0; j < xml.attributes.length; j++) {
            var attribute = xml.attributes.item(j);
            obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
          }
        }
      } else if (xml.nodeType == 3) { // text
        obj = xml.nodeValue;
      }
    
      // do children
      if (xml.hasChildNodes()) {
        for(var i = 0; i < xml.childNodes.length; i++) {
          var item = xml.childNodes.item(i);
          var nodeName = removeNS(item.nodeName);
          if (typeof(obj[nodeName]) == "undefined") {
            obj[nodeName] = xmlToJson(item);
          } else {
            if (typeof(obj[nodeName].length) == "undefined") {
              var old = obj[nodeName];
              obj[nodeName] = [];
              obj[nodeName].push(old);
            }
            obj[nodeName].push(xmlToJson(item));
          }
        }
      }
      return obj;
    };
    
    — answered August 10th 2011 by Joe Da Silva
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.