user727198
user727198

Reputation: 89

Uncaught TypeError for javascript

I am getting the this message "Uncaught TypeError: Cannot read property 'split' of null" for this linesplitted = elements[i].nextSibling.nodeValue.split("||"); The code is in below. I will appreciate your input to solve this. The code has worked before. I think it is something about new php oder jquery...

    $('#r_NEStd').hide(); // Das Feld ist am Anfang versteckt und duch klicken 
auf NE wird wieder gezeigt.
$("input[name='x_Posten[]'], input[name='x_NE[]']").click(function() { // 
    Field2 has multiple inputs(checkboxes) so they should be selected by name
    document.getElementById("x_Total").readOnly = true;
    document.getElementById("x_Auslagen").readOnly = true;
    document.getElementById("x_RBetrag").readOnly = true;
    if ($("input[name='x_Posten[]']:checked").val() &&
        $("input[name='x_NE[]']:checked").val()) { // WENN NichtErhaeltlichkeit und 
        Posten geklickt sind
        document.finvoicesadd.x_Total.value = '';

        var Anzahl = 0;
        var Auslagen = 0;
        var AuslagenMwSt = 0;
        var Total = 0;
        var splitted;
        var nestdsatz = document.finvoicesadd.x_NEStd.value;
        var e = document.getElementById("x_Vorschuss");
        var VorschussValue = e.options[e.selectedIndex].value;
        var nestdsatz = document.finvoicesadd.x_NEStd.value;
        var elements = document.getElementsByName("x_Posten[]");
        for (i = 0; i < elements.length; i++) {
            if (elements[i].checked) {
                splitted = elements[i].nextSibling.nodeValue.split("||");
                Auslagen = Auslagen + +parseFloat(splitted[4]);
                AuslagenMwSt = AuslagenMwSt + +(parseFloat(splitted[4]) *
                    (1 + parseFloat(splitted[5])));
                Anzahl = Anzahl + +parseFloat(splitted[2]);
                Total = Total + +(parseFloat(splitted[2] * nestdsatz) *
                    (1 + parseFloat(splitted[5])));
            }
        }
        document.finvoicesadd.x_Auslagen.value = Auslagen.toFixed(2);
        document.finvoicesadd.x_Total.value = (Total + AuslagenMwSt).toFixed(2);
        document.finvoicesadd.x_RBetrag.value = (Math.round(((Total +
            AuslagenMwSt) - VorschussValue) * 20, 0) / 20).toFixed(2);
        document.finvoicesadd.x_Vorlage.value = "rechnungne";

    } else { // WENN x_Posten oder NE nicht oder nur ein Feld gelickt ist
        document.finvoicesadd.x_Total.value = '';
        var Anzahl = 0;
        var Auslagen = 0;
        var Total = 0;
        var splitted;
        var e = document.getElementById("x_Vorschuss");
        var VorschussValue = e.options[e.selectedIndex].value;
        var nestdsatz = document.finvoicesadd.x_NEStd.value;
        var elements = document.getElementsByName("x_Posten[]");
        for (i = 0; i < elements.length; i++) {
            if (elements[i].checked) {
                splitted = elements[i].nextSibling.nodeValue.split("||");
                // Auslagen = Auslagen+ +(parseFloat(splitted[4])* 
                (1 + parseFloat(splitted[5])));
            Auslagen = Auslagen + +parseFloat(splitted[4]);
            Anzahl = Anzahl + +parseFloat(splitted[3]);
            Total = Total + +parseFloat(splitted[6]);
        }
    }
    document.finvoicesadd.x_Auslagen.value = Auslagen.toFixed(2);
    document.finvoicesadd.x_Total.value = (Total).toFixed(2);
    document.finvoicesadd.x_RBetrag.value = (Math.round(((Total) -
        VorschussValue) * 20, 0) / 20).toFixed(2);
    document.finvoicesadd.x_Vorlage.value = "rechnung";
}
});

Upvotes: 1

Views: 107

Answers (1)

Saturnix
Saturnix

Reputation: 10554

Replace this:

if (elements[i].checked) {

with this:

debugger;
if (elements[i].checked && elements[i].nextSibling.nodeValue != null) {

This will solve the error, although I have no idea which repercussions it will have on your program.

The error is given by the fact that elements[i].nextSibling.nodeValue is null when you're checking it.

Upvotes: 1

Related Questions