# The MidiBus Crashing

**URL:** <https://discourse.processing.org/t/the-midibus-crashing/39537>\
**Category:** Libraries\
**Created:** [November 1, 2022, 8:31am UTC](https://discourse.processing.org/t/the-midibus-crashing/39537 "2022-11-01T08:31:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ducky](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@Ducky](https://discourse.processing.org/u/Ducky)\
**Post date:** [November 1, 2022, 8:31am UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/1 "2022-11-01T08:31:08Z")

</div>

Hi everyone!

I’ve recently been working on assignment and have come to a issue that stumps me completely.

If I call upon end() when trying to run code with a MidiBus using the library ‘The MidiBus’ ,the sketch crashes instead.

I have no idea why this is happening and would be very grateful for any insights people may have!

Cheers!

Code below;

```auto
import themidibus.*;
MidiBus myBus;

void setup() {
  
    fullScreen(); 
    
   //////MIDI////
  
  //MidiBus.list();
  myBus = new MidiBus (this,3,3);//MUTING THIS LINE PREVENTS CRASHING
  
}

void draw(){

 background ((map(sin(frameCount*0.005),-1,1,0,255)),(map(sin(frameCount*0.015),-1,1,0,255)),(map(sin(frameCount*0.01),-1,1,0,255)));
  
  if(keyPressed==true){
  exit();
  }
  
}

```

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 2, 2022, 1:07pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/2 "2022-11-02T13:07:47Z")

</div>

I’m not familiar with the library you’re using, but when I initialize `myBus` in the same way it’s done in the [basic example](https://github.com/sparks/themidibus/blob/master/examples/Basic/Basic.pde), the error doesn’t occur. So it has something to do with the values you pass in the constructor.

```auto
myBus = new MidiBus(this, -1, "Java Sound Synthesizer");

```

ps. When working with user input such as key presses or mouse clicks, my suggestion is to take care of it outside `draw()`. There are methods within Processing such as [`keyPressed()`](https://processing.org/reference/keyPressed_.html) which make that possible.

---

<div class="post-metadata">

**Author:** ![Ducky](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@Ducky](https://discourse.processing.org/u/Ducky)\
**Post date:** [November 2, 2022, 9:07pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/3 "2022-11-02T21:07:33Z")

</div>

Thank you for your help!

Unfortunately I tried changing the values going through the constructor previously and encountered the same problem. If I change the values to unassigned integers then I don’t have the issue - it only happens when either the 2nd or 3rd values in the constructor are utilised.

I’ve also tried using the actual tags as you have in yours with the “Java Sound Synthesizer” but unsurprisingly that made no difference.

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 2, 2022, 11:04pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/4 "2022-11-02T23:04:23Z")

</div>

What error do you get if you run the following code?

```auto
import themidibus.*;
MidiBus myBus;

void setup() {
  size(400, 400);
  myBus = new MidiBus(this, -1, "Java Sound Synthesizer");
}

void draw() {
  background ((map(sin(frameCount*0.005), -1, 1, 0, 255)), (map(sin(frameCount*0.015), -1, 1, 0, 255)), (map(sin(frameCount*0.01), -1, 1, 0, 255)));
  if (keyPressed==true) {
    exit();
  }
}

```

---

<div class="post-metadata">

**Author:** ![Ducky](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@Ducky](https://discourse.processing.org/u/Ducky)\
**Post date:** [November 3, 2022, 12:52am UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/5 "2022-11-03T00:52:52Z")

</div>

“The MidiBus Warning: No available input MIDI devices named: “Java Sound Synthesizer” were found”

---

<div class="post-metadata">

**Author:** ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)\
**Post date:** [November 3, 2022, 10:43pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/6 "2022-11-03T22:43:51Z")

</div>

Alright, so we get the same warning. In my case that’s different from the error I got from the code in your original post, where the entire sketch crashed when I pressed a button:

```auto
The MidiBus Warning: The chosen input device numbered [3] was not added because it doesn't exist
java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because the return value of "java.util.Map.get(Object)" is null
	at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1334)
	at processing.core.PApplet.handleMethods(PApplet.java:1488)
	at processing.core.PApplet.dispose(PApplet.java:3529)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:405)
NullPointerException

```

Unfortunately I know nothing about this library so I can’t be of much more help, but my suggestion is to search online for people with [similar warnings](https://github.com/ghztomash/visuals_tutorial/issues/2).

An alternative —but it might be a tough nut to crack— is to study the [source code](https://github.com/sparks/themidibus/blob/66c055c09704778a37af090a48dbfe33cdc42327/src/themidibus/MidiBus.java) and see if it gives you any insights. The code below is the method where the error originates from, including a short description.

```auto
	/**
	 * Adds a new MIDI output device specified by the name device_name. If the MIDI output device has already been added, it will not be added again.
	 * <p>
	 * If two or more MIDI outputs have the same name, whichever appears first when {@link #list()} is called will be added. If this behavior is problematic use {@link #addOutput(int device_num)} instead.
	 *
	 * @param device_name the name of the MIDI output device to be added.
	 * @return true if and only if the output device was successfully added.
	 * @see #addOutput(int device_num)
	 * @see #list()
	*/
	public boolean addOutput(String device_name) {
		if (device_name.equals("")) return false;
		
		MidiDevice.Info[] devices = availableOutputsMidiDeviceInfo();
		
		for (int i = 0;i < devices.length;i++) {
			if (devices[i].getName().equals(device_name)) return addOutput(devices[i]);
		}
		
		System.err.println("\nThe MidiBus Warning: No available input MIDI devices named: \""+device_name+"\" were found");
		return false;	
	}

```

---

<div class="post-metadata">

**Author:** ![Ducky](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@Ducky](https://discourse.processing.org/u/Ducky)\
**Post date:** [November 3, 2022, 10:57pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/7 "2022-11-03T22:57:20Z")

</div>

Oh okay. Thanks heaps for all your time, I really appreciate it!

---

<div class="post-metadata">

**Author:** ![null0](https://avatars.discourse-cdn.com/v4/letter/n/ba9def/32.png) [@null0](https://discourse.processing.org/u/null0)\
**Post date:** [November 27, 2022, 1:25pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/8 "2022-11-27T13:25:00Z")

</div>

Hey, I also came across this.

add

```auto
void dispose(){}

```

to your main sketch file

---

<div class="post-metadata">

**Author:** ![nchowlett](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@nchowlett](https://discourse.processing.org/u/nchowlett)\
**Post date:** [December 3, 2022, 12:19am UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/9 "2022-12-03T00:19:30Z")

</div>

Heya @Ducky, think I was having the same issue. Turns out my issue could be fixed by using a work-around where you create a dummy function and use that to prevent the exception being raised: [Not working on processing4 · Issue #28 · sparks/themidibus · GitHub](https://github.com/sparks/themidibus/issues/28)

---

<div class="post-metadata">

**Author:** ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.processing.org/u/Christian)\
**Post date:** [December 15, 2022, 1:50pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/10 "2022-12-15T13:50:55Z")

</div>

Did you check the Midi device list? My sketch also crashed after I had upgraded Mac OS, guess the index changed by doing this. So you might have to change the integers here according to the device you want to use.

```auto
myBus = new MidiBus(this, 1, 2);

```

---

<div class="post-metadata">

**Author:** ![Christian](https://avatars.discourse-cdn.com/v4/letter/c/b2d939/32.png) [@Christian](https://discourse.processing.org/u/Christian)\
**Post date:** [April 25, 2023, 1:34pm UTC](https://discourse.processing.org/t/the-midibus-crashing/39537/11 "2023-04-25T13:34:39Z")

</div>

Now I actually think it didn’t crash because of the OS update but the migration to Processing 4 😜
