Please use the java tags: [ java ] and [ /java ] (without the spaces).
To your problem:
You can't just comment lines out of the code and then expect the program to work.
These are the lines you shouldn't have commented:
userMessage = new JTextField();
buttonBox.add(userMessage);
sequence.deleteTrack(track);
track = sequence.createTrack();
//and this block
try {
while((obj = in.readObject()) != null) {
String nameToShow = (String) obj;
checkboxState = (boolean[]) in.readObject();
otherSeqsMap.put(nameToShow, checkboxState);
listVector.add(nameToShow);
incomingList.setListData(listVector);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
Your problem starts here:
new BeatBoxFinal();//.startUp(args[0]);
This way, startUp
never gets called, therefore sequence
never gets initialized and that's why you get a NullPointerexception when calling sequence.deleteTrack(track);
. Because you commented track = sequence.createTrack();
, track
didn't get initialized and so on and so on.
If you don't have any input, because you launch the program from your IDE, then write something like:
new BeatBoxFinal().startUp((args.length == 0)? "standard" : args[0]);
Here is the working code for your client (the server works fine as you wrote it).
I'm not sure what the first textfield does or better: should do, I'm leaving the
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.sound.midi.*;
import java.util.*;
import java.awt.event.*;
import java.net.*; // Class Socket is in java.net
import javax.swing.event.*;
public class BeatBoxFinal implements MetaEventListener {
public BeatBoxFinal() {
buildGUI();
}
JFrame theFrame;
JPanel mainPanel;
JButton submitMessage;
//JTextField message;
JTextArea chatArea;
JList incomingList;
JTextField userMessage;
ArrayList checkboxList;
int bpm = 120;
int nextNum;
Vector listVector = new Vector();
String userName;
ObjectOutputStream out;
ObjectInputStream in;
/*
A HashMap is like an ArrayList except you get to store a key/velue pair.
This is used to store the incoming messages from the server. The key is
the String message from the server. The value is the pattern associated
with that message.
*/
HashMap otherSeqsMap = new HashMap();
Sequencer sequencer;
Sequence sequence;
Sequence mySequence = null;
Track track;
// Instrument names come from this array
String[] instrumentNames = {
"bass1", "bass2", "drum1", "drum2", "hihat"
};
int[] instruments = {
35, 42, 46, 38, 49//, 39, 50, 60, 70, 72, 64, 56, 58, 47, 67, 63
};
public static void main(String[] args) {
new BeatBoxFinal().startUp((args.length == 0)? "standard" : args[0]);
}
public void startUp(String name) {
userName = name;
// Open connection to the server
try {
/*
Make a Socket connection to the server. 4242
is the port number. 127.0.0.1 is the IP address
for "localhost". This can be changed in order to
run the programs from different machines.
*/
Socket sock = new Socket("127.0.0.1", 4242);
out = new ObjectOutputStream(sock.getOutputStream());
in = new ObjectInputStream(sock.getInputStream());
Thread remote = new Thread(new RemoteReader());
remote.start();
}
catch(Exception ex) {
System.out.println("Couldn't connect - you'll have to play alone");
}
setUpMidi();
buildGUI();
}
public void buildGUI() {
theFrame = new JFrame("Cyber BeatBox");
BorderLayout layout = new BorderLayout();
JPanel background = new JPanel(layout);
background.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
checkboxList = new ArrayList();
Box buttonBox = new Box(BoxLayout.Y_AXIS);
submitMessage = new JButton("Submit message");
submitMessage.addActionListener(new MySubmitMessageListener());
buttonBox.add(submitMessage);
// message = new JTextField();
// buttonBox.add(message);
chatArea = new JTextArea();
buttonBox.add(chatArea);
/*
Create buttons and checkboxes. Add the buttons to the
GUI and add ActionListeners to them. Instead of passing
(this) to the buttons ActionListener registration method,
pass a new instance of the appropiate listener class.
The BoxLayout constructor needs to know the component its
laying out and the axis - the buttonBox and y axis.
*/
JButton start = new JButton("Start");
start.addActionListener(new MyStartListener());
buttonBox.add(start);
JButton stop = new JButton("Stop");
stop.addActionListener(new MyStopListener());
buttonBox.add(stop);
JButton upTempo = new JButton("Tempo up");
upTempo.addActionListener(new MyUpTempoListener());
buttonBox.add(upTempo);
JButton downTempo = new JButton("Tempo down");
downTempo.addActionListener(new MyDownTempoListener());
buttonBox.add(downTempo);
JButton sendIt = new JButton("Send it");
sendIt.addActionListener(new MySendListener());
buttonBox.add(sendIt);
userMessage = new JTextField();
buttonBox.add(userMessage);
incomingList = new JList();
incomingList.addListSelectionListener(new MyListSelectionListener());
incomingList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane theList = new JScrollPane(incomingList);
buttonBox.add(theList);
incomingList.setListData(listVector);
Box nameBox = new Box(BoxLayout.Y_AXIS);
for(int i = 0; i < 5; i++) {
nameBox.add(new Label(instrumentNames[i]));
}
background.add(BorderLayout.EAST, buttonBox);
background.add(BorderLayout.WEST, nameBox);
theFrame.getContentPane().add(background);
GridLayout grid = new GridLayout(5, 10);
grid.setVgap(1);
grid.setHgap(2);
mainPanel = new JPanel(grid);
background.add(BorderLayout.CENTER, mainPanel);
for (int i = 0; i < 50; i++) {
JCheckBox c = new JCheckBox();
c.setSelected(false);
checkboxList.add(c);
mainPanel.add(c);
}
theFrame.setBounds(50, 50, 300, 300);
theFrame.pack();
theFrame.setVisible(true);
}
public void setUpMidi() {
try {
// Get the sequencer, make a sequence and make a track
/*
The sequencer object is the main part of the MIDI
device/instrument being used. It's the thing that
"sequences" all the MIDI information into a song.
*/
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.addMetaEventListener(this);
// Make a sequence and a track
sequence = new Sequence(Sequence.PPQ, 4);
track = sequence.createTrack();
sequencer.setTempoInBPM(bpm);
}
catch(Exception e) {
e.printStackTrace();
}
}
public void buildTrackAndStart() {
/*
Build a track by walking through the checkboxes to
get their state, and mapping that to an instrument
(and making the MidiEvent for it).
*/
ArrayList trackList = null;
sequence.deleteTrack(track);
track = sequence.createTrack();
for(int i = 0; i < 5; i++) {
trackList = new ArrayList();
for(int j = 0; j < 5; j++) {
JCheckBox jc = (JCheckBox) checkboxList.get(j + (5 * i));
if(jc.isSelected()) {
int key = instruments[i];
trackList.add(new Integer(key));
}
else {
trackList.add(null);
}
}
makeTracks(trackList);
}
track.add(makeEvent(192, 9, 1, 0, 15));
try {
// Start it running
sequencer.setSequence(sequence);
sequencer.start();
sequencer.setTempoInBPM(bpm);
}
catch(Exception e) {
e.printStackTrace();
}
}
// The GUI listeners
/*
Implement the ActionListener interfaces ActionPerformed()
method. This is the event-handling method.
*/
public class MyStartListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
buildTrackAndStart();
}
}
public class MyStopListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
sequencer.stop();
}
}
public class MyUpTempoListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
bpm += 3;
}
}
public class MyDownTempoListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
bpm -= 3;
}
}
public class MySubmitMessageListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
if(a.getSource() == submitMessage) {
System.out.println("Message submitted");
chatArea.setText(userMessage.getText());
}
}
}
public class MySendListener implements ActionListener {
public void actionPerformed(ActionEvent a) {
// Make an arrayList of just the STATE of the checkboxes
boolean[] checkboxState = new boolean[50];
for(int i = 0; i < 50; i++) {
JCheckBox check = (JCheckBox) checkboxList.get(i);
if(check.isSelected()) {
checkboxState[i] = true;
}
}
/*
Serialize two objects (the String message and the beat pattern)
and write those two objects to the socket output stream to the server
*/
String messageToSend = null;
try {
out.writeObject(userName + nextNum++ + ": " + userMessage.getText());
out.writeObject(checkboxState);
}
catch(Exception ex) {
System.out.println("Sorry! Could not sent it to the server!");
}
userMessage.setText("");
}
}
/*
ListSelectionListener tells is when the user made a selection on the
list of messages. When the user selects a message, we immediately
load the associated beat pattern and start playing it.
*/
public class MyListSelectionListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent le) {
if(!le.getValueIsAdjusting()) {
String selected = (String) incomingList.getSelectedValue();
if(selected != null) {
boolean[] selectedState = (boolean[]) otherSeqsMap.get(selected);
changeSequence(selectedState);
sequencer.stop();
buildTrackAndStart();
}
}
}
}
/*
This is the thread work - reading in data from the server.
The data in this program will always be the two serialized
objects: the String message and the beat pattern - an
ArrayList of cjeckbox state values.
When a message comes in, we read or deserialize the two objects
(the message and the ArrayList of Boolean checkbox state values)
and add it to the JList component.
Adding to a JList is two-step process: you keep a Vector of the
lists data and then tell the JList to use that Vector as it's
source for what to display in the list.
*/
public class RemoteReader implements Runnable {
boolean[] checkboxState = null;
String nameToShow = null;
Object obj = null;
public void run() {
try {
while((obj = in.readObject()) != null) {
String nameToShow = (String) obj;
checkboxState = (boolean[]) in.readObject();
otherSeqsMap.put(nameToShow, checkboxState);
listVector.add(nameToShow);
incomingList.setListData(listVector);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
/*
This method is called when the user selects something
from the list. The pattern is immediately changed to
the one the user selected.
*/
public void changeSequence(boolean[] checkboxState) {
for(int i = 0; i < 50; i++) {
JCheckBox check = (JCheckBox) checkboxList.get(i);
check.setSelected(checkboxState[i]);
}
}
public void makeTracks(ArrayList list) {
Iterator it = list.iterator();
// Detect which key was pressed
for(int i = 0; i < 5; i++) {
Integer num = (Integer) it.next();
if(num != null) {
int numKey = num.intValue();
// This is where the beat is picked up from.
track.add(makeEvent(144, 9, numKey, 100, i));
track.add(makeEvent(128, 9, numKey, 100, i + 1));
}
}
}
// Add the four arguments for the message
public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
MidiEvent event = null;
try {
// Make the message and the event, using the parameters
// Make a message instance
ShortMessage a = new ShortMessage();
// Call setMessage() with the instructions
a.setMessage(comd, chan, one, two);
// Make a MidiEvent instance for the message
event = new MidiEvent(a, tick);
} catch(Exception e) {
}
// Return the event
return event;
}
public void meta(MetaMessage message) {
if(message.getType() == 47) {
sequencer.start();
sequencer.setTempoInBPM(bpm);
}
}
}