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 them
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 index0
. 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
1 Answer
Reset to default 2This 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.