Unable to retrieve array from other class

edited February 2016 in Android Mode

Hello! I'm working on a school assignment, it's nearly done but i'm unable to get some array's from the classes I made. I'm building the app for android devices, but I'm fairly sure that has nothing to do with it. The code that doesn't work is in the try block. Since both my classes have the exact same problem I've only included 1 here:

MAIN CLASS:

KnobInitiator ki;
SensorInitiator sen;

void setup() {
  orientation(PORTRAIT);
  size(displayWidth, displayHeight);
  KnobInitiator ki = new KnobInitiator();
  SensorInitiator sen = new SensorInitiator();
  ki.init(this);
  sen.init(this);
}

void draw() 
{
  background(255, 255, 255);
  try 
  {
  int[] rectVals = ki.getRectVals();//This doesn't work?
  int[] colorVals = sen.getColorVals();//This doesn't work?
  fill(colorVals[0], colorVals[1], colorVals[2]);//Causes app to crash
  rect(rectVals[0], rectVals[1], rectVals[2], rectVals[3]);//Causes app to crash
  } 
  catch (Exception ex) 
  {
   println("Failed to obtain rectVals."); 
  }
}

SECOND CLASS:

import controlP5.*;

public class KnobInitiator
{
  ControlP5 cp5;
  
  Knob rectX;
  Knob rectY;
  Knob rectW;
  Knob rectH;
  
  int pvalX= displayWidth/8;
  int pvalY= displayHeight/8;
  
  //RectVal: widht, height, x, y
  private int[] rectVal; 
  
  void init(PApplet  papp) 
  {
  rectVal = new int[] {0, displayHeight/2, 300, 300};
  cp5 = new ControlP5(papp);
  rectX = cp5.addKnob("X")
              .setRange(0, displayWidth)
              .setValue(0)
              .setPosition((1 * pvalX), (0.5 * pvalY))
              .setRadius(displayWidth/8)
              .setDragDirection(ControlP5.HORIZONTAL);
  rectY = cp5.addKnob("Y")
              .setRange(displayHeight/2, displayHeight)
              .setValue(0)
              .setPosition((4 * pvalX), (0.5 * pvalY))
              .setRadius(displayWidth/8)
              .setDragDirection(ControlP5.HORIZONTAL);
  rectW = cp5.addKnob("Width")
              .setRange(1, displayWidth)
              .setValue(0)
              .setPosition((1 * pvalX), (2 * pvalY))
              .setRadius(displayWidth/8)
              .setDragDirection(ControlP5.HORIZONTAL);
  rectH = cp5.addKnob("Height")
              .setRange(1, displayHeight/2)
              .setValue(0)
              .setPosition((4 * pvalX), (2 * pvalY))
              .setRadius(displayWidth/8)
              .setDragDirection(ControlP5.HORIZONTAL);
  }
  
  void rectX(int x) 
  {
    rectVal[0] = x;
  }

  void rectY(int y) 
  {
    rectVal[1] = y;
  }
  
  void rectW(int x) 
  {
    rectVal[2] = x;
  }
  
  void rectH(int y) 
  {
    rectVal[3] = y;
  }
  
  public int[] getRectVals()
  {
   return rectVal; 
  }
}

Answers

  • Thanks GoToLoop! Couldn't find how to do that! Apologies!

  • scope problems...

    ki on line 7 isn't the same as the ki on line 1, its scope is limited to setup().

    you are initialising the ki in line 7 but draw is accessing the ki instantiated BUT NOT initialised on line 1.

    remove the first KnobInitiator in line 7 (and the same for line 8).

  • @koogs that makes sense but in that case I'd have a followup issue, i can't possibly keep initializing in draw function. The knobs must be created one time only and keep monitoring from there on. So where would I init. them instead?

  • you already do. line 9 in setup calls ki.init()

    (unless your init() is badly named)

  • Answer ✓

    ah, when i say

    remove the first KnobInitiator in line 7 (and the same for line 8).

    i mean change it so that it reads just

    ki = new KnobInitiator();
    
  • @koogs that solved it after I figured out that I messed up an array somewhere! Thanks a lot!

Sign In or Register to comment.