# I want to get the value of a cp5 DropdownList

**URL:** <https://discourse.processing.org/t/i-want-to-get-the-value-of-a-cp5-dropdownlist/41925>\
**Category:** Libraries\
**Created:** [May 6, 2023, 5:11am UTC](https://discourse.processing.org/t/i-want-to-get-the-value-of-a-cp5-dropdownlist/41925 "2023-05-06T05:11:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azorbz](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@Azorbz](https://discourse.processing.org/u/Azorbz)\
**Post date:** [May 6, 2023, 5:11am UTC](https://discourse.processing.org/t/i-want-to-get-the-value-of-a-cp5-dropdownlist/41925/1 "2023-05-06T05:11:19Z")

</div>

I am trying to select a folder from a cp5 Dropdown List, but nothing I do seems to work. My best attempt is

(The current data folder contains three folders, “A”, “B”, and “C”, which are read perfectly well on the DropdownList)

```auto
import controlP5.*;
ControlP5 cp5;
import java.io.File;

float folder = 0.0;

String[] names;
int index = 0;

void setup() {
  size(500,500);
  cp5 = new ControlP5(this);
  File dir = new File(dataPath(""));
  names = dir.list();
  DropdownList droplist = cp5.addDropdownList("Sets")
  .setPosition(width/2 - 100, height/3 - 50)
  .setSize(200,120)
  .setBarHeight(40)
  .setItemHeight(40)
  .setOpen(false);
  for (int i=0; i < names.length; i++) {
    droplist.addItem(names[i], i);
  }
}

void draw() {
  background(0,0,0);
  fill(255,255,255);
  textAlign(CENTER, CENTER);
  folder = cp5.get(DropdownList.class, "droplist").getValue();
  text(str(folder), width/2, 2 * height / 3);
}

```

However, this gives a NullPointerError.

I have seen a solution on the forum from several months ago, and tried implementing the following instead

```auto
void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    if (theEvent.group().name() == "droplist")
    folder = theEvent.group().value();
  } else if(theEvent.isController()) {
    //
  }
}

```

This causes an error since apparently the “name” and “value” functions do not exist. Changing these to “getName” and “getValue” also did not work.

I am fairly new to Processing, so I am sorry if what I have done is poorly written. Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 6, 2023, 6:27am UTC](https://discourse.processing.org/t/i-want-to-get-the-value-of-a-cp5-dropdownlist/41925/2 "2023-05-06T06:27:47Z")

</div>

Something like the following should work. DropdownList is deprecated according to the author and it is suggested that you use ScrollableList instead. There is an example in Files/Examples/ControlP5. I’ll let you add the folder list since that was unavailable in the code posted.

```auto
// DropdownList is DEPRECATED, 
// use ScrollableList instead, see example ControlP5scrollableList
  
import controlP5.*;
import java.util.*;

ControlP5 cp5;

String[] names = {"A", "B", "C", "D", "E"};

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);
  cp5.addScrollableList("dropdown")
    .setPosition(width/2 - 100, height/3 - 50)
    .setSize(200, 120)
    .setBarHeight(20)
    .setItemHeight(20)
    .setOpen(false)
    .addItems(names)
    ;
}

void draw() {
  background(0);
}

void dropdown(int n) {
  /* request the selected item based on index n */
  println(n, cp5.get(ScrollableList.class, "dropdown").getItem(n));
}

```

---

<div class="post-metadata">

**Author:** ![Azorbz](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@Azorbz](https://discourse.processing.org/u/Azorbz)\
**Post date:** [May 6, 2023, 7:03pm UTC](https://discourse.processing.org/t/i-want-to-get-the-value-of-a-cp5-dropdownlist/41925/3 "2023-05-06T19:03:56Z")

</div>

Thank you for this. After implementing your solution, I managed to get it to work. Thank you again.
