免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1750 | 回复: 1
打印 上一主题 下一主题

[help]程序调试中的tirm()问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-23 09:16 |只看该作者 |倒序浏览
程序已定义了一个类d4r1F,在另一程序调用它,的程序如下,便已通过,执行一下代码时,系统提示:
Exception in thread "main" java.lang.NullPointerException at d4r1.mai(d4r1.java:25)
请教给魏大侠是那里的错,怎么改?谢谢了


d4r1.java的代码:
import java. text. *;
import java. io. *;

public class  d4r1
{
  public static void main(String[] args)
  {
   int i;
   String s=new String();
   double calc, x, actual;
   double pi=3.1415926;
   d4r1F g= new d4r1F();
   DecimalFormat form=new DecimalFormat("0.000000" ) ;
   try
   {
    RandomAccessFile fin=new RandomAccessFile("in.dat","r" ) ;
    String temp= new String("Gama Function" ) ;
    String temp1=new String();
   
    while(s.compareTo(temp)!=0)
     {
       s=fin.readLine();
       s=s.trim();
      }

     temp=fin.readLine();
     temp1=temp.substring( 0 , 3) ;
     double nval1=Double.valueOf(temp1).doubleValue();
   
      System.out.println("Log of Gamma function" );
      System.out.println(" x        actual        gamma(x) " );

      for(i=1; i<=(int)nval1; i++)
      {
         temp= fin.readLine();
         temp1=temp.substring(0,4);
         x= Double.valueOf(temp1).doubleValue();
         temp1=temp.substring( 8 );
         actual=Double.valueOf(temp1).doubleValue();
         if(x>0.0)
         {
            if(x>=1.0)
            {
        calc=g.gammln(x);
            }
            else
             {
        calc=g.gammln(x+1)-Math.log(x);
             }
         System.out.println(form.format(x)+"        " ) ;
         System.out.println(form.format(Math.log(actual))+"        " ) ;
         System.out.println(form.format(calc));

          }
         }
         fin.close();
       }
        catch (FileNotFoundException e)
        {
        System.out.println("FilleStreamTest:"+e );
         }
        catch(IOException e)
       {
        System.out.println("FileStreamTest:"+e );
        }
      }
}



d4r1F.java的代码:
public class  d4r1F
{
        double gammln(double xx)
        {
                int j;
                double temp;
                double cof[]=new double[7];
                double stp, half, one, fpf, x, tmp, ser;
                cof[1]=76.18009173;
                cof[2]=-86.50532033;
                cof[3]=24.01409822;
                cof[4]=-1.231739516;
                cof[5]=0.00120858033;
                cof[6]=-0.00000536382;

                stp=2.50662827465;
                half=0.5;
                one=1.0;
                fpf=5.5;
                x=xx-one;
                tmp=x+fpf;
                tmp=(x+half)*Math.log(tmp)-tmp;
                ser=one;
                for(j=1; j<=6; j++)
                {
                        x=x+one;
                        ser=ser+cof[j]/x;
                }
                temp=tmp+Math.log(stp*ser);
                return temp;
        }
}

数据in.dat
1.00000
1.20000
1.40000
1.60000
1.80000
2.00000
0.20000
0.40000
0.60000
0.80000
10.00000
20.00000
30.00000

[ 本帖最后由 yupeng_cun 于 2006-5-23 09:33 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-05-24 02:47 |只看该作者
s=fin.readLine() 可能返回的是null啊,你得保证if (s=fin.readLine()!=null) 然后才能 s.xxx 否则,如果s==null你就会得到NullPointerException
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP