null exception in javascript - javascript

I met with the following error from the following javascript functions, any ideas what is wrong?
BTW: since the whole page is long, I can not post them all here. I am trying to find a small but complete sample to reproduce this issue. Any ideas to debug further to find the root cause?
'Null' is null or not an object
Script:
<script>
$(document).ready(function() {
$("#tag0").tooltip({ effect: 'slide'});
});
</script>
thanks in advance,
George

My first guess was that
$("#tag0")
is returning null, and attempting to call a method on null is probably giving you the error. I have been informed that jQuery won't actually return null if your selector doesn't match anything -- you just get an empty set of results with a length property of 0. If you call a nonexistent method on an object of this result type, perhaps you get the error message you're seeing.
Is it possible there isn't actually an element on the page with ID "tag0"? Should it be a class instead (".tag0" instead of "#tag0")?

Related

Using a for loop in JavaScript to loop over an array of objects

My code and terminal
Just ignore the comments, they are in danish as it is for a school assignment..
I need some help. My loop keeps giving me an undefined value in my terminal, and I can't seem to find the issue. It is working as it should, and gives me the correct values, but still has those 'undefined' ones which irritates me.
EDIT: Has been fixed by #aqq, thx for the help everybody!
As #dikuw mentioned the undefined call might be coming from the gørBeskedPersonlig function being called.
I don't see that function being defined in your code so that's probably it, try commenting out the following line:
console.log(gørBeskedPersonlig(katalog[index].navn));
UPDATE: After reviewing your code, i can see that the function gørBeskedPersonlig was not returning anything.
Updating it to return the new value has fixed the "undefined" error.
function gørBeskedPersonlig(navn){
hemmeligBesked.splice(1,1,navn+'!');
return hemmeligBesked.join(' ');
}
It seems that you have some console.log() in function gørBeskedPersonlig that is returning undefined. Send a code for that function.

Javascript json, check all keys for undefined ('null') and set default

Firstoff I'd like to add I've been learning javascript for like only 2 days now. I'm pretty much way ahead of myself with what I'm trying to get but here goes.
I have a json array from which I get data to replace/insert in my page. The first problem I have is that if it comes across an empty ('null') key it will just stop. Will not even try to continu.
document.getElementById("id1")src=json.img1.link;
document.getElementById("id2")src=json.img2.link;
document.getElementById("id3")src=json.img3.link;
json.img2.link is empty ('null' response from json.). javascript will then not replace "id2" but it also won't replace "id3".
I'm now trying to find a solution where it will if nothing else at least set a default.
The script is not continuing executing because it comes to an error --trying to access property link of an undefined object
Try
document.getElementById('id2').src = json.img2 ? json.img2.link : 'defaultLink';
This way your are checking for undefined (ie null) object in img2 and assigning the default value. This assumes that what is not defined (null) is the img2 object.
Actually I don't think your code should work at all, you are missing the. before the src So, try
document.getElementById("id1").src=json.img1.link;
document.getElementById("id2").src=json.img2.link;
document.getElementById("id3").src=json.img3.link;
and let us know if that doesn't solve the problem.
Btw, +points for learning JavaScript and not just straight into jQuery!

javascript if condition not executing all commands

I'm having one little iritating problem. I have simple if condition in javascript code.
It goes something like this:
if (istinito)
{
alert ('123');
document.getElementById('obavestavanje').value="Pobedi "+ime_igraca+"!!!";
kraj=true;
}
Alert apears when istinito=true, but element with id="obavestenje" never get its value, and variable kraj never is set to true. Variable kraj is global variable, and there are no conflicts with other parts of the JS code.
Any ideas why code stops after alert?
Looks like document.getElementById('obavestavanje') is returning null. You are trying to de-reference the null reference by using document.getElementById('obavestavanje').value which results in null pointer exception. If you look into the console, you should see some exception being raised. It is always a good idea to check if the document.getElementById() is returning a valid object before trying to dereference it.
e.g.
if (istinito)
{
alert ('123');
element = document.getElementById('obavestavanje')
if(element){
element.value="Pobedi "+ime_igraca+"!!!";
}
kraj=true;
}
First advice i could give you:
Use more console logging for debugging. Almost any modern browser got a console to debug and other things.
if (istinito) {
console.log("i am here");
}
from that same console you can also execute commands. Those dom manipulations are easily done from the console. just run them and see if it works.
the code:
document.getElementById('obavestavanje').value = "some value"
looks ok. nothing wrong with it. i guess you don't have an element with id "obavestavanje" ?
Looks like your code is okay. And you are sure you have an element by id 'obavestavanje'. Could you please tell what element is it? Is it a button, textbox or someting like that?
Also the String in the "Pobedi "+ime_igraca+"!!!" , what is 'ime_igraca'? Is it a variable and if it is have you defined this variable somewhere?
Or did you mean to give the value "Pobedi ime_igraca !!!" ??
Thanks
Ranis MK

document.evaluate won't work from content script

var allTags = document.evaluate("//*[contains(#src,'"+imgSrc+"')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
This is the code that gives errors, it gives:
Uncaught Error: TYPE_ERR: DOM XPath Exception 52
Could someone tell me what the problem is?
I don't have a precise answer, but I can guess and give a workaround.
First the work around: change UNORDERED_NODE_SNAPSHOT_TYPE to a type that don't create a snapshot(unless you need it that way) and returns multiple nodes like UNORDERED_NODE_ITERATOR_TYPE(or ANY_TYPE).
And my guess: After reading the spec it say for this function 'TYPE_ERR: Raised if the result cannot be converted to return the specified type.'. It may be the case it can't allocate the resources to create a snapshot or something like this(the workaround assumes that).
Edit:
The real problem is most likely not the call to document.evaluate is that in your code you do allTags.iterateNext and this call expects allTags to be a *_NODE_ITERATOR_TYPE and not a *_NODE_SNAPSHOT_TYPE, using allTags.snapshotItem don't cause an error to be thrown. I wrote a sample at jsfiddle, it changes the borders after 2 seconds using the call to evaluate in your question and iterate over the elements in the proper way.

Javascript height returning "getElementById('div') is null?

k this is probably something very easy but for some reason i am getting an error when trying to use javascript to change style.height.
My javascript looks like this:
document.getElementById('div').style.height=100px;
I have also tried
document.getElementById('div').style.height='100px';
document.getElementById('div').style.height='100';
document.getElementById('div').style.height=100;
and get the same error every time.
The error i am getting is:
document.getElementById('div') is null
I get this error in my firebug console. Thanks.
You probably don't have an element with id="div".
Or you're running your code before the body exists.
this have to work fine :
document.getElementById('div').style.height='100px';
please check your html code, and try in the console :
document.getElementById('div').style.height;
and see what returns after and before
(maybe its working and you cant see it, you can check it adding an background-color)

Categories

Resources