trouble with Arraylist in constructor ; "the constructor sketch_123.StoreStuff() is undefined

I am trying to run this, but i cannot get the object to initialize.

 class StoreStuff 
    {
        private ArrayList<String> botle ;
        private ArrayList<String> apple ;

        public StoreStuff ( ArrayList<String> botle, ArrayList<String> apple ) {
            this.botle   = botle;
            this.apple   = apple;
        }

        void addApple() {
            apple.add("Jonagold");
        }
    }

    void draw () {
    final StoreStuff stuffs = new StoreStuff();

    stuffs.apple.add("Jonagold");
    println(stuffs.apple);
    } 

Answers

  • _vk_vk
    edited October 2014

    Hello, you defined one constructor in your class:

    public StoreStuff ( ArrayList<String> botle, ArrayList<String> apple ) {
               this.botle   = botle;
               this.apple   = apple;
           }
    

    It takes two ArrayList<String> as arguments. But when creating the object, you pass nothing as arguments.

     final StoreStuff stuffs = new StoreStuff();
    

    So the, very clear, message tells you that there is not a constructor with this signature (the kind and order of arguments).

    You can either build the object with the expected parameters, or define a constructor that takes no arguments.

    :)

    ps: Do you really wants to recreate the object 60 times each second in draw?

  • Nope, it that would be a bit redundant no? I expect the information to stay in the class without it needing to be recreated all the time.

  • _vk_vk
    edited October 2014 Answer ✓

    So you should not declare it inside draw()

    like:

    StoreStuff stuffs;
    ArrayList<String> bottles = new ArrayList <String>();
    ArrayList<String> apples = new ArrayList <String>();
    
    
    void setup(){ 
    // setup
    
    stuffs = new StoreStuff(bottles, apples); 
    }
    
    void draw () {
     // adding 60 "Jonagold" per second...
    stuffs.apple.add("Jonagold");
    println(stuffs.apple);
    

    }

  • Answer ✓

    Have you forgotten your previous StringList thread already? :-\"
    http://forum.processing.org/two/discussion/7657/how-to-put-a-stringlist-in-a-constructor

  • yehaaa, that works!, many many thanx! ;) you made my day :)

  • _vk_vk
    edited October 2014 Answer ✓

    :) I don't know what you are looking for, but if you intent to pass the apples alone, perhaps you only need the Lists inside the class not the outside ones...

    perhaps you are looking for this? (I changed the behaviour of the addApple to don't always add "JonasGold")

    edit2: and changed the constructor also.

    StoreStuff stuffs;
    
    String[] testApples = {"JonaGold", "JonaSilver", "JonaCooper", "JonaIron", "JonaPlatinum"};
    int i;
    
    
    void setup(){ 
    // setup
    
    stuffs = new StoreStuff(); 
    }
    
    void draw () {
    
    }
    
    void keyPressed(){
      stuffs.addApple(testApples[i]);
      i = (i+1)%testApples.length;
      println(stuffs.apple);
    }
    
    class StoreStuff 
    {
      private ArrayList<String> bottle ;
      private ArrayList<String> apple ;
    
      public StoreStuff () {
        this.bottle = new ArrayList<String>();
        this.apple  = new ArrayList<String>();
      }
    
      void addApple(String appleToAdd) {
        apple.add(appleToAdd);
      }
    }
    
  • edited October 2014

    GoToLoop: No, I haven't forgotten about it, in fact that is from where I started!!, but there was something there that I could not grasp, I was missing something.

    _vk : Thx, this gives me all the functionality I need :)

  • edited October 2014

    ... but there was something there that I could not grasp, ...

    Well, if you don't tell us, we can't guess it, right? ;)

  • Well the thing in question was that I didn't knew how initialize the class, or how put any values/strings into it.

  • edited October 2014 Answer ✓
    // forum.processing.org/two/discussion/7657/
    // how-to-put-a-stringlist-in-a-constructor
    
    class StoreStuff {
      final StringList bottles = new StringList();
      final StringList apples  = new StringList();
    
      StoreStuff addBottle(String... bottle) {
        bottles.append(bottle);
        return this;
      }
    
      StoreStuff addApple(String... apple) {
        apples.append(apple);
        return this;
      }
    
      @ Override String toString() {
        return "StoreStuff:\n"
          + bottles.toString() + "\n"
          + apples.toString();
      }
    }
    
    final StoreStuff stuffs = new StoreStuff();
    
    void setup() {
      stuffs
        .addBottle("Katniss", "Magnetic_Garden")
        .addApple("Jonagold");
    
      println(stuffs);
      exit();
    }
    

    ...how initialize the class, ...

    new StoreStuff();

    ... or how put any values/strings into it.

    <StoreStuff's reference>.addBottle("Katniss", "Magnetic_Garden")

    Alternatively, w/o using some "setter" method:

    <StoreStuff's reference>.bottles.append("Katniss");
    <StoreStuff's reference>.bottles.append("Magnetic_Garden");

  • Thx Dude!!, this is all of great help!

Sign In or Register to comment.