bargo bargo - 1 month ago 5x
Java Question

execution difficulties on my java program

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

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) { = 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) { = tab;

public void ajouterproduit(int i)
{ String nom = null;
String marque=null;
float prix=0;
int idp=0;
Scanner sc1 = new Scanner(;
{ for (int j=0;j<i;j++)
System.out.println("entrez l'id");
System.out.println("entrez le nom");
System.out.println("entrez la marque");
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é");[i].setid(idp);[i].setlibelle(nom);[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="


public void affichernbtotal()
{ int somme=0;

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

} }
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)

{return 1;}
{return 0;}

public boolean chercher(int id)
{for(int j=0;j<CAPACITE;j++)
return true;
return false;

public void supproduit(int i)

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


and this is

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) { = id;
this.libelle = libelle;
this.marque = marque;
this.prix = prix;


public int getid()
{return id;}
public void setid(int a)
public String getlibelle()
{return libelle;}
public void setlibelle(String a)
public String getmarque()
{return marque;}
public void setmarque(String 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+" ");}

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 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(
at tn.tuniprob.gestionmagasin.Main.main(

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

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();