Partage
  • Partager sur Facebook
  • Partager sur Twitter

Erreur de type Exception in thread "main" java.lan

    9 octobre 2021 à 18:26:55

    Bonjour à vous, je viens auprès de vous car je travaille sur un mini projet en java.

    Mon code compile correctement mais lors de l'exécution du switch(case 1) ca me renvoie une erreur du type Exception in thread "main" java.lang.NullPointerException" j'arrive pas à voir mon erreur, j'ai parcouru les diffférents forum et on dit que c'est parceque j'ai implemente une taille du tableau qui est petite mais j'y arrive. Je travaille avec 3 fichiers et Gestion_Magasin et le main. Merci d'avance pour votre aide!!!!!!!!!!!

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    //package gestion_magasin;
    package tn.tuniprob.gestionmagasin;
    import java.util.Scanner;
    
    /**
     *
     * @author DELL
     */
    public class Gestion_Magasin {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            
            //Produit[] p = new Produit[50];
            Magasin m = new Magasin();
            Produit p = new Produit();
            int choix;
            Scanner sc = new Scanner(System.in);
            do{
                System.out.println("***********************PROSIT0***********************\n");
                System.out.println("1)Ajouter un produit");
                System.out.println("2)Afficher les caratéristiques du magasin et des produits ");
                System.out.println("3)Afficher le nombre total de produit");
                System.out.println("0)Quitter le programme\n");
                System.out.println("Veuillez selectionner une option: ");
                choix =sc.nextInt();
                
            }
            while((choix<0)||(choix>3));
            
            switch(choix)
            {
                case 0:
                    System.exit(0);
                case 1:
                    m.ajouterProduit(p);
                    break;
            }
        }
        
    }
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author DELL
     */
    package tn.tuniprob.gestionmagasin;
    
    public class Magasin {
        
        private int identifiant;
        private String adresse;
        private int capacite;
        private int all_produit;
        
        Produit[] produits;
        
        public Magasin()
        {
            
        }
        
        public Magasin(int id, String ad, int cap, int allp)
        {
            this.identifiant = id;
            this.adresse = ad;
            this.capacite = cap;
            this.all_produit = allp;
            this.produits = new Produit[50];
        }
        
        public void ajouterProduit(Produit p)
        {
            //Produit p = new Produit();
            this.produits[capacite] = p;
            capacite++;
        }
        
        public Produit[] getProduit(Produit[] p)
        {
            return p;
        }
      
        
        public int getID()
        {
            return identifiant;
        }
        
        public String getAdresse()
        {
            return adresse;
        }
        
        public int getCapacite()
        {
            return capacite;
        }
        
        public int getAll_produit()
        {
            return all_produit;
        }
        
        public void setID(int Idf)
        {
            this.identifiant = Idf;
        }
        
         public void setAdresse(String Ad)
        {
            this.adresse = Ad;
        }
         
          public void setCapacite(int Cap)
        {
            this.capacite = Cap;
        }
          
         public void setAll(int all_p)
        {
            this.all_produit = all_p;
        }
        
    }
    
    
    package tn.tuniprob.gestionmagasin;
    
    import java.util.Scanner;
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author DELL
     */
    public class Produit {
        private int identifiant;
        private String libelle;
        private String marque;
        private float prix;
        
        public Produit()
        { 
            
        }
        
        public Produit(int id, String lib, String maq, float prx)
        {
            this.identifiant = id;
            this.libelle = lib;
            this.marque = maq;
            this.prix = prx;
        }
        
        public void afficher()
        {
            System.out.println("Details des differents produits: \n");
            System.out.println("Description: \nLe numéro d'identifiant est : ID("+this.identifiant+ "), son libelle est: "+this.libelle+", la marque est: "+this.marque+" et le prix est de "+this.prix);
        }
        
        
    //Acceseurs
        
        public int getId()
        {
            return identifiant;
        }
        
        public String getLib()
        {
            return libelle;
        }
        
        public String getMarque()
        {
            return marque;
        }
        
        public float getPrix()
        {
            return prix;
        }
        
        
    //Mutateurs
        
        public void setId(int mId)
        {
            identifiant = mId;
        }
        
        public void setLib(String Lib)
        {
            libelle = Lib;
        }
        
        public void setMarque(String Marq)
        {
            marque = Marq;
        }
        
        public float setPrix(float Price)
        {
            prix = Price;
            return Price;
        }
        
        
    }
    





    • Partager sur Facebook
    • Partager sur Twitter
      10 octobre 2021 à 8:47:57

       Exception in thread "main" java.lang.NullPointerException" j'arrive pas à voir mon erreur

      Si tu te focalises uniquement sur le type de l'exception sans lire le reste de la trace, c'est un peu normal.

      • Partager sur Facebook
      • Partager sur Twitter
        11 octobre 2021 à 14:42:04

        Suis ce que fais ton programme et si tes attributs de Magasin et de Produits sont OK au cours de l'exécution (ils existent ? ils sont définis? l'opération demandée est faisable?).
        • Partager sur Facebook
        • Partager sur Twitter
          11 octobre 2021 à 22:27:13

          Salut UmFred j'ai vérifié mais malheureusement je vois pas mon erreur.

          Sachant que mon programme focntionne mais lorsque j'execute le code j'ai une erreur sur cette partie

          • Partager sur Facebook
          • Partager sur Twitter
            12 octobre 2021 à 13:00:12

            lignes 23 et 24 de ton main: que valent les attributs de ton produit et de ton magasin ?

            qu'est ton tableau m.produits[] au moment tu appelles ta fonction m.ajouterProduit(p) ? (vérifie ton hypothèse)

            • Partager sur Facebook
            • Partager sur Twitter

            Erreur de type Exception in thread "main" java.lan

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown