- 论坛徽章:
- 0
|
程序已定义了一个类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 编辑 ] |
|