最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property '1' of null - Stack Overflow

programmeradmin1浏览0评论

Im getting an error: Uncaught TypeError: Cannot read property '1' of null Source: X.push( check [ 1 ]);

what is the problem?

wspolrzedne.value = text typed in textArea like that: "2.4 5 1 67 15 67"

So maybe the problem is becouse titanium do not safe that string with \n as new line?

var coordinates = wspolrzedne.value.split( "\n" );
    var X = [];
    var Y = [];


    for( var i = 0; i < coordinates.length; ++i ) {
        var check = coordinates[ i ].match( /^([0-9]+.[0-9]*) ([0-9]+.[0-9]*)$/ ); 

        if( check == false) {
                var zlewspolrzedne = Ti.UI.createAlertDialog({
                title: "Niew?a?ciwe wspó?rz?dne: " + coordinates[ i ],
                buttonNames: ['Popraw'],
                cancel: 0
            });
            zlewspolrzedne.show();
        }

        X.push( check[ 0 ] );
        Y.push( check[ 1 ] );    

    }

Im getting an error: Uncaught TypeError: Cannot read property '1' of null Source: X.push( check [ 1 ]);

what is the problem?

wspolrzedne.value = text typed in textArea like that: "2.4 5 1 67 15 67"

So maybe the problem is becouse titanium do not safe that string with \n as new line?

var coordinates = wspolrzedne.value.split( "\n" );
    var X = [];
    var Y = [];


    for( var i = 0; i < coordinates.length; ++i ) {
        var check = coordinates[ i ].match( /^([0-9]+.[0-9]*) ([0-9]+.[0-9]*)$/ ); 

        if( check == false) {
                var zlewspolrzedne = Ti.UI.createAlertDialog({
                title: "Niew?a?ciwe wspó?rz?dne: " + coordinates[ i ],
                buttonNames: ['Popraw'],
                cancel: 0
            });
            zlewspolrzedne.show();
        }

        X.push( check[ 0 ] );
        Y.push( check[ 1 ] );    

    }
Share Improve this question asked Jul 1, 2013 at 23:00 lol2xlol2x 4512 gold badges6 silver badges14 bronze badges 4
  • Where do you anticipate the \n appearing in the string? Clearly your regex won't match the full string. FYI, you can use the m modifier on the regex to redefine ^ and $ as matching the start and end of a line instead of the start and end of the string. – user2437417 Commented Jul 1, 2013 at 23:19
  • ...and if check is null, then I don't know why it wouldn't plain about index 0. Something's missing. – user2437417 Commented Jul 1, 2013 at 23:20
  • i forgot about enters in my example. im typing into textarea 2 numbers in each line – lol2x Commented Jul 1, 2013 at 23:25
  • im sure that this code work in normal js jsfiddle/fzYe7/10 – lol2x Commented Jul 1, 2013 at 23:30
Add a ment  | 

1 Answer 1

Reset to default 2

This is because textArea doesn't care about linebreaks, its just a simple whitespace. So the solution is to split by '' and then concat each two of them (or feed every pair directly to the check array and check their content on the fly).

Edit:

Ok, so the problem is this line:

var check = coordinates[ i ].match( /^([0-9]+.[0-9]*) ([0-9]+. [0-9]*)$/ )

It assigns only one value instead of two.

发布评论

评论列表(0)

  1. 暂无评论