IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

exception NullReferenceException levée


Sujet :

C#

  1. #1
    Candidat au Club
    Inscrit en
    Mai 2008
    Messages
    2
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 2
    Points : 2
    Points
    2
    Par défaut exception NullReferenceException levée
    bonjour
    quelqu'un a une idée sur cette exception et comment la résoudre svp ?
    merci d'avance

  2. #2
    Membre confirmé Avatar de elbj
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2004
    Messages
    371
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Services à domicile

    Informations forums :
    Inscription : Novembre 2004
    Messages : 371
    Points : 558
    Points
    558
    Christophe B.

  3. #3
    Débutant
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Points : 66
    Points
    66
    Par défaut
    merci
    j'ai essayé les try catch
    mais ca donne rien just ca bloque

  4. #4
    Membre confirmé Avatar de elbj
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2004
    Messages
    371
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Services à domicile

    Informations forums :
    Inscription : Novembre 2004
    Messages : 371
    Points : 558
    Points
    558
    Par défaut
    Cette exception signifie que ton programme essaye d'utiliser un objet qui n'est pas instancié. Donc repère l'objet en question et vérifie que tu l'instancies bien.

    Cordialement
    Christophe B.

  5. #5
    Membre expérimenté

    Homme Profil pro
    kiné passionné de dev
    Inscrit en
    Mars 2006
    Messages
    1 570
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France

    Informations professionnelles :
    Activité : kiné passionné de dev

    Informations forums :
    Inscription : Mars 2006
    Messages : 1 570
    Points : 1 738
    Points
    1 738
    Billets dans le blog
    1
    Par défaut
    Le plus facile pour t'aider, c'est de montrer ton code
    Petit poulain devient grand de jour en jour...

    Petit Poulain, ce n'est que pour les intimes!

  6. #6
    Débutant
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Points : 66
    Points
    66
    Par défaut
    merci beaucoup
    voila une partie

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public void résoudre_Click_1(object sender, EventArgs e)
            {
               int  nbr_con = Convert.ToInt32(txtcon.Text);
               int  nbr_var = Convert.ToInt32(txtvar.Text);
               decimal[] Simpl = new decimal[(nbr_var + 2) * (nbr_con + 1)];
               decimal[] sol = new decimal[nbr_var];
               decimal[] Donnees = new decimal[(nbr_var + 2) * (nbr_con + 1)];
    
                int i = 0;
                    while (i < nbr_var)
                    {
                        Simpl[Convert.ToInt16( (nbr_con * nbr_var )+ 2 + i)] =(this.Controls["CofonctiObjec" + i.ToString()] as NumericUpDown).Value;
                        i++;
                    }
                i = 0;
                    while (i <nbr_con)
                    {
                        j = 0;
                        
                        while (j < nbr_var)
                        {
                            Simpl[Convert.ToInt16( i * (nbr_var + 2) + j)] =(this.Controls["CofContrainte" + i.ToString()] as NumericUpDown).Value;
                            
                            j++;
                        }
                
                    Simpl[Convert.ToInt16( i * (nbr_var + 2) + j + 1)] = -1;
                    try { Simpl[Convert.ToInt16(i * (nbr_var + 2) + j)] = (this.Controls["CofSecoMembre" + i.ToString()] as NumericUpDown).Value; }
                        catch(Exception ){}
                    
                    i++;
                }
                 Simplex S = new Simplex(); 
                
                Resultat = S.Simplexe(Simpl, nbr_var, nbr_con, sol, z, mot);
                mot += "\n\t*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-RESULTAT:*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
                if (Resultat != 0)
                {
    
                    mot += "\nLe probleme admet une solution optimale :\t\n -Z =" + z[0] + "\n\n\tavec\t";
                    for (i = 0; i < nbr_var; i++)
                        mot += "\n\n X" + (i + 1) + "=\t" + (sol[Convert.ToInt16(i)]);
                    mot += "\n";
                }
                else
                    mot += "\n\"Le  n'admet pas de solution!!!\"";
                MessageBox.Show(mot, "DONNEES");
    
                
              
            }
    la ligne coloré la ou souligné l'erreur

  7. #7
    Membre expérimenté
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Points : 1 521
    Points
    1 521
    Par défaut
    Vérifie bien que les contrôles NumericUpDown "CofonctiObjec" + i.ToString() existent bien pour toutes les valeurs de i entre 0 et nbr_var - 1.

  8. #8
    Débutant
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Points : 66
    Points
    66
    Par défaut
    merci ,ca progresse apres ce changement,mais ca reste et manant ca genere
    dans autre ligne coloré ci dessous
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace simplexessaie
    {
        public partial class frm : Form
        {
    
            public frm()
            {
    
                InitializeComponent();
    
            }
    
    
    
    
            decimal Resultat, j;
    
            decimal[] z = new decimal[1];
            String mot = "";
    
    
            public void affiche_coefficient(object sender, EventArgs e)
            {
                int nbr_con = Convert.ToInt32(txtcon.Text);
                int nbr_var = Convert.ToInt32(txtvar.Text);
                decimal[] Simpl = new decimal[(nbr_var + 2) * (nbr_con + 1)];
                decimal[] sol = new decimal[nbr_var];
                decimal[] Donnees = new decimal[(nbr_var + 2) * (nbr_con + 1)];
    
                int j = 0;
                int c = (nbr_con * 30) + 30;
                int k = 0;
                int s = 1;
                int r = 170;
    
                // lblcoefobj
                Label lblcoefobj = new Label();
                lblcoefobj.Location = new System.Drawing.Point(7, 104);
                lblcoefobj.Size = new System.Drawing.Size(200, 13);
                lblcoefobj.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.75F,
    
    ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold |
    
    System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblcoefobj.Text = "Coefficient de la fonction objectif:";
                this.Controls.Add(lblcoefobj);
                // lblcoefcon
                Label lblcoefcon = new Label();
                lblcoefcon.Location = new System.Drawing.Point(7, 200);
                lblcoefcon.Size = new System.Drawing.Size(200, 13);
                lblcoefcon.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.75F,
    
    ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold |
    
    System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblcoefcon.Text = "Coefficient des contraintes :";
                this.Controls.Add(lblcoefcon);
                // lblcoefsemem
    
                for (int i = 1; i <= nbr_var; i++)
                {
                    Label LbVarcf = new Label();
                    LbVarcf.Location = new System.Drawing.Point(280 + j, 100);
                    LbVarcf.Name = "LbVar";
                    LbVarcf.Size = new System.Drawing.Size(20, 13);
                    LbVarcf.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F,
    
    ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold |
    
    System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    LbVarcf.Text = "X" + i.ToString();
                    LbVarcf.ForeColor = System.Drawing.Color.BlueViolet;
    
                    this.Controls.Add(LbVarcf);
                    NumericUpDown CofonctiObjec = new NumericUpDown();
                    CofonctiObjec.Location = new Point(230 + j, 100);
                    CofonctiObjec.Size = new System.Drawing.Size(50, 20);
                    CofonctiObjec.DecimalPlaces = 2;
                    CofonctiObjec.Name = "CofonctiObjec" + i.ToString();
                    CofonctiObjec.Minimum = -500;
                    CofonctiObjec.Maximum = 500;
                    this.Controls.Add(CofonctiObjec);
                    j += 80;
                }
    
    
                do
                {
                    r += 80;
                    k = 0;
                    for (int i = 1; i <= nbr_con; i++)
                    {
                        Label LbVarcc = new Label();
                        LbVarcc.Location = new System.Drawing.Point(r + 50, 200 + k);
                        LbVarcc.Name = "LbVar";
                        LbVarcc.Size = new System.Drawing.Size(20, 13);
                        LbVarcc.Font = new System.Drawing.Font("Microsoft Sans Serif", 6.75F,
    
    ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold |
    
    System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        LbVarcc.ForeColor = System.Drawing.Color.BlueViolet;
    
                        LbVarcc.Text = "X" + s.ToString();
                        this.Controls.Add(LbVarcc);
                        NumericUpDown CofContrainte = new NumericUpDown();
                        CofContrainte.Location = new Point(r, 200 + k);
                        CofContrainte.Size = new System.Drawing.Size(50, 20);
                        CofContrainte.DecimalPlaces = 2;
                        CofContrainte.Name = "CofContrainte" + i.ToString();
                        CofContrainte.Minimum = -500;
                        CofContrainte.Maximum = 500;
                        this.Controls.Add(CofContrainte);
                        k += 30;
                    }
    
                    s++;
                }
                while (s <= nbr_var);
    
    
                bool ok = false;
                do
                {
                    int m = 0;
                    r += 60;
                    for (int i = 1; i <=nbr_con; i++)
                    {
    
                        ComboBox com = new ComboBox();
                        com.Location = new Point(r + 20, 200 + m);
                        com.Size = new System.Drawing.Size(50, 21);
                        com.Name = "com" + i.ToString();
                        com.Font = new System.Drawing.Font("Arial", 9.75F,System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                        com.FormattingEnabled = true;
                        com.Items.AddRange(new object[] {
                "=>",
                "<=",
                "="});
                        //com.TabIndex = 13;
                        this.Controls.Add(com);
    
    
                        NumericUpDown CofSecoMembre = new NumericUpDown();
                        CofSecoMembre.Location = new Point(r + 80, 200 + m);
                        CofSecoMembre.Size = new System.Drawing.Size(50, 20);
                        CofSecoMembre.DecimalPlaces = 2;
                        CofSecoMembre.Name = "CofSecoMembre" + i.ToString();
                        this.Controls.Add(CofSecoMembre);
                        CofSecoMembre.Minimum = -500;
                        CofSecoMembre.Maximum = 500;
                        m += 30;
                    }
    
    
                }
                while (ok);
            }
    
    
            public void résoudre_Click_1(object sender, EventArgs e)
            {
    
                int nbr_con = Convert.ToInt32(txtcon.Text);
                int nbr_var = Convert.ToInt32(txtvar.Text);
                decimal[] Simpl = new decimal[(nbr_var + 2) * (nbr_con + 1)];
                decimal[] sol = new decimal[nbr_var];
                decimal[] Donnees = new decimal[(nbr_var + 2) * (nbr_con + 1)];
    
                int i = 1;
                while (i <= nbr_var)
                {
    
                    Simpl[Convert.ToInt16((nbr_con * nbr_var) + 2 + i)] =(this.Controls["CofonctiObjec" + i.ToString()] as NumericUpDown).Value;
                    i++;
                }
                i = 1;
                while (i <=nbr_con)
                {
                    j = 1;
    
                    while (j <= nbr_var)
                    {
                        Simpl[Convert.ToInt32(i * (nbr_var + 2) + j)]=(this.Controls["CofContrainte" + i.ToString()] as NumericUpDown).Value;
    
                        j++;
                    }
                    //choix de signe
                    ComboBox com = new ComboBox();
                    if (com.SelectedItem.ToString() == "<=")
                    {
    
                        Simpl[Convert.ToInt32(i * (nbr_var + 2) + j + 1)] = -1;
    
    
                    }
    
                    else if (com.SelectedItem.ToString() == "=>")
                    {
                        Simpl[Convert.ToInt32(i * (nbr_var + 2) + j + 1)] = 1;
    
    
                    }
                    else if (com.SelectedItem.ToString() == "=")
                    {
    
                        Simpl[Convert.ToInt32(i * (nbr_var + 0) + j + 1)] = 0;
                    }
                   // Simpl[Convert.ToInt32(i * (nbr_var + 2) + j + 1)] = -1;
                    try
                    {
                        Simpl[Convert.ToInt32(i * (nbr_var + 2) + j)] =
    
                            (this.Controls["CofSecoMembre" + i.ToString()] as NumericUpDown).Value;
                    }
                    catch (Exception) { }
    
                    i++;
                }
    
    
    
                Simplex S = new Simplex();
    
                Resultat = S.Simplexe(Simpl, nbr_var, nbr_con, sol, z, mot);
                mot +=
    
    "\n\t*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-RESULTAT:*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
                if (Resultat != 0)
                {
    
                    mot += "\nLe probleme admet une solution optimale :\t\n -Z =" + z[0] +
    
    "\n\n\tavec\t";
                    for (i = 0; i < nbr_var; i++)
                        mot += "\n\n X" + (i + 1) + "=\t" + (sol[Convert.ToInt32(i)]);
                    mot += "\n";
                }
                else
                    mot += "\n\"Le  n'admet pas de solution!!!\"";
                MessageBox.Show(mot, "DONNEES");
    
    
    
            }
    
            private void button1_Click_1(object sender, EventArgs e)
            {
                int nbr_con = Convert.ToInt32(txtcon.Text);
                int nbr_var = Convert.ToInt32(txtvar.Text);
    
                try
                {
    
                    for (int i = 0; i < nbr_var; i++)
                    {
                        this.Controls["CofonctiObjec" + i].Text = "";
                    }
    
                    for (int i = 0; i < nbr_var; i++)
                    {
                        for (int j = 0; j < nbr_con; j++)
                        {
                            this.Controls["CofContrainte" + i].Text = "";
    
                        }
    
                    }
    
    
                    for (int i = 0; i < nbr_con; i++)
                    {
                        this.Controls["CofSecoMembre" + i].Text = "";
                    }
    
                }
                catch (Exception) { }
            }
    
    
            private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {
                MessageBox.Show("essaie");
            }
    
    
        }
    }
    et merci beaucoup

  9. #9
    Membre expérimenté
    Homme Profil pro
    Inscrit en
    Juillet 2007
    Messages
    1 277
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Réunion

    Informations forums :
    Inscription : Juillet 2007
    Messages : 1 277
    Points : 1 521
    Points
    1 521
    Par défaut
    Merci d'apprendre à utiliser la balise CODE (voir ce que Poulain a fait sur ton post précédent).

    Je vois pas l'intérêt de faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ComboBox com = new ComboBox();
    puis

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    if (com.SelectedItem.ToString() == "<=")
    En effet, tu viens de créer une nouvelle ComboBox qui ne contient aucun élément (logique, ils n'ont pas été Add) et qui par conséquent ne peut proposer aucune sélection. Il est donc tout à fait normal que SelectedItem est NULL et que tu ne puisses y faire ToString();

  10. #10
    Débutant
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    98
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Maroc

    Informations forums :
    Inscription : Juillet 2006
    Messages : 98
    Points : 66
    Points
    66
    Par défaut
    merci
    j'ai fait une déclaration globale comme ceci:combobox com;
    est ce bon

Discussions similaires

  1. L'exception NullReferenceException n'a pas été gérée
    Par sycoulibaly dans le forum VB.NET
    Réponses: 13
    Dernier message: 20/02/2020, 11h49
  2. Exception 'NullReferenceException' incomprise
    Par samworld dans le forum ASP.NET
    Réponses: 0
    Dernier message: 27/11/2008, 09h34
  3. [VS2005] S'arreter quand une exception est levée
    Par ZePostman dans le forum Visual Studio
    Réponses: 8
    Dernier message: 28/08/2008, 15h24
  4. Réponses: 4
    Dernier message: 14/05/2007, 09h28
  5. Exception non levée
    Par michaelbob dans le forum Langage
    Réponses: 4
    Dernier message: 17/10/2006, 14h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo