Uncaught TypeError: Cannot set property '1' of undefined

edited December 2015 in JavaScript Mode

Hi everyone :)

I am trying to upload my processing sketch on the Web.

But i am getting the following error: Uncaught TypeError: Cannot set property '1' of undefined

In my processing sketch I have the following:

     Par[][] p1;

void setup() {

     p1 = new Par[140][140];

      for(int i = 0; i < p1.length*p1.length; i++) {
        p1[i/p1.length][i%p1.length] = new Par();
      }
}

void draw{
    for(Par[] d1:p1)for(Par q1:d1){
    q1.update();
  }
}

I think the error is in the for loop but I am not sure how to fix it...

Answers

  • I have just changed the two-dimensional array to a one-dimensional array and it worked perfectly.

    Anyone has any insight why it is not working for the two-dimensional?

  • edited December 2015 Answer ✓

    Languages like Java, C, C++, Python 2, etc., when dealing w/ division, if both operands are of type whole, the result is also of type whole. Anything after the decimal point is simply cut off! @-)

    Most of the time, that's unexpected and it's 1 of the biggest source of bugs for most programmers, even in this very forum here: :-&
    https://forum.Processing.org/two/discussions/tagged/division

    And particularly for Processing, to compound to that problem even more, not even a single letter is mentioned about it in the division operator's reference: ~X(
    https://Processing.org/reference/divide.html

    Interesting in your particular case here, the automatic decimal truncate @ [i/p1.length] is desired! ;))

    However, when the sketch is transpiled from Java to JS (Processing to PJS), the integer division result is lost! JS doesn't have any integer type hence all divisions are of fractional type implicitly! >-)

    In order to maintain the cross-mode match between Java & JS, we need to apply the fractional truncate step by ourselves. ~:>

    We can for example use the (int) cast operator over the whole division expression.
    Or call method int() for it too: https://Processing.org/reference/intconvert_.html

    But my fav is simply suffixing the division operator w/ | 0: i/p1.length | 0.
    Or even prefix it w/ double ~~ for a nice show off effect: ~~(i/p1.length) :ar!

    // forum.Processing.org/two/discussion/14158/
    // uncaught-typeerror-cannot-set-property-1-of-undefined
    
    // 2015-Dec-27
    
    class Par {
      void update() {
      }
    }
    
    static final int DIM = 140, QTY = DIM*DIM;
    final Par[][] pars = new Par[DIM][DIM];
    
    void setup() {
      for (int i = 0; i != QTY; ++i)  pars[i/DIM | 0][i%DIM] = new Par();
    }
    
    void draw() {
      for (Par[] row : pars)  for (Par par : row)  par.update();
    }
    

    However, you coulda simply used some regular double for () loop block in order to get by w/o worryin' 'bout division rules: :-\"

    for (Par[] row : pars)  for (int col = 0; col != DIM; row[col++] = new Par());
    

    TIP: Use CTRL+T inside Processing's IDE (PDE) in order to auto-indent the code there! :-B

  • thank you GoToLoop :)

Sign In or Register to comment.