[TIMOB-3464] Android: Throws NullPointerException when tag not found
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Trivial |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2011-11-05T13:40:46.000+0000 |
Affected Version/s | Release 1.6.2 |
Fix Version/s | Sprint 2011-30, Release 1.8.0 |
Components | Android |
Labels | n/a |
Reporter | Jon Alter |
Assignee | Reggie Seagraves |
Created | 2011-04-15T03:45:30.000+0000 |
Updated | 2011-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
File | Date | Size |
---|---|---|
Screen Shot 2011-08-24 at 11.13.40 AM (2).png | 2011-08-24T11:14:48.000+0000 | 2983794 |
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:Pull request ready: https://github.com/appcelerator/titanium_mobile/pull/283
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).
attached screenshot with error
Tested with 1.8.0.v20110819142548 Motorola Xoom 3.2 Nexus One 2.2.2
Fixing labels.