public class  BoxDemo1{
public static void main(String args[])
{
box[] a=new box[3];
a[0].width=10;
a[0].length=10;
a[0].color=10;
a[0].dialog="i love you";

System.out.print("a0"+a[0].width);
System.out.print("a0"+a[0].length);
System.out.print("a0"+a[0].color);
System.out.print("a0"+a[0].dialog);
}

}

解决方案 »

  1.   

    你初始化的是a[1]对象 调用的却是a[0]对象错误是不是空指针啊 NullPointer
      

  2.   

    你设置的是a[1],操作的是a[0]。此时a[0]的属性都是空啊,因为没有初值。
    所以要能有结果
    System.out.print("a0"+a[1].width);
    System.out.print("a0"+a[1].length);
    System.out.print("a0"+a[1].color);
    System.out.print("a0"+a[1].dialog);
      

  3.   

    对不起楼上的兄弟,你说的不对啊。仍旧不行!
    错误仍旧是空指针NullPointer!!!!!
      

  4.   

    我将两个文件分开BoxDemo1.java:
    public class  BoxDemo1{
    public static void main(String args[])
    {
    box[] a=new box[3];
    a[0].width=10;
    a[0].length=10;
    a[0].color=10;
    a[0].dialog="xxxxxxxxx";

    System.out.print("a0"+a[0].width);
    System.out.print("a0"+a[0].length);
    System.out.print("a0"+a[0].color);
    System.out.print("a0"+a[0].dialog);
    }

    }box.java
    class box{
    public int width;
    public int length;
    public int color;
    public String dialog;
    }把他们作为一个工程,编译通过,但是执行工程时却是空指针!
      

  5.   

    各位老大们,这不是什么特别难的问题吧。各位斑竹和网友可要帮忙啊,我才学习JAVA,多多指教啊。
      

  6.   

    class box{
    public int width;
    public int length;
    public int color;
    String dialog;
    }public class  BoxDemo1{
    public static void main(String args[])
    {
    box[] a=new box[3];
    a[0]=new box();
    a[0].width=10;
    a[0].length=10;
    a[0].color=10;
    a[0].dialog="i love you";

    System.out.print("a0"+a[0].width);
    System.out.print("a0"+a[0].length);
    System.out.print("a0"+a[0].color);
    System.out.print("a0"+a[0].dialog);
    }

    }
    对数组的每个元素还要初始化:a[0]=new box();
      

  7.   

    box[] a=new box[3];这样不是已经初始化3个了吗?
      

  8.   

    box[] a=new box[3]并没有初始化数组中的元素,具体为什么我不是很清楚
    不过我想这样做并不是没有原因的
    比如有一个抽象类abs,有三个类实现了abs,分别是a1,a2和a3
    那么就可以
    abs[] test=new abs[3];
    test[0]=new a1();
    test[1]=new a2();
    test[2]=new a3();
      

  9.   

    真正的原因是这样的:box[] a=new box[3];初始化一个 box 类型的数组,由于此数组的内容是对象类型的,数组在初始化时自动对内容赋初值,若为原始类型则是 int,long = 0;long,double=0.0,boolean=false; 若为对象类型则为 null,你的代码初始的是对象类型,自然为 null.直接调用和赋值当然说 NullPointerException.所以必须有这句a[0]=new box();这才是真正对数组元素进行初始化。
      

  10.   

    box[] = new box[3];
    for(int i=0;i<box.length;i++) {
      box[i] = new box();
    }