Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-3464] Android: Throws NullPointerException when tag not found

GitHub Issuen/a
TypeBug
PriorityTrivial
StatusClosed
ResolutionFixed
Resolution Date2011-11-05T13:40:46.000+0000
Affected Version/sRelease 1.6.2
Fix Version/sSprint 2011-30, Release 1.8.0
ComponentsAndroid
Labelsn/a
ReporterJon Alter
AssigneeReggie Seagraves
Created2011-04-15T03:45:30.000+0000
Updated2011-11-05T13:40:46.000+0000

Description

In Android, when you getElementsByTagName() and element that doesn't exist, It throws a NPE.
note: iOS does not throw an exception

Step 0: Notice 'bas' is not in the xmlString in the example
Step 1: Paste example code below into appi.js
Step 2: Run example
Step 3: Click 'Find Bas'
Step 4: Notice the Null Pointer Exception

var xmlString = '<items><foo>FOO</foo><bar>BAR</bar></items>';

var window = Ti.UI.createWindow({
    backgroundColor: 'green'
});

var button = Ti.UI.createButton({
    title: 'Find Foo',
    height: 60,
    width: 100,
  top: 40
});
button.addEventListener('click', function(e){
    var xml = Titanium.XML.parseString(xmlString);
  var element = xml.getElementsByTagName("foo");
  Ti.API.info( element.item(0).text );
});

var button2 = Ti.UI.createButton({
    title: 'Find Bas',
    height: 60,
    width: 100,
  top: 140
});
button2.addEventListener('click', function(e){
    var xml = Titanium.XML.parseString(xmlString);
  var element = xml.getElementsByTagName("bas");
  Ti.API.info( element.item(0).text );
});

window.add(button);
window.add(button2);
window.open();

Tested On

TiSDK 1.6.1, Android SDK 2.1

Associated Helpdesk Ticket

http://support-admin.appcelerator.com/display/WEJ-65612-589">http://support-admin.appcelerator.com/display/WEJ-65612-589

Attachments

FileDateSize
Screen Shot 2011-08-24 at 11.13.40 AM (2).png2011-08-24T11:14:48.000+00002983794

Comments

  1. Bill Dawson 2011-07-26

    The failcase above will cause an exception even after the fix, since you can't read .text from null ( .item(0) will return null since there are no matching items.) Simpler failcase app.js:
       var xmlString = "<benny/>";
       var doc = Ti.XML.parseString(xmlString);
       var elem = doc.getElementsByTagName("mickey").item(0);
       //Titanium.Network;
       
  2. Bill Dawson 2011-07-26

    Pull request ready: https://github.com/appcelerator/titanium_mobile/pull/283
  3. Vikramjeet Singh 2011-08-24

    Test fails on iOS while using simpler test case given by Bill Dawson in above comment. Error is pasted below and also screenshot of the error is attached. Tested On: Mac OSX Lion TiMob: 1.8.0.v20110819142548 TiStud: 1.0.4.201108101535 Devices used: iPhone4 4.2.10 iPad2 4.3.5 iPhone Simulator Following is the error: [DEBUG] loading: /Users/vsingh/Library/Application Support/iPhone Simulator/4.3.2/Applications/5C91A3FF-5977-4666-95E8-89A2BA7B2625/QTS.app/atestList.js, resource: atestList_js [DEBUG] loading: /Users/vsingh/Library/Application Support/iPhone Simulator/4.3.2/Applications/5C91A3FF-5977-4666-95E8-89A2BA7B2625/QTS.app/tests/ios/2968.js, resource: tests/ios/2968_js [DEBUG] loading: /Users/vsingh/Library/Application Support/iPhone Simulator/4.3.2/Applications/5C91A3FF-5977-4666-95E8-89A2BA7B2625/QTS.app/tests/xplat/3464.js, resource: tests/xplat/3464_js [ERROR] Script Error = Result of expression 'doc.getElementsByTagName("mickey")' [null] is not an object. at 3464.js (line 1).
  4. Vikramjeet Singh 2011-08-24

    attached screenshot with error
  5. Natalie Huynh 2011-08-25

    Tested with 1.8.0.v20110819142548 Motorola Xoom 3.2 Nexus One 2.2.2
  6. Don Thorp 2011-11-05

    Fixing labels.

JSON Source