Results 1 to 5 of 5
  1. #1
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default Help with null pointer exception

    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.

    Java 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(){ 
        [COLOR="Red"] ****** This is where the Sequence is created   [/COLOR]
            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];
    [COLOR="Red"] ***** this is where I pass the sequence, it is the first parameter[/COLOR]       
              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;
            [COLOR="Red"]public static   Sequencer aSequencer;
            public static   Sequencer  aSequencer1;[/COLOR]
                    
            public static int questionNumber;
      
        } // end of mario1
        
     
    class QuestionsFrame extends JFrame
    {
    [COLOR="Red"]***** This is one constructor, NOT the one that is called above [/COLOR]       
        public QuestionsFrame(Sequencer theSequencer,String anIcon,String q,String b1,String b2,String b3,String b4,int a)
        {
            
           //this.frame = frame;
          // this.counter = counter;
           answer = a;
           theicon = anIcon;
           mario1.aSequencer1 = theSequencer;
           //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
        
           
          
           
            
        
        
     [COLOR="Red"]**** Here is the other, note this IS the one that is called above[/COLOR]   
         public QuestionsFrame(Sequencer theSequencer,String b1,String anIcon,int a)
        {
          // this.frame = frame;
        //   this.counter = counter;
           theicon = anIcon;
         [COLOR="Red"]  mario1.aSequencer = theSequencer;[/COLOR]
           
           
           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){
            
           
    
             
      [COLOR="Red"] ***** This is where I try to stop the sequence.  This is where the null pointer is      [/COLOR]
            
            if(questionNumber==1){
         [COLOR="Red"]   mario1.aSequencer.stop();[/COLOR]
            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 Container cp;
        public static final int DEFAULT_WIDTH = 1000;
        public static final int DEFAULT_HEIGHT = 700;
       
        
        
        
    } //end of QuestionFrame clas

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Where do you ever initialize aSequencer? So if you never initialize it, it will of course be null when you try to use it.

    Also, you would probably like to get rid of all those statics as the only thing that should be static here is the main method and nothing more.

  3. #3
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    I thought it is being initialized in the second constructor, of class QuestionsFrame?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gammaman View Post
    I thought it is being initialized in the second constructor, of class QuestionsFrame?
    Post the line(s) of code that does this, and it will answer both our questions.

  5. #5
    gammaman is offline Member
    Join Date
    Dec 2007
    Posts
    31
    Rep Power
    0

    Default

    Never mind, I am a complete asshole!!! I tried to pass the sequencer to the object while outside the try statement. It if fixed now.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  3. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 03:17 PM
  4. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 04:30 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •