首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

数组出现空指针错误,实在是找不出为啥为null来啊求教

2012-12-23 
数组出现空指针异常,实在是找不出为啥为null来啊,求教本帖最后由 xiaoge090702113 于 2012-12-20 19:02:54

数组出现空指针异常,实在是找不出为啥为null来啊,求教
本帖最后由 xiaoge090702113 于 2012-12-20 19:02:54 编辑 空指针的代码在SuiJiChouTi类第47行,temp[w] 那里,求教求教,真找不出来了,都怨我java是半路出家

package zujuanxitong;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class Helper implements MysqlConfig{
private static Connection conn;
private static Statement stat;
private static ResultSet rs;
int cc;
int temp[];
        /*

         中间是连接数据库的代码,跟问题无关

                */
                public void xuanTi()throws SQLException{
int i=0;
Zongshu bb = new Zongshu();
bb.jiShu();
cc=bb.aa;
//System.out.println(cc);
temp=new int[cc];
String sql = "select ItemID from iteminformation where chapters = '一' ; ";
ResultSet rs = Helper.query(sql);
    while(rs.next()){
    int num=rs.getInt("ItemID");
temp[i]=num;
    System.out.println(temp[i]);
    i++;   
    }
   }
}


package zujuanxitong;
import java.sql.SQLException;
import com.mysql.jdbc.ResultSet;

public class Zongshu {
     int aa;
public void jiShu() throws SQLException{
String sql="select count(*) from iteminformation where chapters = '一' ;";
ResultSet shu =  (ResultSet) Helper.query(sql);
//while(shu.next()){
shu.next();
//System.out.println(shu.getInt(1));
aa = shu.getInt(1);
//System.out.println(aa);
//}
}
}

package zujuanxitong;

import java.util.Random;
public class SuiJiChouTi extends Helper {
int[] intRet = new int[6]; //存放随机数的数组 
int dd;
public void chouTi() {
   Zongshu bb = new Zongshu();
   try{
   bb.jiShu();
   }catch(Exception e){
   e.printStackTrace();
   }
   dd=bb.aa;//dd为选择的该类试题总数
           
   int intRd = 0; //存放随机数
           int count = 0; //记录生成的随机数个数
           int flag = 0; //是否已经生成过标志
      
           while(count<6){
                Random rdm = new Random(System.currentTimeMillis());
                intRd = Math.abs(rdm.nextInt())%dd;//dd为该类试题总数,随机数的范围设定,dd=10意思为 范围是0~9


                for(int i=0;i<count;i++){
                    if(intRet[i]==intRd){
                        flag = 1;
                        break;
                    }else{
                        flag = 0;
                    }
                }
                if(flag==0){
                    intRet[count] = intRd;
                    count++;
                }
       }//生成随机数,并初始化intRet[]
               
           Helper tt = new Helper();
               try{
               tt.xuanTi();
               }catch(Exception e){
           e.printStackTrace();
               }//声明temp[]
              
               for(int t=0;t<6;t++){
           int w=intRet[t];
           //if(temp[w]!=null)
           System.out.println(temp[w]);//出现空指针异常的数组!!!
       }
}

public static void main(String[]args){
SuiJiChouTi sjct= new SuiJiChouTi();
sjct.chouTi();
}
}


[解决办法]
        Helper tt = new Helper();
               try{
                   tt.xuanTi();
               }catch(Exception e){
               e.printStackTrace();
               }//声明temp[]

               this.temp=tt.temp;//楼主加上试试。



               for(int t=0;t<6;t++){
               int w=intRet[t];
               //if(temp[w]!=null)
               System.out.println(temp[w]);//出现空指针异常的数组!!!
       }


[解决办法]
或者:(没有验证,供楼主参考)。
        //Helper tt = new Helper();
               try{
                   xuanTi();//直接执行这个方法。自然就是本对象的temp.
               }catch(Exception e){
               e.printStackTrace();
               }//声明temp[]
               
               for(int t=0;t<6;t++){
               int w=intRet[t];
               //if(temp[w]!=null)
               System.out.println(temp[w]);//出现空指针异常的数组!!!
       }

[解决办法]
引用:

楼上正解,楼主【tt.xuanTi();】调用的是tt的对象,所以this.temp还是没有初始化,这样肯定temp就会空指针。
可以直接把【tt.xuanTi()】改成【this.xuanTi()】调用就行!相当于自己调用自己,temp就能初始化了。

热点排行