-4

I have created a simple java program to manage a commercial company's store I'm using netbeans 8.0.2, here is Magasins.java

package tn.tuniprob.gestionmagasin;

import java.util.*;
public class Magasins {
  private int id;  
  private String adresse;
  private final int CAPACITE=50;
  private Produit []tab=new Produit[CAPACITE];
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getAdresse() {
    return adresse;
}

public void setAdresse(String adresse) {
    this.adresse = adresse;
}

public int getCapacite() {
    return CAPACITE;
}



public Produit[] getTab() {
    return tab;
}

public void setTab(Produit[] tab) {
    this.tab = tab;
}


public void ajouterproduit(int i)
  { String nom = null;
      String marque=null;
      float prix=0;
      int idp=0;
      Scanner sc1 = new Scanner(System.in);
  if((i<CAPACITE)&&(i<50))
  { for (int j=0;j<i;j++)
  { 
      System.out.println("entrez l'id");
      idp=sc1.nextInt();
      System.out.println("entrez le nom");
      nom=sc1.nextLine();
      nom=sc1.nextLine();
      System.out.println("entrez la marque");
      marque=sc1.nextLine();
      }
      for (int j=0;j<i;j++)
      { if (idp==tab[i].getid())
      {System.out.println("objet dupliqué");}
        if (i==CAPACITE)
              System.out.println("magazin complet");
  else {
              for (j=0;j<i;j++)
              System.out.println("objet dupliqué");
              this.tab[i].setid(idp);
              this.tab[i].setlibelle(nom);
              this.tab[i].setmarque(marque);
      }
  }}}



public void afficher()



{  for(int j=0;j<CAPACITE;j++)

      System.out.println("d'abord le magazin: id="+id
                          +" adresse="+adresse+" capacite="+CAPACITE+
                          "\n produits: numero"+j+" id="+tab[j].getid()+" libelle="
                          +tab[j].getlibelle()+" marque="+tab[j].getmarque()+" prix="
                          +tab[j].getprix()+" date d'expiration="
                          +tab[j].getDate_expiration()+"\n");


 }

public void affichernbtotal()
{ int somme=0;

    for(int j=0;j<CAPACITE;j++)
    {
    if(null!=tab[j]) {
        somme=somme+1;

    } }
    System.out.println("le nombre total des produits est="+somme);}
public int comparer(Produit p)

{for (int j=0;j<CAPACITE; j++)
if ((p.getid()==tab[j].getid())&&(p.getlibelle()==tab[j].getlibelle())&&(p.getprix()==tab[j].getprix()))
return 1;

  return 0;
    }

public int comparer(Produit p1,Produit p2)



 {if((p1.getid()==p2.getid())&&(p1.getlibelle()==p2.getlibelle())&&(p1.getprix()==p2.getprix()))
    {return 1;}
    else
    {return 0;}
    }




 public boolean chercher(int id)
    {for(int j=0;j<CAPACITE;j++)
    if(tab[j].getid()==id)
        return true;
    return false;
    }

public void supproduit(int i)
{
    tab[i].setid(0);
    tab[i].setlibelle(null);
    tab[i].setmarque(null);
    tab[i].setprix(0);

}
public Magasins comparer(Magasins m1, Magasins m2)
{
    int s1=0,s2=0;
    for(int i=0; i<CAPACITE;i++)
        if((m1.tab[i].getmarque()!=null))
            s1++;
    for(int i=0; i<CAPACITE;i++)
    if (m2.tab[i].getmarque()!=null)
        s2++;
    return ((s1<s2)?m2:m1);
}








}

and this is Produit.java

package tn.tuniprob.gestionmagasin;

import java.util.Date;
public class Produit {
    private int id;
    private String libelle;
    private String marque;
    private float prix;
    private Date date_expiration;

    public Produit(int id, String libelle, String marque, float prix) {
        this.id = id;
        this.libelle = libelle;
        this.marque = marque;
        this.prix = prix;

    }


    public int getid()
    {return id;}
    public void setid(int a)
    {id=a;}
    public String getlibelle()
    {return libelle;}
    public void setlibelle(String a)
    {libelle=a;}
    public String getmarque()
    {return marque;}
    public void setmarque(String a)
    {marque=a;}
    public float getprix()
    {return prix;}
    public void setprix(float a)
    {if (a<=0) System.out.println("erreur"); else prix=a;}
    public void afficher()
    { System.out.println(id+" "+libelle+" "+marque+" "+prix+" ");}

    @Override
    public String toString() {
        return "produit_alimentaire{" + "id=" + id + ", libelle=" + libelle + ", marque=" + marque + ", prix=" + prix + '}';
    }


    public Date getDate_expiration() {
        return date_expiration;
    }

    public void setDate_expiration(Date date_expiration) {
        this.date_expiration = date_expiration;}
         public void afficherplusdate()
    { System.out.println(id+" "+libelle+" "+marque+" "+prix+" "+date_expiration);}


}

My Main.java is just here to test every method, my problem is on the execution there is

Exception in thread "main" java.lang.NullPointerException
    at tn.tuniprob.gestionmagasin.Magasins.ajouterproduit(Magasins.java:70)
    at tn.tuniprob.gestionmagasin.Main.main(Main.java:24)

when I try to run it, can you give me a solution please? thanks in advance PS: I really hate java but it's on my school program to tech everyone that trash

talex
  • 17,973
  • 3
  • 29
  • 66
bargo
  • 3
  • 1
  • 6
    http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Mehraj Malik Sep 19 '16 at 12:14
  • 1
    `new Produit[CAPACITE]` create array but doesn't fill it. – talex Sep 19 '16 at 12:19
  • 4
    Dont get me wrong, but: your source code isn't simple. The stuff that you posted here is a **horribly confusing MESS**. Terrible formatting, convention-violating naming, un-clean code on many many levels. It is absolutely no wonder that you think you need our help to debug a simple NPE from such "input". – GhostCat Sep 19 '16 at 12:20
  • 2
    "I really hate java". Obviously, that is why you are struggling with it because you don't have the interest to learn it. Don't expect others to help you when you are not ready to learn. "that trash" - really? – birraa Sep 19 '16 at 12:23
  • 1
    And looking at your final paragraph: the *trash* is not in the language, but in the code that *you* wrote. – GhostCat Sep 19 '16 at 13:30

1 Answers1

1
private Produit[] tab = new Produit[CAPACITE];

This will create an array of size 50 initialized with null values, something like this:

tab = {null, null, null, ...}

so tab[0] and tab[1] are still null.

When you invoke tab[0].getId(), this throws NullPointerException.

You need to initialize the array first with some values in order to use them, something like:

tab[0] = new Produit();
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • I am trying to explain to the OP the reason why his code is not working well. I am not sure what the down-vote is for. – Eng.Fouad Sep 19 '16 at 12:27
  • ah, a constructor, just like in basic C, that's why I hate it, so much basic problems (C++ and C# have nearly the same syntax but much much problems don't exist, and to be frank, I want to learn everything, but I hate J-things because I used them not because it's prejudgement – bargo Sep 19 '16 at 13:00
  • and @ Mehraj Malik that's french you know? – bargo Sep 19 '16 at 13:03
  • @Eng.Fouad Maybe because the idea would be to just put down a close request, and maybe give a comment with the fix (as others did) ... instead of looking out to make some reputation on an obvious NPE-dup question. – GhostCat Sep 19 '16 at 13:10
  • reputation? ain't nobody have time for that – bargo Sep 19 '16 at 14:33