访问2D数组时出现NullPointerException

时间:2014-04-16 08:29:58

标签: java nullpointerexception

首先,这是我的函数“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+" €";

现在我打电话给我:

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

我的问题:

我在“setProduct”=>中遇到错误this.Produit [nbrProduct] [0] =代码;

错误说“java.lang.NullPointerException”,我猜它试图在“this.Produit [nbrProduct] [0]”中放入一个空值,但事情就是当我执行“System.out.println”时“Code =”+ code);“它向我展示了正确的代码,并为所有其他代理人提供了相同的代码:/

编辑:

以下是我创建Produit attribut的方法:

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

编辑2:

堆栈跟踪:

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)

代码(对不起,我用法语留下,然后你看到我使用的真实姓名):

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

我的公共课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+"";

} 

4 个答案:

答案 0 :(得分:2)

很可能你没有正确设置数组。

最简单的例子:

Foo[] foo = new Foo[10];

它只是使用10引用Foo创建一个数组,但是这些引用没有指向实际的Foo对象。因此,当您尝试访问foo[0].bar()

时,它会导致NPE

类似,

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

将给出一个指向Product []的10个引用数组。但是,没有创建实际的Product数组对象。因此,与上面的示例类似,如果执行product[1][0],则会获得NPE,因为product[1]指向null,因为要访问该[0] Product[]而引发NPE参考。您需要明确地实例化它们。


更新

使用OP引用的额外代码,我试图运行它,并且该代码中没有NPE。

最有可能的是,您已将produit(我强烈建议您遵循Java的通用命名约定)重新分配给null,或者将produit[n]分配给其他代码中的null

我建议快速检查哪一个为空:

在作业之前添加:

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

它应该告诉你哪一个是null,并给你提示你可能有不正确的更新参考。

答案 1 :(得分:1)

我的猜测是this.Produit[nbrProduct]null

可以初始化:this.Produit[nbrProduct] = new String[someSize];

答案 2 :(得分:1)

分配null永远不会导致NPE,问题必须是您的数组尚未初始化。

在创建对象时初始化数组,因此我的猜测是在调用setProduct方法之前重新分配数组。调用setProduct时,检查this.Produit[nbrProduct]是否为空。

PS:请使用Java约定:方法,属性和变量名称以小写开头,类/接口名称以大写开头。通常尝试使用CamelCase(常量除外,应为LIKE_THIS_EXAMPLE

答案 3 :(得分:1)

问题

根据您的堆栈跟踪,NPE由此行引起:

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

显然,您通过控制台输出发现此处的nullthis.Produit

可能的原因

由于您说过使用以下行初始化Produit

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

那么你的NPE不应该发生,除非你通过某些代码重置了对null的引用,如:

Commande.Produit = null;

查找写访问

如果您正在使用Eclipse,则可以找到对您的属性的每个写访问权限:

  1. 点击Produit课程中的属性Commande_Final,以便突出显示
  2. 转到Search > Write Access > Project
  3. 检查是否存在除初始化之外的任何其他写访问权
相关问题