D3fman
D3fman

Reputation: 85

NullPointerException when accessing 2D array

First, here is my function "setProduct" :

public void setProduct(String code, int qty,double price,int nbrProduct){

    System.out.println("Code = "+code);
    System.out.println("qty = "+qty);
    System.out.println("price = "+price);
    System.out.println("nbrProduct = "+nbrProduct);


    this.Produit[nbrProduct][0] = code;
    this.Produit[nbrProduct][1] = Integer.toString(qty);
    this.Produit[nbrProduct][2] = price+" €";

And now where I call :

Commande_Final Commande = new Commande_Final();
Commande = Cmd;

String name = Commande.getName();
double prixUnit = Function.GetMagPrice(radar,Qtyradar)/Qtyradar;

System.out.println("j'affiche le radar : "+name);
System.out.println("j'affiche le Qtyradar : "+Qtyradar);
System.out.println("j'affiche le prix du radar : "+prixUnit+" €");
System.out.println("nbr de produit = "+nbr_de_Produit);

Commande.setProduit(name,Qtyradar,prixUnit,nbr_de_Produit);

My problem :

I get an error when I do in "setProduct" => this.Produit[nbrProduct][0] = code;

The error says "java.lang.NullPointerException", I guess its trying to put an empty value in "this.Produit[nbrProduct][0]" but the thing is when I do the "System.out.println("Code = "+code);" its show me the correct code and the same for all others attributs :/

EDIT :

Here is how I create my Produit attribut :

public String[][] Produit = new String[99][3];

EDIT 2 :

stack trace :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Commande_Final.setProduit(Commande_Final.java:33)
at ZDialogInfo.Confirmer(ZDialogInfo.java:449)
at ZDialogV2$29.actionPerformed(ZDialogV2.java:1037)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The code (sorry I leave in french, then you see the real name I use):

public void Confirmer(boolean LoginCheck,String Nom,ZDialogInfo zInfo,final String Langue, Commande_Final Cmd) throws IOException{

                Commande_Final Commande = Cmd;

...

                if (Qtyradar > 0){

                    /*
                     * PROBLEME ICI
                     * 
                     * */
                    String name = Commande.showName();
                    double prixUnit = Function.GetMagPrice(radar,Qtyradar)/Qtyradar;

                    System.out.println("j'affiche le radar : "+name);
                    System.out.println("j'affiche le Qtyradar : "+Qtyradar);
                    System.out.println("j'affiche le prix du radar : "+prixUnit+" €");
                    System.out.println("nbr de produit = "+nbr_de_Produit);

/*ZDialogInfo.Confirmer(ZDialogInfo.java:449) => */

  Commande.setProduit(name,Qtyradar,prixUnit,nbr_de_Produit);

                    nbr_de_Produit++;
                    System.out.println("nbr de produit = "+nbr_de_Produit);


                JP_CNom.add(JL_CRadar);
                JP_CNom.add(JL_CQtyRadar);
                JP_CNom.add(JL_CPrixRadar);
                if (RnbrItem >= 1) JP_CNom.add(RCompo1);
                if (RnbrItem >= 2) JP_CNom.add(RCompo2);
                if (RnbrItem >= 3) JP_CNom.add(RCompo3);
                }

My public class Commande_Final

public class Commande_Final {
private String[][] Produit = new String[MAX_Produit][3];        
/*
 * 
 * [Nbr de Produit][0] = Code_Item
 * [Nbr de Produit][1] = Qty_Cmdé
 * [Nbr de Produit][2] = Prix_Unitaire
 *
 * */

public void setProduit(String code, int qty,double prix,int nbrProduit){

    System.out.println(" ------------------- Dans setProduit ---------------------- ");



    System.out.println("Code = "+code);
    System.out.println("qty = "+qty);
    System.out.println("prix = "+prix);
    System.out.println("nbrProduit = "+nbrProduit);


/*at Commande_Final.setProduit(Commande_Final.java:33) =>*/     this.Produit[nbrProduit][0] = code;
    this.Produit[nbrProduit][1] = Integer.toString(qty);
    this.Produit[nbrProduit][2] = prix+"";

} 

Upvotes: 0

Views: 275

Answers (4)

Joffrey
Joffrey

Reputation: 37650

Problem

According to your stack trace, NPE is caused by this line:

this.Produit[nbrProduct][0] = code;

Apparently, you found out via console output that what is null here is this.Produit.

Possible Cause

Since you said you initialized Produit with the following line:

private String[][] Produit = new String[99][3];

then your NPE should not happen, unless you have reset the reference to null somewhere via some code like:

Commande.Produit = null;

Finding write accesses

If you're using Eclipse, there is a way to find every write access to your attribute:

  1. click on your attribute Produit in your Commande_Final class, so that it is highlighted
  2. go to Search > Write Access > Project
  3. check if there is any other write access than your initialization

Upvotes: 1

Pablo Lozano
Pablo Lozano

Reputation: 10342

Assigning null never causes a NPE, the issue must be your array has been not initialized.

The array is initialized when the object is created, so my guess is that array is re-assigned before the setProduct method is called. Check if this.Produit[nbrProduct] is not null when setProduct is called.

PS: Please, use Java conventions: method, attribute and variable names start with lowcase, class/interface names start with uppercase. In general try to use CamelCase (except constants, that should be LIKE_THIS_EXAMPLE)

Upvotes: 1

Adrian Shum
Adrian Shum

Reputation: 40036

Most probably you haven't set up the array correctly.

A simplest example:

Foo[] foo = new Foo[10];

it is just making an array with 10 reference to Foo, however there is no actual Foo object those reference is pointing to. Hence it will cause NPE when you are trying to access foo[0].bar()

Similar,

Product[][] product = new Product[10][];

is going to give u an array of 10 reference pointing to Product[]. However there is no actual Product array object created. Therefore similar to the above example, you will get NPE if you do product[1][0], because product[1] is point to null, NPE is thrown because you want to access [0] of that null Product[] reference. You need to instantiate them explicitly.


Update:

With the extra code that OP quoted, I have tried to run that and there is no NPE from that piece of code.

It is most probably that, you have reassigned your produit (I would strongly recommend you follow Java's common naming convention) to null, or assigned produit[n] to null in some other code.

I would suggest a quick check on which one is null:

Add before your assignment:

System.out.println("produit null ? " + (produit == null));
System.out.println("produit[n] null ? " + (produit[nbrProduit] == null));

it should tell you which one is null, and give you hints where you may have incorrect updated the reference.

Upvotes: 2

Jean Logeart
Jean Logeart

Reputation: 53809

My guess is that this.Produit[nbrProduct] is null.

It can be initialized: this.Produit[nbrProduct] = new String[someSize];

Upvotes: 1

Related Questions