The MidiBus Crashing

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;

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();
  }
  
}

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, the error doesn’t occur. So it has something to do with the values you pass in the constructor.

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() which make that possible.

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.

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

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();
  }
}

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

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:

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.

An alternative —but it might be a tough nut to crack— is to study the source code and see if it gives you any insights. The code below is the method where the error originates from, including a short description.

	/**
	 * 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;	
	}

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

Hey, I also came across this.

add

void dispose(){}

to your main sketch file

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

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.

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

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