Applet java

Fermé
miloud1990 Messages postés 22 Date d'inscription lundi 9 janvier 2012 Statut Membre Dernière intervention 16 mai 2017 - 12 févr. 2013 à 10:50
KX Messages postés 16755 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 12 février 2025 - 12 févr. 2013 à 14:42
Bonjour,

j'ai réaliser une applet java qui permet d'afficher une webcam sous applet mais j'ai une petit erreur pas le corrigé quelqu'un peut m'aider .merci
voila code source:


import java.awt.*;
import java.applet.*;
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class Applet1 extends Applet implements Runnable
{
Thread tcam;
MediaTracker track = new MediaTracker(this);
Image i;
public void init()
{
}
public void start (){
if(tcam==null){
tcam=new Thread(this);
tcam.start ();
System.out.println("Start Cam()");
}
}
public void stop (){
if(tcam!=null){
tcam=null;
tcam.stop ();
System.out.println("Stop Cam()");
}
}
public void run(){
while(true){
repaint();
try{
tcam.sleep(1000);//pause de 1 seconde
System.out.println("Refresh Cam()");
}catch(InterruptedException e) {
}
}
}
public void paint(Graphics g){
i = getImage(getCodeBase(),"picture.jpg");
track.addImage(i,0); //0 était sencé être l'indice du tableau
try {
track.waitForID(0); //le programme attends le complet chargement de l'image
} catch (InterruptedException e) { }
if((i.getWidth(this)==320) & (i.getHeight(this)==240))
{
g.drawImage(i,0,0,this);
}
i.flush();
}
public void update(Graphics g){
paint(g);
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the <PARAM NAME="label" VALUE="some string">,
* <PARAM NAME="background" VALUE="rrggbb">,
* and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
/**
* External interface used by design tools to show properties of an applet.
*/
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
/**
* Intializes values for the applet and its components
*/
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
}



voici l'erreur quand fait run:

java.lang.NullPointerException
at Applet1.stop(Applet1.java:28)
at sun.applet.AppletPanel.run(AppletPanel.java:535)
at java.lang.Thread.run(Thread.java:722)


A voir également:

1 réponse

KX Messages postés 16755 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 12 février 2025 3 020
12 févr. 2013 à 14:42
Tu ne peux pas appeler de méthode sur une référence null.
L'erreur est indiqué ton message : at Applet1.stop(Applet1.java:28)
Ce qui correspond à ce code :

public void stop (){
    if(tcam!=null){
        tcam=null;
        tcam.stop();
        System.out.println("Stop Cam()");
    }
}

Il est évident que tcam vaut null à cet endroit vu que tu lui affectes cette valeur à la ligne d'avant !

Il est à mon avis inutile de mettre tcam à null, on peut faire autrement en utilisant les méthode de la classe Thread. Au passage, stop() est une méthode dépréciée, il faudrait la remplacer par interrupt(), ce qui permet donc par la suite de faire des tests avec isInterrupted()

@Override
public void stop ()
{
    if(!tcam.isInterrupted())
    {
    	tcam.interrupt();
    	System.out.println("Stop Cam()"); 
    } 
}
0