La référence d'objet n'est pas définie à une instance d'un objet
bonjour,
voila j ai repri le tp trouver sur internert, un formulaire doit etre complter pour créer une jungle representer par des rond circulaire dans un panel lorsque on appuie sur le bouton go.Mais voila j obtiens un message d'erreur:La référence d'objet n'est pas définie à une instance d'un objet a cette ligne:
laJungle.dessiner(objGraphics);.
Et quand j i donc instancier l objet graphic: objGraphics=new graphics mais maintenant il me dit de mettre un constructeur.
Mais je ne sais pas vraiment ou le placer voici le script du formulaire sous visual studio c#:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Proj_jungle
{
public partial class frmJungle : Form
{//objet de la classe Graphics, permettant de dessiner la jungle
jungle laJungle;
Graphics objGraphics;
public frmJungle()
{
InitializeComponent();
objGraphics = picbJungle.CreateGraphics();
pnAjoutCarni.Enabled = false;
pnAjoutHerbivore.Enabled = false;
}
private void btnOk_Click(object sender, EventArgs e)
{
if (txtPays.Text != "")
{//instanciation et initialisation de l'objet jungle
//laJungle = new jungle(txtPays.Text);
laJungle = new jungle();
laJungle.init(txtPays.Text);
pnJungle.Enabled = false;
pnAjoutCarni.Enabled = true;
pnAjoutHerbivore.Enabled = true;
}
}
private void btnajoutcarni_Click(object sender, EventArgs e)
{
animal animal1;
carnivore carni1 = new carnivore();
if (txtajoutespececarni.Text != "" && txtAjoutNomCarni.Text != "")
{//instanciation et initialisation de l'objet animal
//animal1 = new animal(txtNom.Text, txtEspece.Text);
animal1 = new animal();
//animal1.init(txtNom.Text, txtEspece.Text);
carni1.init(txtAjoutNomCarni.Text, txtajoutespececarni.Text, 10);
//carni1.affiche();
animal1 = carni1;
animal1.affiche();
txtajoutespececarni.Text = "";
txtAjoutNomCarni.Text = "";
txtAjoutNomCarni.Focus();
laJungle.ajouterAnimal(carni1);
}
}
private void btnGo_Click(object sender, EventArgs e)
{
pnAjoutCarni.Enabled = false;
pnAjoutHerbivore.Enabled = false;
laJungle.dessiner(objGraphics);
//afficher les infos des animaux
laJungle.affiche();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{// à chaque exécution de cette procédure, on fait évoluer les animaux (ils vieillissent)
laJungle.evoluer();
laJungle.affiche();
laJungle.dessiner(objGraphics);
}
private void btnAjoutHerbi_Click(object sender, EventArgs e)
{
animal animal1;
herbivore carni1 = new herbivore();
if (txtajoutespeherbi.Text != "" && txtajoutnomherbi.Text != "")
{//instanciation et initialisation de l'objet animal
//animal1 = new animal(txtNom.Text, txtEspece.Text);
animal1 = new animal();
//animal1.init(txtNom.Text, txtEspece.Text);
carni1.init(txtajoutnomherbi.Text, txtajoutespeherbi.Text, 10);
//carni1.affiche();
animal1 = carni1;
animal1.affiche();
txtajoutespeherbi.Text = "";
txtajoutnomherbi.Text = "";
txtajoutnomherbi.Focus();
laJungle.ajouterAnimal(carni1);
}
}
}
} |
et voici la methode pour dessiner:
Code:
1 2 3 4 5 6 7 8 9 10
| public void dessiner(Graphics MonGr)
{
MonGr.Clear(Color.Transparent);
for (int i = 0; i < _lesAnimaux.Count; i++)
{
_lesAnimaux[i].dessiner(MonGr);
}
} |