I am having trouble stopping a sequence that is playing through a midi sequencer. This is because a null pointer exception is thrown (I will show you where in red text). The class in which the sequencer is created is different from the class in which I am trying to stop the sequence. I cannot figure out for my life, why it throws a null pointer.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.midi.*;
import java.io.*;
import java.net.*;
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
public class mario1{
public static void main(String[]args){
new mario1().run();
} // end of main
public void run(){
****** This is where the Sequence is created
try{
Sequence sequence = MidiSystem.getSequence(new File("C:\\users\\Matt\\Documents\\mario09.mid"));
Sequence sequence1 = MidiSystem.getSequence(new File("C:\\users\\Matt\\Documents\\ff3battl.mid"));
// Create a sequencer for the sequence
Sequencer sequencer = MidiSystem.getSequencer();
Sequencer sequencer1 = MidiSystem.getSequencer();
sequencer.open();
sequencer1.open();
sequencer.setSequence(sequence);
sequencer1.setSequence(sequence1);
// Start playing the first sequence
sequencer.start();
} // end of try
catch (IOException e) {
} catch (MidiUnavailableException e) {
} catch (InvalidMidiDataException e) {
}
Theframe = new QuestionsFrame[9];
***** this is where I pass the sequence, it is the first parameter
Theframe[0] = new
QuestionsFrame(aSequencer,"start","mariotitle.jpg",1);
Theframe[0].addWindowListener(new QuestionsFrameListener(1));
Theframe[0].setBackground(Color.blue);
Theframe[0].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Theframe[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Theframe[0].setVisible(true);
}
// WindowListener to tell main application when a frame closes
static class QuestionsFrameListener extends WindowAdapter{
int questionNumber;
// constructor taking the question number
QuestionsFrameListener(int questionNumber) {
this.questionNumber = questionNumber;
}
// when frame closes, notify the main application method
@Override
public void windowClosed(WindowEvent e) {
QuestionFrameClosed(questionNumber);
}
//end of QuestionsFrameListener
public void QuestionFrameClosed(int questionNumber) {
// display next frame if not finished yet
if (questionNumber <= mario1.Theframe.length) {
mario1.Theframe[questionNumber].setVisible(true);
//Theframe[questionNumber].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else {
// no more questions
}
}// end of questionFrameClosedMethod
}
public static QuestionsFrame Theframe[] = new QuestionsFrame[9];
public String theSequence;
public Sequencer aSequencer;
public Sequencer aSequencer1;
public static int questionNumber;
} // end of mario1
class QuestionsFrame extends JFrame
{
***** This is one constructor, NOT the one that is called above
public QuestionsFrame(Sequencer extSequencer,String anIcon,String q,String b1,String b2,String b3,String b4,int a)
{
//this.frame = frame;
// this.counter = counter;
answer = a;
theicon = anIcon;
this.aSequencer = extSequencer;
//Theframe = Theframe;
// Question = q;
Question = q;
B1 = b1;
B2 = b2;
B3 = b3;
B4 = b4;
//size the frame
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
cp = getContentPane();
cp.setBackground(Color.white);
//create Icon object and set its location
ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" + theicon);
// ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
JButton button1 = new JButton(new AnswerAction(B1,answer ==1,questionNumber)); //create button
JButton button2 = new JButton(new AnswerAction(B2,answer ==2,questionNumber));
JButton button3 = new JButton(new AnswerAction(B3,answer==3,questionNumber));
JButton button4 = new JButton(new AnswerAction(B4,answer==4,questionNumber));
ImagePanel = new JPanel();
ImagePanel.setBackground(Color.white);
QuestionPanel = new JPanel();
// QuestionPanel.setBackground(Color.BLUE);
add(ImagePanel,BorderLayout.NORTH);
newLabel = new JLabel();
newLabel1 = new JLabel();
//add the icon to the label
// newLabel.setLocation(29,40);
// newLabel1.setLocation(80,80);
newLabel.setPreferredSize((new Dimension(600,450)));
newLabel.setIcon(icon);
// newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
// newLabel1.setIcon(icon1);
ImagePanel.add(newLabel);
ImagePanel.add(newLabel1);
QuestionPanel.add(button1); // adding button to frame
QuestionPanel.add(button2);
QuestionPanel.add(button3);
QuestionPanel.add(button4);
add(QuestionPanel,BorderLayout.SOUTH);
QuestionLabel = new JLabel(Question,JLabel.CENTER);
add(QuestionLabel,BorderLayout.CENTER);
}//end of QuestionFrame Contstructor
**** Here is the other, note this IS the one that is called above
public QuestionsFrame(Sequencer theSequencer,String b1,String anIcon,int a)
{
// this.frame = frame;
// this.counter = counter;
theicon = anIcon;
aSequencer = theSequencer;
answer = a;
B1 = b1;
//size the frame
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
cp = getContentPane();
cp.setBackground(Color.white);
//create Icon object and set its location
ImageIcon icon = new ImageIcon("C:\\users\\Matt\\Documents\\" +theicon);
// ImageIcon icon1 = new ImageIcon("C:\\users\\Matt\\Documents\\mario1.gif");
JButton button = new JButton(new AnswerAction(B1,answer==1,questionNumber)); //create button
ImagePanel = new JPanel();
ImagePanel.setBackground(Color.white);
QuestionPanel = new JPanel();
// QuestionPanel.setBackground(Color.BLUE);
add(ImagePanel,BorderLayout.NORTH);
newLabel = new JLabel();
newLabel1 = new JLabel();
//add the icon to the label
// newLabel.setLocation(29,40);
// newLabel1.setLocation(80,80);
newLabel.setPreferredSize((new Dimension(650,500)));
newLabel.setIcon(icon);
// newLabel.setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
// newLabel1.setIcon(icon1);
ImagePanel.add(newLabel);
ImagePanel.add(newLabel1);
QuestionPanel.add(button); // adding button to frame
add(QuestionPanel,BorderLayout.SOUTH);
QuestionLabel = new JLabel(Question,JLabel.CENTER);
add(QuestionLabel,BorderLayout.CENTER);
}//end of QuestionFrame Contstructor
public void theQuestions( int questionNumber){
***** This is where I try to stop the sequence. This is where the null pointer is
if(questionNumber==1){
aSequencer.stop();
mario1.Theframe[1] = new QuestionsFrame(aSequencer1,"smwbullet.jpg","What is the name of this enemy","Banzai Bill","Blurp","Bullet Bill","Goomba",1);
mario1.Theframe[1].addWindowListener(new mario1.QuestionsFrameListener(2));
// mario1.Theframe[1].setBackground(Color.yellow)
mario1.Theframe[1].QuestionPanel.setBackground(Color.yellow);
mario1.Theframe[1].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mario1.Theframe[1].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mario1.Theframe[1].setVisible(true);
this.aSequencer1.start();
}
if(questionNumber==2){
// mario1.Theframe[1].sequencer.stop();
mario1.Theframe[2] = new QuestionsFrame(null,"","this is the second question","Banzai Bill","Blurp","Bullet Bill","Goomba",1);
mario1.Theframe[2].addWindowListener(new mario1.QuestionsFrameListener(3));
//mario1.Theframe[2].setBackground(Color.yellow);
mario1.Theframe[2].QuestionPanel.setBackground(Color.green);
mario1.Theframe[2].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mario1.Theframe[2].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mario1.Theframe[2].setVisible(true);
}
if(questionNumber==3){
mario1.Theframe[3] = new QuestionsFrame(null,"","this is the third question"," "," "," "," ",4);
mario1.Theframe[3].addWindowListener(new mario1.QuestionsFrameListener(4));
mario1.Theframe[3].setBackground(Color.yellow);
mario1.Theframe[3].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mario1.Theframe[3].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mario1.Theframe[3].setVisible(true);
}
if(questionNumber==4){
mario1.Theframe[4] = new QuestionsFrame(null,"","this is the fourth question"," "," ","","",2);
mario1.Theframe[4].addWindowListener(new mario1.QuestionsFrameListener(5));
mario1.Theframe[4].setBackground(Color.yellow);
mario1.Theframe[4].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mario1.Theframe[4].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mario1.Theframe[4].setVisible(true);
}
}
public static int getAnswer(int answer){
return answer;
}//end of getAnswer method
class AnswerAction extends AbstractAction {
private boolean isCorrect;
public AnswerAction(String name, boolean isCorrect, int questionNumber) {
super(name);
this.isCorrect = isCorrect;
mario1.questionNumber = questionNumber;
} //end of AnswerAction
public void actionPerformed(ActionEvent e) {
if (isCorrect) {
mario1.Theframe[mario1.questionNumber].setVisible(false);
theQuestions(questionNumber+=1);
System.out.println("Correct");
//questionNumber++;
}
else {
//loseChime();
// QuestionsFrame.incorrectAnswer(); // .. - you could pass the answer string if required
}
// or possibly: questionManager.handleAnswer(isCorrect);
} //end of ActionPerformed
//public int questionNumber;
} //end of AnswerAction class
//instance variables
public JPanel QuestionPanel;
public JPanel ImagePanel;
public JLabel QuestionLabel;
public JLabel newLabel;
public JLabel newLabel1;
public String theicon;
// public static QuestionsFrame Theframe[] = new QuestionsFrame[9];
// public int theFrame;
public String theSequence;
public static int questionNumber;
// public QuestionsFrame frame;
public int counter;
public int answer;
public String Question;
public String B1,B2,B3,B4;
// public Sequence sequence;
//public Sequence sequence1;
// / public Sequencer sequencer;
public Sequencer aSequencer;
public Sequencer aSequencer1;
public Container cp;
public static final int DEFAULT_WIDTH = 1000;
public static final int DEFAULT_HEIGHT = 700;
} //end of QuestionFrame class