What's new

Welcome to the forum 👋, Visitor

To access the forum content and all our services, you must register or log in to the forum. Becoming a member of the forum is completely free.

Cannot read property 'length' of null at getSum

master-pc

New member
Joined
Jul 3, 2021
Messages
2
Reaction score
0
HTML Coins
0
I am going to calculate total sales from a dynamically created table with JavaScript. I have two tables. Data from an Excel file are parsed into the first table. The second table has some data rows copied from the first table. There is a checkbox column in the first table that copies selected rows into the second table.

This is the function that creates the tale dynamically with JS.

JavaScript:
<script type="text/javascript">
    $("body").on("click", "#upload", function () {
        //Reference the FileUpload element.
        var fileUpload = $("#fileUpload")[0];

      
        var reader = new FileReader();

        //For Browsers other than IE.
        if (reader.readAsBinaryString) {
                reader.onload = function (e) {
                        ProcessExcel(e.target.result);
                    };
             reader.readAsBinaryString(fileUpload.files[0]);
             } else {
                    //For IE Browser.
                    reader.onload = function (e) {
                        var data = "";
                        var bytes = new Uint8Array(e.target.result);
                        for (var i = 0; i < bytes.byteLength; i++) {
                            data += String.fromCharCode(bytes[I]);
                        }
                        ProcessExcel(data);
                    };
                    reader.readAsArrayBuffer(fileUpload.files[0]);
                }
           
    });
    function ProcessExcel(data) {
        var dvExcel = $("#dvExcel");
        //Clear the table.
        $("#dvExcel tr").remove();
        //Read the Excel File data.
        var workbook = XLSX.read(data, {
            type: 'binary'
        });

        //Fetch the name of First Sheet.
        var firstSheet = workbook.SheetNames[0];

        //Read all rows from First Sheet into an JSON array.
        var excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);

        //Create a HTML Table element.
var table = $("<table />");
        table[0].border = "1";

        //Add the header row.
        var row = $(table[0].insertRow(-1));

        //Add the header cells.                                    

var headerCell = $("<th />");
        headerCell.html("Product Name");
        row.append(headerCell);

var headerCell = $("<th />");
        headerCell.html("Sales");
        row.append(headerCell);

        //Add the data rows from Excel file.
        for (var i = 0; i < excelRows.length; i++) {
            //Add the data row.
            var row = $(table[0].insertRow(-1));

            //Add the data cells.
var cell = $("<td />");
            cell.html(excelRows[I].ProductName);
            row.append(cell);

cell = $("<td />");
            cell.html(excelRows[I].Sales);
            row.append(cell);


cell = $("<td />");
            var selectedCB=document.createElement('input');
            selectedCB.type="checkbox";
            selectedCB.id = "chk" ;
            selectedCB.name = "chk" ;
            cell.html(selectedCB);
            row.append(cell);
        }

       
        dvExcel.html("");
        dvExcel.append(table);
    };[/I][/I][/I]

The headers for the second table are here, written in HTML:

HTML:
[I][I][I]<div id="endingHeader">
<div>
<h2>Selected Rows</h2>
</div>
<div>
<table class="secondTable">
<tr>
<th>Product name</th>
<th>Daily Sales</th>
</tr>
               
</table>
</div>
</div>[/I][/I][/I]

Here is the copy table function:

JavaScript:
[I][I][I]// Copying Function

$(function() {
       
        $(document).on("click",".transferrows", function() {
            $("#secondTable tr").remove();
            var getselectedValues=$("#dvExcel input:checked").parents("tr").clone().appendTo(".secondTable tbody").add(getselectedValues)
        })
    })[/I][/I][/I]


I have a button that would select a specific column, and then calculate the sum of the total sales in the selected column, which is column 2.

HTML:
[I][I][I]<div>
<button class="sum" onclick="getSum()">Get Sum</button>
</div>[/I][/I][/I]

Here is the JS code I have so far:

JavaScript:
[I][I][I]//This function takes the data from the first column and then calcualtes the sum.
function getSum()
{
    var table=document.getElementById("secondTable"), sumVal=0;
    for(var i=1;i<table.length;i++)             //<--- The offending line here
    {
        sumVal=sumVal+parseInt(table.rows[I].cells[1].innerHTML);
    }
    console.log(sumVal);
}[/I][/I][/I][/I]

That was where I hit a problem. It's called:

main.html:306 Uncaught TypeError: Cannot read property 'length' of null at getSum

What is wrong with my code? How can I Fix this?
 

Theme customization system

You can customize some areas of the forum theme from this menu.

  • Wide/Narrow view

    You can control a structure that you can use to use your theme wide or narrow.

    Grid view forum list

    You can control the layout of the forum list in a grid or ordinary listing style structure.

    Picture grid mode

    You can control the structure where you can open/close images in the grid forum list.

    Close sidebar

    You can get rid of the crowded view in the forum by closing the sidebar.

    Fixed sidebar

    You can make it more useful and easier to access by pinning the sidebar.

    Close radius

    You can use the radius at the corners of the blocks according to your taste by closing/opening it.

  • Choose the color combination that reflects your taste
    Background images
    Color gradient backgrounds
Back