=arr[0].x(); //在这里就发生错误???
???

解决方案 »

  1.   

    classname[] arr=new classname[3];或classname arr[]=new classname[3];
    应该都是可以的呀。
      

  2.   

    classname[] arr=new classname[3];或classname arr[]=new classname[3];你只是定义了一个reference arr,并没有对数组元素(classname对象)初始化
    当你试图使用数组元素时,自然就抛出异常
    java.lang.NullPointerException
      

  3.   

    哦,知道了,你的数组里的数据没有初始化.. 
    可能是吧.
    classname[] arr ={new classname(), new classname(), new classname()};
    这样应该可以了
      

  4.   

    classname[] arr = (new classname[] {
                classname_instant1,
                classname_instant2,
                classname_instant3,
            });
      

  5.   

    代码如下:
    ----文件ace\allvar.java
    package ace;
    public class allvar{
      int num;
      public void num(int n){num=n;}
      public int num(){return num;}
      String str;
      public void str(String s){str=s;}
      public String str(){return str;}
      public allvar(){}
    }
    --------文件test.jsp
    <%@ page contentType="text/html;charset=Gb2312"%>
    <%@ page import="java.lang.*,java.util.*"%>
    <%@ page import="java.sql.*,oracle.jdbc.pool.*" %>
    <%@ page import="ace.*" %>
    <html>
    <head>
      <META http-equiv=CONTENT-TYPE content="TEXT/HTML; CHARSET=GB2312">
    </HEAD>
    <body>
    <%
    allvar k[]=new allvar[3];
    k[0].num(6);//错误在此发生:java.lang.NullPointerException
    out.println(k[0].num());
    %>
    </body>
    </html>
    --------------------------
    这个问题折腾了我很长时间,请高手指教!
      

  6.   


    allvar k[]=new allvar[3];->allvar k[]={new classname(), new classname(), new classname()};
      

  7.   

    faint!
    allvar k[]={new allvar(), new allvar(), new allvar()};
      

  8.   

    allvar k[]={new allvar(), new allvar(), new allvar()};
    用这种方法就没问题了,可数组数量若不是3个,而是1000个该怎么办?
      

  9.   

    allvar k[]=new allvar[1000];
    for(int i=0;i<k.length;i++)
       k[i]=new allvar();
      

  10.   

    我原来作的东西:
    bean中如下:
        /**************************年报返回结果**************************/
        public String[][] getYearReport(Statement Stmt, String nf, String UnitID) throws SQLException
        {
           ResultSet RS=null;
       String sqlStr=null,StrTmp=null;
       int cnt=0;String MontData[][];
       //取得数组的一维下标,并填充固定资产分类、分类编码和年折旧率
           try
           {
             sqlStr="select count(distinct ID) as cnt From E_Category";
             RS=Stmt.executeQuery(sqlStr);
             RS.next();
             cnt=RS.getInt("cnt"); 
              RS.close();
           }
           catch(Exception e){System.out.println("Depreciate.java-->getYearReport():" + e.toString());}
       MontData = new String [cnt+1][17];// //最后一行为累计值,初始化数组
       try
       {
        sqlStr="select * From E_Category";
             RS=Stmt.executeQuery(sqlStr);
    RS.beforeFirst();
     for (int i=0;i<cnt;i++)
     {
     RS.next();
     MontData[i][0]=RS.getString("Name");
     MontData[i][1]=RS.getString("ID");
     MontData[i][2]=RS.getString("Percentage2");
     }
       }
       catch (Exception e)
       {
       System.out.println("Depreciate.java-->getYearReport():" + e.toString());
       }
       float fData[][]=new float[cnt+1][14];
      try{
       //查询表Depreciate
       for (int i=0;i<cnt;i++)
    {
       if ((MontData[i][1].indexOf('*'))!=-1)
       {
       StrTmp=MontData[i][1].substring(0,MontData[i][1].indexOf('*'));
       }else{
       StrTmp=MontData[i][1];
       }
       sqlStr="select FLID,NZhJL,avg(JTZhJYZh1+JTZhJYZh2+JTZhJYZh3+JTZhJYZh4+JTZhJYZh5+JTZhJYZh6+JTZhJYZh7+JTZhJYZh8+JTZhJYZh9+JTZhJYZh10+JTZhJYZh11+JTZhJYZh12) as JTZhJYZh";
       sqlStr=sqlStr+",sum(ZhJE1+ZhJE2+ZhJE3+ZhJE4+ZhJE5+ZhJE6+ZhJE7+ZhJE8+ZhJE9+ZhJE10+ZhJE11+ZhJE12) as ZhJE";
       sqlStr=sqlStr+",sum(ZhJE1) as ZhJE1,sum(ZhJE2) as ZhJE2,sum(ZhJE3) as ZhJE3,sum(ZhJE4) as ZhJE4,sum(ZhJE5) as ZhJE5,sum(ZhJE6) as ZhJE6,sum(ZhJE7) as ZhJE7,sum(ZhJE8) as ZhJE8,sum(ZhJE9) as ZhJE9,sum(ZhJE10) as ZhJE10,sum(ZhJE11) as ZhJE11,sum(ZhJE12) as ZhJE12";
       sqlStr=sqlStr+" from Depreciate where NF='"+nf+"' and DW='" +UnitID+ "' and FLID like '"+StrTmp+"%' order by FLID"; 
       RS=Stmt.executeQuery(sqlStr);
       RS.next();
       fData[i][0]=RS.getFloat("JTZhJYZh");
       fData[i][1]=RS.getFloat("ZhJE");
       fData[i][2]=RS.getFloat("ZhJE1");
       fData[i][3]=RS.getFloat("ZhJE2");
       fData[i][4]=RS.getFloat("ZhJE3");
       fData[i][5]=RS.getFloat("ZhJE4");
       fData[i][6]=RS.getFloat("ZhJE5");
       fData[i][7]=RS.getFloat("ZhJE6");
       fData[i][8]=RS.getFloat("ZhJE7");
       fData[i][9]=RS.getFloat("ZhJE8");
       fData[i][10]=RS.getFloat("ZhJE9");
       fData[i][11]=RS.getFloat("ZhJE10");
       fData[i][12]=RS.getFloat("ZhJE11");
       fData[i][13]=RS.getFloat("ZhJE12");
       }
      RS.close();
          }
           catch(Exception e){System.out.println("Depreciate.java-->getYearReport():" + e.toString());}
     //计算“合计”
     MontData[cnt][0]="合        计";
     MontData[cnt][1]="";MontData[cnt][2]="";//&nbsp;
     for (int j=0;j<14;j++ )
       for (int i=0;i<cnt+1 ;i++ )
       {fData[cnt][j]+=fData[i][j];}
     for (int i=0;i<cnt+1 ;i++ )
       for (int j=3;j<=16;j++ )
       {
        MontData[i][j]=String.valueOf(fData[i][j-3]);
       }       return MontData;     
        }jsp中:
    String Data[][] = Dep.getYearReport(Stmt, Year,UnitID);
    int RowCount = Data.length;
    for (int i=0; i<=RowCount-1; i++)
    {System.out.println("Data["+i+"][0]:"+Data[i][0]);}