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 :

Object reference not set to an instance of an object


Sujet :

C#

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Décembre 2010
    Messages
    4
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2010
    Messages : 4
    Points : 5
    Points
    5
    Par défaut Object reference not set to an instance of an object
    Bonjour, j'ai une erreur à la ligne : MessageBox.Show("Votre solde actuel est" + list_user[i].Compte_user.Solde_actuelle);
    qui dit : Object reference not set to an instance of an object

    Classe Compte
    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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
     
    namespace Ex7
    {
        [Serializable]
     
        class Compte
        {
     
            private int no_compte;
            private float solde_actuelle = 50f;
            private float solde_max;
            private float solde_min;
     
            public Compte() { }
     
            public Compte(int c_no_compte, float c_solde_actuelle, float c_solde_max, float c_solde_min)
            {
                this.no_compte = c_no_compte;
                this.solde_actuelle = c_solde_actuelle;
                this.solde_max = c_solde_max;
                this.solde_min = c_solde_min;
            }
     
            public int No_compte
            {
                get { return no_compte; }
                set { no_compte = value; }
            }
     
            public float Solde_actuelle
            {
                get { return solde_actuelle; }
                set { solde_actuelle = value; }
            }
     
            public float Solde_max
            {
                get { return solde_max; }
                set { solde_max = value; }
            }
     
            public float Solde_min
            {
                get { return solde_min; }
                set { solde_min = value; }
            }
        }
    }
    Classe Utilisateurs
    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
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    //sérialisation
     
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
     
    namespace Ex7
    {
        [Serializable]
        class Utilisateurs
        {
            // attributs privées
     
            private string nom;
            private string prenom;
            private string pass;
            private string login;
            private int no_compte;
            private Compte compte_user = new Compte();
     
            public Utilisateurs(string c_nom, string c_prenom, string c_pass, string c_login, int c_no_compte)
            {
                nom = c_nom;
                prenom = c_prenom;
                pass = c_pass;
                login = c_login;
                no_compte = Convert.ToInt32(c_no_compte); 
     
            }
     
            // attributs publiques
     
            public string Prenom
            {
                get { return prenom; }
                set { prenom = value; }
            }
     
            public string Pass
            {
                get { return pass; }
                set { pass = value; }
            }
     
            public int No_compte
            {
                get { return no_compte; }
                set { no_compte = value; }
            }       
     
            public string Nom
            {
                get { return nom; }
                set { nom = value; }
            }
     
            public string Login
            {
                get { return login; }
                set { login = value; }
            }
     
            public Compte Compte_user
            {
                get { return compte_user; }
                set { compte_user = value; }
            }
        }
    }
    Form1
    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
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
     
    namespace Ex7
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
     
            private void btConnexion_Click(object sender, EventArgs e)
            {
     
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream("File.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
                List<Utilisateurs> list_user = (List<Utilisateurs>)formatter.Deserialize(stream);
                stream.Close();
                for (int i = 0; i < list_user.Count; i++) 
                {
                    if (tbLogin.Text == list_user[i].Login && tbPassword.Text == list_user[i].Pass)
                    {
                        MessageBox.Show("Vous etes connecté");
                        MessageBox.Show("votre numéro de compte est" + list_user[i].No_compte);
                        MessageBox.Show("Votre solde actuel est" + list_user[i].Compte_user.Solde_actuelle);
                        Banque bq = new Banque();
                        bq.Show();
                    }
                }
     
     
     
            }
     
            private void btNoveauUtilisateur_Click(object sender, EventArgs e)
            {
                FormNoveauUtilisateur fnu = new FormNoveauUtilisateur();
                fnu.Show();
            }
     
     
        }
    }

  2. #2
    Membre émérite
    Profil pro
    Mangeur de gauffre
    Inscrit en
    Octobre 2007
    Messages
    4 413
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Mangeur de gauffre

    Informations forums :
    Inscription : Octobre 2007
    Messages : 4 413
    Points : 2 498
    Points
    2 498
    Par défaut
    Probablement que l'un de ces deux eléments est null

    Compte_user
    ou
    Compte_user.Solde_actuelle

    Verifie la construction de tes objets
    « Ils ne savaient pas que c'était impossible, alors ils l'ont fait ». (Twain)

Discussions similaires

  1. Réponses: 6
    Dernier message: 01/04/2008, 18h36
  2. new : Object reference not set to an instance of an object
    Par zulad dans le forum Windows Forms
    Réponses: 3
    Dernier message: 19/06/2007, 20h35
  3. Object reference not set to an instance of an object
    Par DjRusty dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 24/03/2007, 07h26
  4. Réponses: 1
    Dernier message: 28/12/2006, 11h05
  5. Réponses: 1
    Dernier message: 20/04/2006, 12h09

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