PDA

View Full Version : Attempt 08, Coding.



Hunter
April 11th, 2010, 10:15 AM
Hmm, lets see if anyone can help me this time...

Error:


Error ``user_node[0].firstChild is null'' [x-] in file ``*url removed*'', line 63, character 0.


As you can see it says that the value is NULL although it shouldn't be... Here is the JavaScript which the error refers to:


function manageMessage() {
if( receiveReq.readyState == 4 ) { // request complete
var chat_div = document.getElementById( 'chat' );
var xmldoc = receiveReq.responseXML;
var msg_nodes = xmldoc.getElementsByTagName( 'message' );
var n_msg = msg_nodes.length;
for( i = 0; i < n_msg; i++ ) {
alert(i + "," + n_msg);
var user_node = msg_nodes[ i ].getElementsByTagName( 'user' );
var time_node = msg_nodes[ i ].getElementsByTagName( 'time' );
var text_node = msg_nodes[ i ].getElementsByTagName( 'text' );
alert(user_node[0].firstChild.nodeValue);

//Display message
chat_div.innerHTML += user_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />";
}
}
}


And again, the whole script if you want it...



<script type="text/javascript">
//Credits//
//Help: Greg (Gjslick)
//Code example: http://www.dynamicajax.com/fr/AJAX_Driven_Web_Chat_Backend-271_290_291_296.html

// Declare Global Variables
var lastMess;
var sendReq;
var receiveReq;
var mTimer;

// Initialize Global Variables (on page load)
function init() {
lastMess = 0;
sendReq = getXmlHttpRequestObject();
receiveReq = getXmlHttpRequestObject();
mTimer;
}


function getXmlHttpRequestObject() {
if( window.XMLHttpRequest ) {
return new XMLHttpRequest(); // code decent browsers like Firefox, Chrome, Opera, Safari. And IE7+...

} else if( window.ActiveXObject ) {
return new ActiveXObject( 'Microsoft.XMLHTTP' ); //Code for crap like IE

} else {
var statusEl = document.getElementById( 'p_status' );
statusEl.innerHTML = "Status: Could not create XmlHttpRequest Object, please upgrade your browser!";

return null;
}
}

function getMessage() {
//var url = "scripts/get_data.php?uid=<?=$uid?>&m=" + lastMess + "&chat=1";
var url = "testXMLdoc.xml";
receiveReq.open("GET",url,true);
receiveReq.onreadystatechange = manageMessage;
receiveReq.send( null );
lastMess++;
}

function manageMessage() {
if( receiveReq.readyState == 4 ) { // request complete
var chat_div = document.getElementById( 'chat' );
var xmldoc = receiveReq.responseXML;
var msg_nodes = xmldoc.getElementsByTagName( 'message' );
var n_msg = msg_nodes.length;
for( i = 0; i < n_msg; i++ ) {
alert(i + "," + n_msg);
var user_node = msg_nodes[ i ].getElementsByTagName( 'user' );
var time_node = msg_nodes[ i ].getElementsByTagName( 'time' );
var text_node = msg_nodes[ i ].getElementsByTagName( 'text' );
alert(user_node[0].firstChild.nodeValue);

//Display message
chat_div.innerHTML += user_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += time_node[ 0 ].firstChild.nodeValue + "<br />";
chat_div.innerHTML += text_node[ 0 ].firstChild.nodeValue + "<br /><br />";
}
}
}
</script>



And here is the XML file (test doc for now):


<?xml version="1.0" ?>
<root>
<message id="1">
<user></user>
<text>testing stupid chat thing</text>
<time>0000-00-00 00:00:00</time>
</message>
<message id="2">
<user></user>
<text>dsadasfsdfsggsdgrgsdgfd</text>
<time>2010-04-22 19:17:16</time>
</message>
</root>

Hunter
April 11th, 2010, 10:31 AM
Never mind, fixed.

<user></user> had no value which was causing problems.

Limited
April 11th, 2010, 11:11 AM
Hunter, you need to just make 1 thread and keep replying to it with your problems....You start waaaaaaaay too many threads.

Hunter
April 11th, 2010, 02:45 PM
Yup, I noticed Lol.