Erreur system.nullReferenceException

isobary Messages postés 13 Date d'inscription lundi 26 juillet 2010 Statut Membre Dernière intervention 6 mai 2013 - 14 févr. 2012 à 11:59
BunoCS Messages postés 15476 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 3 mai 2024 - 14 févr. 2012 à 13:51
bonjour les gars

je suis plante sur ce probleme depuis pret d'une semaine apres avoir fait le tour des forum je n'ai vraiment vu une discution qui traite mon probleme. En fait j'ai cette erreur system.nullreferenceException (la reference d'objet n'est pas definie a une instance). Cela se produit lorsque je clique sur le contenu d'une cellule de mon dataGridView. Voici mon code complet:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Gesbi.Data;
using Gesbi.BD;
using Gesbi.Controleur;
namespace Gesbi.Ihm
{
public partial class LivraisonCmdeFournisseurIhm : Form
{
private List<CmdeFournisseurData> listCmdeNonLivrer = new List<CmdeFournisseurData>();
private List listProCmder = new List();
private List<FournisseurData> listFournisseur = new List<FournisseurData>();
private List livraisonPartielle = new List();
private double montantLivrerCmdde = 0;
private double montantLivrerCmddePro = 0;
private string voirSiCheckBoxValider = "";
private int QteCondiDjaLivrer = 0;
private int QteProDjaLivrer = 0;
public LivraisonCmdeFournisseurIhm()
{
InitializeComponent();
chargerComboFourni(FournisseurBD.afficheFournisseur());
}
private void chargerCombo(List<CmdeFournisseurData> listCmde)
{
this.listCmdeNonLivrer = listCmde;
comboBoxCmdeAconfirmer.Items.Clear();
comboBoxCmdeAconfirmer.Items.Add("Choisir la commande");
for (int i = 0; i < listCmdeNonLivrer.Count; i++)
{
comboBoxCmdeAconfirmer.Items.Add(listCmdeNonLivrer[i].getDateCmdeFourni());
}
comboBoxCmdeAconfirmer.SelectedIndex = 0;
}
private void chargerComboFourni(List<FournisseurData> listFournisseur)
{
this.listFournisseur = listFournisseur;
comboBoxFourni.Items.Clear();
comboBoxFourni.Items.Add("Choisir le fournisseur");
for (int i = 0; i < listFournisseur.Count; i++)
{
comboBoxFourni.Items.Add(listFournisseur[i].getDenominationFourni());
}
comboBoxFourni.SelectedIndex = 0;
}
private void chargerTablo(List listProCmde)
{
this.listProCmder = listProCmde;
dataGridViewCmdeFinal.EndEdit();
dataGridViewCmdeFinal.Rows.Clear();
for (int i = 0; i < listProCmder.Count; i++)
{
dataGridViewCmdeFinal.Rows.Add();
dataGridViewCmdeFinal.Rows[i].Cells[0].Value = listProCmder[i].getConcerneProModel().getCodPro().getLibPro() + "(" + listProCmder[i].getConcerneProModel().getCodModel().getLibModel() + ")";
dataGridViewCmdeFinal.Rows[i].Cells[1].Value = listProCmder[i].getQteCmderCondi();
dataGridViewCmdeFinal.Rows[i].Cells[2].Value = listProCmder[i].getMontantCmderCondi();
dataGridViewCmdeFinal.Rows[i].Cells[5].Value = listProCmder[i].getQteCmderPro();
dataGridViewCmdeFinal.Rows[i].Cells[6].Value = listProCmder[i].getMontantCmderPro();
}
dataGridViewCmdeFinal.EndEdit();
}
private int rechercheValeurList(List listProCmder, string ch)
{
int tr = 0;
for (int i = 0; i < listProCmder.Count; i++)
{
if (listProCmder[i].getNumCondi().getLibCondi().Equals(ch))
{
tr = i;
break;
}
else { continue; }
}
return tr;
}
private double calculMontantLivrer(DataGridView tablo)
{
double montantTotal=0;
for(int i=0;i<tablo.RowCount;i++){
try
{
montantTotal = montantTotal + double.Parse(tablo.Rows[i].Cells[4].Value.ToString()) + double.Parse(tablo.Rows[i].Cells[8].Value.ToString());
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.Message);
montantTotal += 0;
//MessageBox.Show(this, "Une erreur de convertion d'une chaîne en réel s'est produite!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return montantTotal;
}
private void button6_Click(object sender, EventArgs e)
{
this.Close();
}
private void comboBoxCmdeAconfirmer_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxCmdeAconfirmer.SelectedIndex > 0)
{
chargerTablo(CmdeFournisseurBD.afficheCmdeNonLivrerAconfirmer(listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1].getNumCmdeFourni()));
dataGridViewCmdeFinal.ReadOnly = false;
dataGridViewCmdeFinal.Columns[0].ReadOnly = true;
dataGridViewCmdeFinal.Columns[1].ReadOnly = true;
dataGridViewCmdeFinal.Columns[2].ReadOnly = true;
dataGridViewCmdeFinal.Columns[5].ReadOnly = true;
dataGridViewCmdeFinal.Columns[6].ReadOnly = true;
dataGridViewCmdeFinal.Columns[8].ReadOnly = true;
}
else
{
dataGridViewCmdeFinal.Rows.Clear();
}
dataGridViewCmdeFinal.EndEdit();
}
private void LivraisonCmdeFournisseurIhm_Load(object sender, EventArgs e)
{
}
private void dataGridViewCmdeFinal_CellContentClick(object sender, DataGridViewCellEventArgs e1)
{
if (dataGridViewCmdeFinal.RowCount > 0)
{
//dataGridViewCmdeFinal.EndEdit();

// c'est ici que l'exception se signale
voirSiCheckBoxValider = dataGridViewCmdeFinal[9, e1.RowIndex].Value.ToString();
if (voirSiCheckBoxValider.Equals("True"))
{
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].ReadOnly = true;
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].Value = "" + (int.Parse(dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[1].Value.ToString()) - QteCondiDjaLivrer);
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].ReadOnly = true;
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].Value = "" + (int.Parse(dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[5].Value.ToString()) - QteProDjaLivrer);
}
else
{
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].ReadOnly = false;
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].Value = "";
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[4].Value = "";
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].ReadOnly = false;
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].Value = "";
dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[8].Value = "";
}
livraisonPartielle = LivraisonCmdeFourniBD.afficheLivraisonPartiel(listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1].getNumCmdeFourni(), listProCmder[e1.RowIndex].getConcerneProModel().getCodPro().getCodPro(), listProCmder[e1.RowIndex].getConcerneProModel().getCodModel().getCodModel());
if (livraisonPartielle.Count > 0)
{
textBoxQteCondiDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerCondi();
textBoxQteProDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerPro();
QteCondiDjaLivrer = livraisonPartielle[0].getQteLivrerCondi();
QteProDjaLivrer = livraisonPartielle[0].getQteLivrerPro();
}
else
{
textBoxQteCondiDjaLivrer.Text = "" + 0;
textBoxQteProDjaLivrer.Text = "" + 0;
QteCondiDjaLivrer = 0;
QteProDjaLivrer = 0;
}
}
else
{
textBoxQteCondiDjaLivrer.Text = "";
textBoxQteProDjaLivrer.Text = "";
QteCondiDjaLivrer = 0;
QteProDjaLivrer = 0;
}
dataGridViewCmdeFinal.EndEdit();
}
private void dataGridViewCmdeFinal_CellValueChanged(object sender, DataGridViewCellEventArgs e2)
{
dataGridViewCmdeFinal.EndEdit();
if (dataGridViewCmdeFinal.RowCount > 0)
{
int qteLivrerCom=0,qteLivrerPro=0,qteCmderPro=0,qteCmderCom=0;
try
{
qteCmderCom = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[1].Value.ToString());
if (qteCmderCom == 0)
{
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[3].Value = "" + 0;
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[4].Value = "" + 0;
}
else
{
qteLivrerCom = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[3].Value.ToString());
int sommeQteAlivrerEtLivrerCondi = qteLivrerCom + QteCondiDjaLivrer;
if (sommeQteAlivrerEtLivrerCondi <= qteCmderCom)
{
montantLivrerCmdde = qteLivrerCom * (double.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[2].Value.ToString()) / int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[1].Value.ToString()));
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[4].Value = "" + montantLivrerCmdde;
}
else
{
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[3].Value = "" + 0;
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[4].Value = "" + 0;
}
}
qteCmderPro = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[5].Value.ToString());
if (qteCmderPro == 0)
{
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[7].Value = "" + 0;
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[8].Value = "" + 0;
}
else
{
qteLivrerPro = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[7].Value.ToString());
int sommeQteAlivrerEtLivrerPro = qteLivrerPro + QteProDjaLivrer;
if (sommeQteAlivrerEtLivrerPro <= qteCmderPro)
{
montantLivrerCmddePro = qteLivrerPro * (double.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[6].Value.ToString()) / int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[5].Value.ToString()));
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[8].Value = "" + montantLivrerCmddePro;
}
else
{
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[7].Value = "" + 0;
dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[8].Value = "" + 0;
}
}
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.Message);
//MessageBox.Show(this, "Vérifiez que vous n'avez pas saisie une chaîne au lieu d'un chiffre réel sur la colonne quantitée livrée!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (qteLivrerCom != 0 || qteLivrerPro != 0)
{
buttonEnregistrer.Enabled = true;
}
else
{
buttonEnregistrer.Enabled = false;
}
}
}
private void buttonEnregistrer_Click(object sender, EventArgs e)
{
LivraisonCmdeData cmdeLivrer = new LivraisonCmdeData(0, listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1], MaterielControl.dateDuJr(), calculMontantLivrer(dataGridViewCmdeFinal));
List livraisonTotal=new List();
//MessageBox.Show(""+cmdeLivrer);
int qteLivrerPro 0, qteLivrerCondi 0;
double montantLivrerPro 0, montantLivrerCondi 0;
for (int i = 0; i < dataGridViewCmdeFinal.RowCount; i++)
{
try
{
qteLivrerPro = int.Parse(dataGridViewCmdeFinal.Rows[i].Cells[7].Value.ToString());
montantLivrerPro = double.Parse(dataGridViewCmdeFinal.Rows[i].Cells[8].Value.ToString());
qteLivrerCondi = int.Parse(dataGridViewCmdeFinal.Rows[i].Cells[3].Value.ToString());
montantLivrerCondi = double.Parse(dataGridViewCmdeFinal.Rows[i].Cells[4].Value.ToString());
ProduitData produit = new ProduitData(listProCmder[i].getConcerneProModel().getCodPro().getCodPro(), listProCmder[i].getConcerneProModel().getCodPro().getLibPro(), null);
ModelProduitData model = new ModelProduitData(listProCmder[i].getConcerneProModel().getCodModel().getCodModel(), listProCmder[i].getConcerneProModel().getCodModel().getLibModel(), "");
ConcernerData concerner = new ConcernerData(produit, model);
livraisonTotal.Add(new ProduitLivrerData(cmdeLivrer, concerner, listProCmder[i].getNumCondi(), qteLivrerPro, qteLivrerCondi, montantLivrerPro, montantLivrerCondi));
}
catch (Exception ex) { Console.Out.WriteLine(ex.Message); }
}
if (calculMontantLivrer(dataGridViewCmdeFinal) != 0)
{
if (LivraisonCmdeFourniBD.enregistrerLivraisonCmde(livraisonTotal) == true)
{
MessageBox.Show(this, "La confirmation de votre livraison s'est effectuée avec succès!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(this, "Une erreur est survenue lors de la confirmation de cette livraison!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void dataGridViewCmdeFinal_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
}
private void comboBoxFourni_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxFourni.SelectedIndex > 0)
{
comboBoxCmdeAconfirmer.Enabled = true;
chargerCombo(CmdeFournisseurBD.afficheCmdeNonLivrer(listFournisseur[comboBoxFourni.SelectedIndex - 1].getCodFourni()));
//comboBoxCmdeAconfirmer.SelectedIndex = 1;
}
else
{
comboBoxCmdeAconfirmer.Items.Clear();
dataGridViewCmdeFinal.Rows.Clear();
comboBoxCmdeAconfirmer.Enabled = false;
}
}
private void dataGridViewCmdeFinal_CellClick(object sender, DataGridViewCellEventArgs e3)
{
if (dataGridViewCmdeFinal.RowCount > 0)
{
livraisonPartielle = LivraisonCmdeFourniBD.afficheLivraisonPartiel(listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1].getNumCmdeFourni(), listProCmder[e3.RowIndex].getConcerneProModel().getCodPro().getCodPro(), listProCmder[e3.RowIndex].getConcerneProModel().getCodModel().getCodModel());
if (livraisonPartielle.Count > 0)
{
textBoxQteCondiDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerCondi();
textBoxQteProDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerPro();
QteCondiDjaLivrer = livraisonPartielle[0].getQteLivrerCondi();
QteProDjaLivrer = livraisonPartielle[0].getQteLivrerPro();
try
{
int condiCmder = int.Parse(dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[1].Value.ToString());
int proCmder = int.Parse(dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[5].Value.ToString());
if (QteCondiDjaLivrer == condiCmder)
{
dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[3].Value = "" + 0;
dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[3].ReadOnly = true;
}
if (QteProDjaLivrer == proCmder)
{
dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[7].Value = "" + 0;
dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[7].ReadOnly = true;
}
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.Message);
}
}
else
{
textBoxQteCondiDjaLivrer.Text = "" + 0;
textBoxQteProDjaLivrer.Text = "" + 0;
QteCondiDjaLivrer = 0;
QteProDjaLivrer = 0;
}
}
else
{
textBoxQteCondiDjaLivrer.Text = "";
textBoxQteProDjaLivrer.Text = "";
QteCondiDjaLivrer = 0;
QteProDjaLivrer = 0;
}
dataGridViewCmdeFinal.EndEdit();
}
}
}

2 réponses

NHenry Messages postés 15114 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 4 mai 2024 159
14 févr. 2012 à 12:46
Bonjour,

dataGridViewCmdeFinal[9, e1.RowIndex].Value.ToString()
C'est que au moins un élément de cette partie est null, regardes en Debug pour savoir lequel.
Je penche plus pour .Value ou e1.

---------------------------------------------------------------------
[list=ordered][*]Pour poser correctement une question et optimiser vos chances d'obtenir des réponses, pensez à lire le règlement CS, ce lien ou encore celui-ci[*]Quand vous postez un code, merci d'utiliser la coloration syntaxique (3ième icône en partant de la droite : )
[*]Si votre problème est résolu (et uniquement si c'est le cas), pensez à mettre "Réponse acceptée" sur le ou les messages qui vous ont aidés./list
---
Mon site
0
BunoCS Messages postés 15476 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 3 mai 2024 103
14 févr. 2012 à 13:51
Yop,
Merci d'utiliser les balises "code" (3e icone au-dessus de la zone de texte en partant de la droite) pour plus de lisibilité


@+
Buno, Admin CS
L'urgent est fait, l'impossible est en cours. Pour les miracles, prévoir un délai...
0
Rejoignez-nous