package first;
//import java.util.*;
public class Gouzao {
  public Gouzao(String s) {
    System.out.println("This is a test"+s);
  }
  Random rand=new Random();
  public static void main(String[] args) {
    Gouzao a[]=null;
    Gouzao b=new Gouzao("aa");
       a[0]=b;
       a[1]=b;
       a[2]=b;
       for (int i=0;i<a.length ;i++ ) {
       System.out.println(a[i]);
       }
  }
}当我运行的是侯,结果显示为:
java.lang.NullPointerException
at first.Gouzao.main(Gouzao.java:20)
Exception in thread "main" This is a testaa
请高手帮我看看。不胜感激!

解决方案 »

  1.   

    a[]没初始化,正常的Gouzao a[]应该执行默认的构造函数,也就是public Gouzao(){},你改成Gouzao a[]=new Gouzao[2];
      

  2.   

    Gouzao a[]=null;这句应改为Gouzao a[]=new Gouzao[3];
      

  3.   

    要这样改一下就OK
    Gouzao a[]=new Gouzao[3];
      

  4.   

    为什么不能显示列名:请高手过招
    import javax.swing.table.*;public class InAndOutTableModel extends AbstractTableModel {
      private Object[][] data=null;
      private String[] columnNames=null;
      public InAndOutTableModel() {
      }
      public int getRowCount() {
        return data!=null?data.length:0;
      }
      public int getColumnCount() {
        return columnNames!=null?columnNames.length:0;
      }
      public Object getValueAt(int rowIndex, int columnIndex) {
        return data!=null?data[rowIndex][columnIndex]:"";
      }
      public void setColumnNames(String[] columnNames){
        this.columnNames=columnNames;
        
      }
      public void setData(String[][] data){
        this.data=data;
      }}
      

  5.   

    Gouzao a[]=new Gouzao[3];
    但打印的结果将是对象的地址。
      

  6.   

    肯定是地址啦,b只实例化一次,实例化时输出this is a testaa 后面打印的就是b的引用啦
      

  7.   

    to有缘人:你的也是那两个设为null的变量没有初始化的问题,private Object[][] data=new Object[3][3];
      private String[] columnNames=new String[3];下次发贴要出新主题哦