Problème null pointer exception dans une applet graphique

Contenu du snippet

Source / Exemple :

//package stage2;
   import javax.swing.*;
   import java.awt.geom.*;
   import java.applet.*;
   import java.io.*;
   import java.net.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;
   import java.awt.BorderLayout;
   import java.lang.*;
   import java.math.*;
   import java.awt.Graphics;
   import java.lang.Object.*;
   import java.util.Timer;


   public class Pisteb990 extends Applet implements  ActionListener, Runnable
   
   
   {
   
   
      Color couleur,couleur1;
      Font f=new Font("TimesRoman", Font.BOLD, 12);
      int indexPiste, indexVitesse,vitesse,indexSuite, depart, count;
      int reste, countb,valeur,pas,hauteur,longueur,largeur,vit, x, i,j, indice_a , indice_b , indice_c , indice_d ,fin;
      int m,n,t1,t2,pst1_a,pst1_b ,pst2_a,pst2_b,pst3_a,pst3_b ,pst4_a,pst4_b;
      short tmp, maxi=0,mini=3000;
      float val1, val2, val3, val4 ;
      float el_1,el_2,el_3,el_4;
      boolean debut, effacer, suivant, precedent;
      int xh[],yh[],xv[],yv[];
      String s, max, min, st,piste,pression,suite,fichier;
     	//int tempsmaxi=3600;
      Thread thread;
      Image img;
      Graphics g;
      TextArea messageBox = new TextArea();
   
      Vector piste1,piste2,piste3,piste4,t, courbe1, courbe2,courbe3,courbe4 ;
   
      URL u ;
   
      InputStream flux;
      DataInputStream data;
      BorderLayout borderLayout2 = new BorderLayout();
      Choice choixFichier = new Choice();
      Choice choixSuite = new Choice();
      Panel p1 = new Panel();
      Choice choixPression = new Choice();
      Scrollbar js2 = new Scrollbar();
      Panel p2 = new Panel();
      BorderLayout borderLayout1 = new BorderLayout();
      Panel p3 = new Panel();
      Button initialiser = new Button();
      Button avancer = new Button();
      Button stop = new Button();
      Button pause = new Button();
      Choice choixVitesse = new Choice();
      Choice choixPiste = new Choice();
      FlowLayout flowLayout1 = new FlowLayout();
   	//Dialog tailleTemps = new Dialog(this,"taille du vector temps = ";true);
   
   //***************************************************************
   //**** METHODE INIT()********************************************
   //***************************************************************
      public void init()
      {
      
      
      
         couleur = new Color(200, 255, 255);
         couleur1= new Color(220,220,220);
         largeur = getSize().width;
         hauteur = getSize().height;
      
         img     = createImage(largeur,hauteur);
         g       = img.getGraphics();
         setBackground(couleur);
      
      ////////////// création du 1er Pannel avec Listes déroulantes //////////////////////////////////////////////
      
         Panel p1 = new Panel();
      
      
         choixVitesse = new Choice();
         choixVitesse.add("Vitesse");
         choixVitesse.add("Lent");
         choixVitesse.add("Moyen");
         choixVitesse.add("Rapide");
         vitesse = choixVitesse.getSelectedIndex();
      
      
         choixPiste  = new Choice();
         choixPiste.add("Piste");
         choixPiste.add("Piste1");
         choixPiste.add("Piste2");
         choixPiste.add("piste3");
         choixPiste.add("piste4");
         piste = choixPiste.getSelectedItem();
      
      
         choixPression = new Choice();
         choixPression.add("Pression");
         choixPression.add("Bar");
         choixPression.add("mm Hg");
         pression  = choixPression.getSelectedItem();
      
      
         choixFichier = new Choice() ;
         choixFichier.add("DA030410");
         choixFichier.addItem("fich1.dat");
         choixFichier.addItem("fich2.dat");
         fichier = choixFichier.getSelectedItem();
      
      
         choixSuite = new Choice();
         choixSuite.add("Suite ?");
         choixSuite.add("effacer");
         choixSuite.add("début");
         choixSuite.add("suivant");
         choixSuite.add("précedent");
         suite  = choixSuite.getSelectedItem();
      
         p1.setBackground(Color.orange);
         p1.setLayout(flowLayout1);
         choixVitesse.setBackground(Color.green);
         choixFichier.setBackground(Color.cyan);
         choixSuite.setBackground(Color.yellow);
         this.setBackground(Color.lightGray);
      
         this.setLayout(borderLayout2);
         this.add(p1, BorderLayout.NORTH);
         p1.add(choixPiste, null);
         p1.add(choixVitesse, null);
         p1.add(choixPression, null);
         p1.add(choixSuite, null);
         p1.add(choixFichier, null);
      
      
      
      
      ////////// création du 2ème  Panel  avec scrollBar , qui doit accueillir le graphe des mesures ////////////////////////
      
         Panel p2 = new Panel();
         Scrollbar js2 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
         this.add(p2,BorderLayout.EAST);
         p2.add(js2,BorderLayout.EAST);
      
      
      
      
      //////// création du 3ème panel avec Boutons de commandes de défilement ///////////////////////////////////////////
      
         Panel p3 = new Panel();
         this.add(p3,BorderLayout.SOUTH);
         p3.setBackground(Color.lightGray);
      
         stop = new Button("STOP");
         stop.setBackground(Color.red);
         stop.setLabel("STOP");
         stop.addActionListener(this);
         p3.add(stop,null);
      
      
         initialiser = new Button("RESET");
         initialiser.setBackground(Color.cyan);
         initialiser.setLabel("RESET");
         initialiser.addActionListener(this);
         p3.add(initialiser,null);
      
         avancer = new Button("START");
         avancer.setBackground(Color.green);
         avancer.setLabel("START");
         avancer.addActionListener(this);
         p3.add(avancer,null);
      
         pause = new Button("PAUSE");
         pause.setBackground(SystemColor.inactiveCaptionText);
         pause.setLabel("PAUSE");
         pause.addActionListener(this);
         p3.add(pause,null);
      
      
         p1.setVisible(true);
         p2.setVisible(true);
         p3.setVisible(true);
      
      
         Vector courbe1 = new Vector();
         Vector courbe2 = new Vector();
         Vector courbe3 = new Vector();
         Vector courbe4 = new Vector();
      
         Vector piste1 = new Vector();
         Vector piste2 = new Vector();
         Vector piste3 = new Vector();
         Vector piste4 = new Vector();
         Vector t      = new Vector(3600);
         Timer timer = new Timer();
         TimerTask task = new MaTask();
      
      
      
         xh=new int[50];
         yh=new int[50];
         xv=new int[50];
         yv=new int[50];
      
         indice_a =0;
         indice_b =0;
         indice_c =0;
         indice_d =0;
      
         xh[0]=40;
         yv[0]=0;
         fin = 0;
         pas=3;
         depart=0;
         count=520*pas;
      
      
         Integer a = new Integer(indice_a) ;
         courbe1.addElement(a);
         Integer b = new Integer(indice_b) ;
         courbe2.addElement(b);
         Integer c = new Integer(indice_c) ;
         courbe3.addElement(c);
         Integer d = new Integer(indice_d) ;
         courbe4.addElement(d);
      
      
      
         debut=true;
         effacer=true;
         reste=0;
         countb=0;
      
      
      
      }
   
   //***************************************************************************************************************************
   //***************************************************************************************************************************
   
   
   
   
   
   
   
   //************************************************************************************************************
   //**** METHODE ACTION PERFORMED(ACTION EVENT E)***************************************************************
   //************************************************************************************************************
   //************************************************************************************************************
   
   
   
   
      public void actionPerformed(ActionEvent e)
      {
      
      
         if((e.getActionCommand()=="RESET"))
         {
            depart=0;
            count=520*pas;
            debut=false;
            effacer=true;
         }// fin 1er if
      
      
         if((e.getActionCommand()=="STOP"))
         {
            effacer=false;
            debut=false;
            this.stop();
         }// fin 2ème if
      
      
      
      
      //**********************************************************************************************************
      
      
      
         if((e.getActionCommand()=="START"))
         {
         //Timer timer = new Timer();
         //TimerTask task = new MaTask();
         //timer.schedule( task, 1000, 500 );
         
            debut=true;
            effacer=true;
         //tt.scheduleAtFixedRate(thread.run(),0,1000) ;
         //ReadBits rb = new ReadBits();
            this.start();
         
         //for(i=1;i<t.size();i++)
            for(i=1;i<=3600;i++)
            {
               float var = i/pas+40;
               Float wt = new Float(var);
               Integer newI = new Integer(i);
               t.set(i,wt);// problème "NULL POINTER EXCEPTION" à l'éxécution de l'applet 
            
               try
               {
                  URL u = new URL(getDocumentBase(), fichier);
                  flux = u.openStream();
                  data = new DataInputStream(flux);
                  valeur=0;
               
                  try
                  {
                  	 // boucle continue
                     while (true)
                     {
                     
                        valeur = data.readUnsignedShort();//  marche  puisque readUnsignedShort retourne "an int value"
                        Float wa = new Float(valeur);
                        reste= countb%4;
                     
                        switch(reste)
                        {
                           case 0 :
                              Integer nexta = new Integer(indice_a++);
                              courbe1.set(indice_a++,wa);
                              break;
                        
                           case 1 :
                              Integer nextb = new Integer(indice_b++);
                              courbe2.set(indice_b++,wa);
                              break;
                        
                           case 2 :
                              Integer nextc = new Integer(indice_c++);
                              courbe3.set(indice_c++,wa);
                              break;
                        
                           case 3 :
                              Integer nextd = new Integer(indice_d++);
                              courbe4.set(indice_d++,wa);
                              break;
                        }// fin switch
                     
                        countb++;
                        break;
                     }//fin while
                  //this.start();
                  }// fin 2ème try
                  
                  
                     catch(EOFException e1)
                     {
                        flux.close();
                     }//fin 2ème catch
               
               }//fin  1er try
               
                  catch(IOException e1)
                  {
                     System.out.println("Erreur --"+e1.toString());
                  }// fin 1er catch
            
               longueur = countb/4;
            
            }//fin 1er for
         
         //****************************************************************************************************************
            for(j=0;j<piste1.size();j++)
            {
            
               float el1 = ((Float)courbe1.elementAt(j)).floatValue();
               val1 = 180-(el1-2000)/5+40 ;
               Float v1 = new Float(val1);
               piste1.addElement(v1);
            }
         
            for(j=0;j<piste2.size();j++)
            {
               float el2 = ((Float)courbe2.elementAt(j)).floatValue();
               val2 = 180-(el2-2000)/5+60 ;
               Float v2 = new Float(val2);
               piste2.addElement(v2);
            }
         
            for(j=0;j<piste3.size();j++)
            {
               float el3 = ((Float)courbe3.elementAt(j)).floatValue();
               val3 = 180-(el3-2000)/5+180 ;
               Float v3 = new Float(val3);
               piste3.addElement(v3);
            }
         
            for(j=0;j<piste4.size();j++)
            {
               float el4 = ((Float)courbe4.elementAt(j)).floatValue();
               val4 = 180-(el4-2000)/5+300 ;
               Float v4 = new Float(val4);
               piste4.addElement(v4);
            }
         
         
         
         
         
         
         
         
         
         
         } //fin de if (getSource()==avancer)
      
      }//fin de actionPerformed()
   
   
   
   
   
   
   /*******************************************************************************
   ************* METHODE PAINT (GRAPHICS G) *****************************************
   *******************************************************************************/
   
   
   
   
      public void paint(Graphics g1)
      {
      
         indexPiste   = choixPiste.getSelectedIndex();
         indexVitesse = choixVitesse.getSelectedIndex();
         indexSuite   = choixSuite.getSelectedIndex();
        //Timer timer = new Timer();
        //TimerTask task = new MaTask();
        //timer.schedule( task, 1000, 500 );
      
         switch(indexVitesse)
         {
            case 1:
               vit=30;
               break;
            case 2:
               vit=100;
               break;
            case 3:
               vit=200;
               break;
            case 0:
               vit=100;
               break;
         }//fin switch(indexVitesse)
      
      
      
      
         switch(indexSuite)
         {
            case 1:// le cas ou indexSuite ="effacer";
            
               g.clearRect(0,0,largeur,hauteur);//efface et réinitialise
               g.setColor(couleur);
            
               g.fillRect (1, 1, largeur-2, hauteur-2);
               g.setColor(couleur1);
            
               for(i=1;((xh[i-1]+20)<560);i++)   //pour graduer l'axe des x
               {
                  xh[i]=xh[i-1]+40;
                  g.drawLine(xh[i],0,xh[i],480);
                //		g.setColor(Color.blue);
                  //g.drawLine(795,497,800,560);      //les extremites des axes: les fléches pour l'axe des x
               //    g.drawLine(795,503,800,560);
               	//g.setColor(Color.red);
               }// fin 1er for
            
            
               for(i=1;((yv[i-1]+20)<560);i++)  // pour  graduer l'axe des y
               {
                  yv[i]=yv[i-1]+40;
                  g.drawLine(40,yv[i],560,yv[i]);
               	//	  g.setColor(Color.blue);
                  g.drawLine(37,5,40,0);             // les extremites des axes: les fléches pour l'axe des y
                  g.drawLine(43,5,40,0);
               // g.setColor(Color.red);
               
               }// fin 2ème for
            
               g.setColor(Color.red);
               g.drawLine(40,480,560,480);   //axe horizontal des x dans le repère
               g.drawLine(40,480,40,0);      // axe vertical des y  dans le repère
               g.drawString("(s)",545,495);
               g.drawString(String.valueOf(pas*2)+"s",90,78);
               g.drawString("200",60,65);
               g.drawLine(80,80,120,80);
               g.drawLine(80,80,120,80);
               effacer = false;
            
               break;
         
         
         
         
            case 2 :	// le cas ou : indexSuite = "debut" ;
            
            //g.clearRect(0,0,largeur,hauteur);
                        //g.setColor(couleur);
            
            	//g.fillRect (1, 1, largeur-2, hauteur-2);
            	//g.setColor(couleur1);
            	//effacer=false;
                         //g.setColor(Color.cyan);
            
               g.drawString("/"+String.valueOf(longueur/1200)+"min"+String.valueOf((longueur/20)%60)+"s",500,40);
            	// -> ???
            
              	//for(int i=0;i<=t.size();i++)
               for(int i=1;i<=3600;i++)
               {
                  t1 = ((Integer)t.elementAt(i)).intValue();//!!!le vector t est vide !!!puisqu'aucun temps ne lui est passé!!!
               //t1 = ((Integer)(timer)).intValue();
                  t2 = ((Integer)t.elementAt(i+1)).intValue();
                  //t2 = ((Integer)(timer)).intValue();
                  messageBox.append("valeur de  t1 = "+t1);
               }
            
               for(int j=0;j<piste1.size();j++)
               {
                  pst1_a = ((Integer)piste1.elementAt(j)).intValue();
                  pst1_b = ((Integer)piste1.elementAt(j+1)).intValue();
               }
            
               for(int j=0;j<piste2.size();j++)
               {
                  pst2_a = ((Integer)piste2.elementAt(j)).intValue();
                  pst2_b = ((Integer)piste2.elementAt(j+1)).intValue();
               }
            
               for(int j=0;j<piste3.size();j++)
               {
                  pst3_a = ((Integer)piste3.elementAt(j)).intValue();
                  pst3_b = ((Integer)piste3.elementAt(j+1)).intValue();
               }
            
               for(int j=0;j<piste4.size();j++)
               {
                  pst4_a = ((Integer)piste4.elementAt(j)).intValue();
                  pst4_b = ((Integer)piste4.elementAt(j+1)).intValue();
               }
            
               g.setColor(Color.blue);
            
            	//switch(indexPiste)
            	  //     {
            	  //    	case 1:
               for(i=0,j=0;(i<count-1)&&(i<longueur);i++,j++)
               {
                  g.drawLine(t1,pst1_a,t2,pst1_b); // g.drawLine(x1,y1,x2,y2)
               
                  g.setColor(Color.blue);
               }
            	   //      break;
             		  // 	case 2:
               for( i=0, j=0; (i<count-1)&&(i<longueur); i++, j++)
               {
                  g.drawLine(t1,pst2_a,t2,pst2_b);
               
                  g.setColor(Color.blue);
               }
            	    //      break;
              		 // 	case 3:
               for( i=0, j=0; (i<count-1)&&(i<longueur); i++, j++)
               {
                  g.drawLine(t1,pst3_a,t2,pst3_b);
               
                  g.setColor(Color.blue);
               }
            			//	break;
            	 		//  case 4:
               for( i=0, j=0; (i<count-1)&&(i<longueur); i++, j++)
               {
                  g.drawLine(t1,pst4_a,t2,pst4_b);
               
                  g.setColor(Color.blue);
               }
            	 		//  break;
            
            		//	}// fin 2ème  switch(indexPiste)
            
            
            
            
            case 3: //le cas indexSuite = "suivant";
               g.clearRect(0,0,largeur,hauteur);
               g.setColor(couleur);
               g.fillRect (1, 1, largeur-2, hauteur-2);
               effacer=false;
               break;
            case 4: //le cas indexSuite = "precedent";
               g.clearRect(0,0,largeur,hauteur);
               g.setColor(couleur);
               g.fillRect (1, 1, largeur-2, hauteur-2);
               effacer=false;
            
               break;
         
         
         
         }// fin 1er switch(indexSuite)
      
      
         g1.setFont(f);
      
      
      
      //////
         for(int k=0; k<7; k++)//...????????????
         {
            g.drawString(String.valueOf(4*pas*k+depart/20), 40+k*80-5, 495);
            min=String.valueOf(((520*pas+depart)/20)%60);
            s=String.valueOf(((520*pas+depart)/20)%60);
            g.setColor(Color.black);
            g.drawString(min+"min"+s,458,40);
            g.setColor(Color.blue);
         
            if(longueur-count > 20)
            {
               count += 20;
               depart +=20;
            }// fin if
            
            
            else
            {
               count += longueur-count;
               depart += longueur-count;
            //}// fin else
            
               effacer=true;
            }
         }//fin for
      
         g1.drawImage(img,0,0,this);
      }
   
   //**************************************************************************************************************************
   //**************METHODE  UPDATE(GRAPHICS)*****************************************************************************************************
   //***************************************************************************************************************************
   
   
      public void update(Graphics g1)
      {
         paint(g1);
      }
   
   
   /*******************************************************************************
   ************METHODE START******************************************************
   ******************************************************************************/
      public void start()
      {
         if (thread==null)
         {
            thread=new Thread(this);
            thread.start();
         }
      }
   /*******************************************************************************
   ********************METHODE STOP ***********************************************
   *******************************************************************************/
      public void stop()
      {
      
      
         thread=null;
      
      }
   /****************************************************************************
   ********************** METHODE RUN ******************************************
   ****************************************************************************/
      public void run()
      {
         Thread thisThread=Thread.currentThread();
         while((thread==thisThread)&&(thread!=null))
         {
            repaint();
            try
            {
               Thread.sleep(vit);
            }
               catch(InterruptedException e)
               {
                  if(count==longueur)
                  {
                     count=0;
                  }
               }
         }
      }
   
   //****************************************************************************************************************
   //**************** CONVERSION / INVERSION   DES BITS LUS (2 A 2 ) ****************************************************
   //*******************************************************************************************************************
   /*
   public void conversion()
   {	int m,n;
   short tmp, maxi=0,mini=0;
   
   URL u = new URL(getDocumentBase(), fichier);
   flux = u.openStream();
   
   Vector f1  = new Vector();
   Vector f2  = new Vector();
   
   DataInputStream data1  = new DataInputStream(flux);
   //valeur1 = data1.readUnsignedShort();
   Short wa1 = new Short(valeur);
   //***************************************************************************************
   			//DataInputStream data2 = new DataInputStream(flux);
   			//valeur2 = data2.readUnsmgnedShort();
   			//Float wa2 = new Float(valeur2);
   
   for(m=0;m<f1.size();m+=2)
   {for(n=0;n<f2.size();n+=2)
   	{
   	f1.set(m,wa1);
   	//f2.set(n,wa2);
   	//f2.elementAt(n)=f1.elementAt(m+1);
   	//f2.elementAt(n+1)=f1.elementAt(m);
   	//Short fs1 = new Short(f1);
   	//Short fs2= new Short(f2);
   	short shf1a = ((short)(f1.elementAt(m)).shortValue());
   	short shf1b = ((short)(f1.elementAt(m+1)).shortValue());
   	short shf2a = ((short)(f2.elementAt(n)).shortValue());
   	short shf2b = ((short)(f2.elementAt(n+1)).shortValue());
   	shf2a=shf1b;
   	shf1a=shf2b;
   	if(maxi<shf1b)
   		{
   		maxi=shf1b;
   		}
   
   	if((shf1a!=0)&&(mini>shf1a))
   		{
   		mini=shf1a;
   		}
   }//fin du 1erfor
   }//fin du 2ème for
   /*try
   {
   f2.exists();
   }
   catch{
   	System.out.println("impossible d'ouvrir le fichier f2");
   }//fin catch
   
   }//fin methode void convers()
   
   
   */
   //******************************************************************************************************************
   //****************************************************************************************************************
   //*******************************************************************************************************************
   
   //}
   
      public class MaTask extends TimerTask
      {
         public void run()
         {
            thread.run();
         }
      }
   }// fin de la classe Pisteb999

//code Pisteb990.html
<html>
  
  <head>
    <title> applet</title>
  </head>
  
  <body>
    <h1>Visualisation: </h1>
    <applet code="Pisteb999.class"  height=500 width=560 align=ABSMIDDLE>ici l'applet
    </applet>
  </body>

</html>

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.