JDK-5048381 : NullPointerException when saving a MIDI sequence
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-05-17
  • Updated: 2004-05-28
  • Resolved: 2004-05-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b54Fixed
Description

Name: gm110360			Date: 05/17/2004


FULL PRODUCT VERSION :
java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

EXTRA RELEVANT SYSTEM CONFIGURATION :
The MIDI sequence that is failing to save was recorded from external MIDI port connected to a Yamaha digital piano.

A DESCRIPTION OF THE PROBLEM :
After recording a MIDI sequence from an external MIDI device, attempts to save the sequence either hang or result in the following exception.

java.lang.NullPointerException
        at java.io.SequenceInputStream.nextStream(SequenceInputStream.java:95)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:175)
        at java.io.InputStream.read(InputStream.java:89)
        at com.sun.media.sound.StandardMidiFileWriter.write(StandardMidiFileWriter.java:130)
        at javax.sound.midi.MidiSystem.write(MidiSystem.java:992)
        at toolkit.sound.midi.MidiDeviceConnector$1.meta(MidiDeviceConnector.java:774)
        at com.sun.media.sound.EventDispatcher.processEvent(EventDispatcher.java:108)
        at com.sun.media.sound.EventDispatcher.dispatchEvents(EventDispatcher.java:174)
        at com.sun.media.sound.EventDispatcher.run(EventDispatcher.java:200)
        at java.lang.Thread.run(Thread.java:566)



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Connect the external MIDI device to the sequencer.
2. Prepare a new sequence for recording.
3. Record to the new sequence for a few seconds.
4. Save the sequence to a file.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was expecting the sequence to be written to the specified file, however the method either hangs or throws an exception after 14 bytes were written to the specified file.
ACTUAL -
Listening...Done.
   28: F8
   46: F8
   64: F8
   83: F8
  101: F8
  121: F8
  139: F8
  157: F8
  175: F8
  194: F8
  212: F8
  231: F8
  249: F8
  268: F8
  286: F8
  304: F8
  323: F8
  341: F8
  360: F8
  380: F8
  397: F8
  416: F8
  433: F8
  452: F8
  471: F8
  489: F8
  507: F8
  523: 90 3C 20
  526: F8
  549: F8
  562: F8
  581: F8
  602: F8
  619: F8
  637: F8
  659: F8
  674: F8
  692: F8
  709: 90 3C 00
  711: F8
  730: 90 3E 25
  730: F8
  748: F8
  766: F8
  784: F8
  803: F8
  821: F8
  840: F8
  859: F8
  877: F8
  896: F8
  914: F8
  924: 90 40 28
  925: 90 3E 00
  933: F8
  951: F8
  969: F8
  989: F8
 1006: F8
 1025: F8
 1043: F8
 1062: F8
 1081: F8
 1099: F8
 1116: 90 41 30
 1117: F8
 1120: 90 40 00
 1139: F8
 1154: F8
 1173: F8
 1191: F8
 1209: F8
 1228: F8
 1249: F8
 1264: F8
 1283: F8
 1300: 90 41 00
 1301: F8
 1314: 90 43 32
 1321: F8
 1339: F8
 1357: F8
 1376: F8
 1394: F8
 1413: F8
 1431: F8
 1449: F8
 1468: F8
 1486: F8
 1496: 90 43 00
 1505: F8
 1514: 90 41 34
 1522: F8
 1541: F8
 1560: F8
 1579: F8
 1597: F8
 1615: F8
 1634: F8
 1652: F8
 1671: F8
 1689: F8
 1708: F8
 1715: 90 41 00
 1724: 90 40 2F
 1726: F8
 1744: F8
 1763: F8
 1784: F8
 1800: F8
 1819: F8
 1837: F8
 1856: F8
 1873: F8
 1892: F8
 1895: 90 40 00
 1900: 90 3E 24
 1910: F8
 1929: F8
 1947: F8
 1966: F8
 1984: F8
 2003: F8
 2022: F8
 2041: F8
 2059: F8
 2077: F8
 2095: F8
 2115: 90 3E 00
 2115: F8
 2133: F8
 2136: 90 3C 27
 2151: F8
 2169: F8
 2188: F8
 2206: F8
 2225: F8
 2256: F8
 2266: F8
 2284: F8
 2299: F8
 2317: F8
 2335: F8
 2353: F8
 2372: F8
 2390: F8
 2409: F8
 2427: F8
 2446: F8
 2464: F8
 2482: F8
 2501: F8
 2520: F8
 2539: F8
 2557: F8
 2568: 90 3C 00
 2576: 90 43 31
 2578: F8
 2594: F8
 2612: F8
 2631: F8
 2650: F8
 2669: F8
 2693: F8
 2705: F8
 2723: F8
 2741: F8
 2760: F8
 2779: F8
 2797: F8
 2816: F8
 2833: F8
 2853: F8
 2870: F8
 2889: F8
 2908: F8
 2926: F8
 2944: F8
 2965: F8
 2981: F8
 3000: F8
 3003: 90 43 00
 3019: F8
 3022: 90 37 29
 3037: F8
 3055: F8
 3074: F8
 3093: F8
 3115: F8
 3129: F8
 3148: F8
 3173: F8
 3184: F8
 3203: F8
 3221: F8
 3240: F8
 3259: F8
 3280: F8
 3296: F8
 3314: F8
 3333: F8
 3352: F8
 3370: F8
 3387: F8
 3406: F8
 3424: F8
 3442: F8
 3461: F8
 3480: F8
 3499: F8
 3517: F8
 3535: F8
 3555: F8
 3573: F8
 3595: F8
 3609: F8
 3628: F8
 3649: F8
 3664: F8
 3682: F8
 3702: F8
 3720: F8
 3738: F8
 3759: F8
 3776: F8
 3777: 90 37 00
 3793: F8
 3818: F8
 3830: F8
 3849: F8
 3868: F8
 3868: FF 2F 00
Saving...java.lang.NullPointerException
        at java.io.SequenceInputStream.nextStream(SequenceInputStream.java:95)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:175)
        at java.io.InputStream.read(InputStream.java:89)
        at com.sun.media.sound.StandardMidiFileWriter.write(StandardMidiFileWriter.java:130)
        at javax.sound.midi.MidiSystem.write(MidiSystem.java:992)
        at Test.main(Test.java:60)


-------------------------------------------------
Contents of test.mid: (It appears only the header is being written)
4D 54 68 64 00 00 00 06  00 00 00 01 01 80

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.NullPointerException
        at java.io.SequenceInputStream.nextStream(SequenceInputStream.java:95)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:175)
        at java.io.InputStream.read(InputStream.java:89)
        at com.sun.media.sound.StandardMidiFileWriter.write(StandardMidiFileWriter.java:130)
        at javax.sound.midi.MidiSystem.write(MidiSystem.java:992)
        at Test.main(Test.java:60)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;
import javax.sound.midi.*;

public class Test
{
	
	public static void main(String[] args)
	{
		try
		{
			// Connect the external MIDI device to the sequencer.
			Sequencer s = MidiSystem.getSequencer();
			Transmitter t = MidiSystem.getTransmitter();
			t.setReceiver(s.getReceiver());

			// Prepare a new sequence for recording.
			Sequence seq = new Sequence(Sequence.PPQ, 384, 1);
			Track track = seq.getTracks()[0];
			s.setSequence(seq);
			s.recordEnable(track, -1);
			
			// Record to the new sequence for a few seconds.
			s.open();
			s.startRecording();
			System.out.print("Listening...");
			Thread.sleep(5000);
			s.close();
			System.out.println("Done.");
			
			// Must have more than end-of-track message to save.
			if (track.size() <= 1)
			{
				System.out.println("No MIDI events were recorded.");
			}
			else
			{
				for (int i = 0; i < track.size(); i++)
				{
					printEvent(track.get(i));
				}
				
				// Save the sequence to a file.
				System.out.print("Saving...");
				FileOutputStream fos = new FileOutputStream("test.mid");
				MidiSystem.write(seq, 0, fos);
				System.out.println("Done.");
			}
			
			System.exit(0);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			System.exit(1);
		}
	}
	
	public static void printEvent(MidiEvent event)
	{
		MidiMessage message = event.getMessage();
		long tick = event.getTick();
		byte[] data = message.getMessage();
		
		StringBuffer sb = new StringBuffer((data.length * 3) - 1);

		for (int i = 0; i < data.length; i++)
		{
			sb.append(toHexByteString(data[i]));
			if (i < data.length - 1) sb.append(' ');
		}
		System.out.printf("%5d: %s%n", tick, sb);
	}
	
	private static String toHexByteString(int n)
	{
		if (n < 0) n &= 0xFF;
		String s = Integer.toHexString(n).toUpperCase();
		if (s.length() == 1) s = '0' + s;
		return s;
	}
}

---------- END SOURCE ----------
(Incident Review ID: 265555) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-rc FIXED IN: tiger-rc INTEGRATED IN: tiger-b54 tiger-rc
14-06-2004

PUBLIC COMMENTS Scheduled for tiger-rc.
10-06-2004

EVALUATION Seems to be a 2-folded problem: 1) The MIDI IN device receives MIDI real-time messages F8 (TIMING CLOCK). Those messages are not intended to be recorded in a sequence. 2) The MIDI file writer internally chokes on these real time messages which creates an inconsistent internal state, leading to the NPE. The fix consists of: 1) do not add real time messages to the recorded sequence in Sequencer. 2) ignore real-time messages when writing to a MIDI file. Should get fixed for tiger-rc. ###@###.### 2004-05-17
17-05-2004