Java中的数学计算

时间:2015-08-27 21:21:59

标签: java math

我正在研究一个计算数学方程式的简单程序。但是有一个我找不到的问题。任何帮助将不胜感激。

似乎问题在于

alpha[j] = (double)(j-1)*2*Math.PI/(double)rotationNum;

返回NullPointerException。这里必须有一些愚蠢的错误。

import java.util.*;
import java.io.*;
//import Jama.Matrix;

class efun {
    static double epso;
    static double sigma;
    static double alpha[];
    static double charge;
    static double axisR;
    static double axisZ;
    //static Random randGen;
    static int numPoints = -1;
    static int rotationNum;


    public static void main (String[] args) {
        try {
            sigma = 300e-6*1e2; 
            epso = 8.854e-12; 

            /*Input arguments*/
            numPoints = Integer.parseInt (args[0]);
            FileReader fr = new FileReader(args[1]);
            rotationNum = Integer.parseInt (args[2]);
            BufferedReader br = new BufferedReader(fr);         
            double pointsR[] = new double[numPoints];
            double pointsZ[] = new double[numPoints];
            double chargeDensity[] = new double[numPoints];
            double electricField = 0.0;
            double ER = 0.0;
            double EZ = 0.0;
            double EY = 0.0;

            for (int id = 0; id < numPoints; id++) {
                // read file
                while ( (line = br.readLine() )!= null) {
                    StringTokenizer stk = new StringTokenizer(line);
                    axisR = Double.parseDouble(stk.nextToken());
                    axisZ = Double.parseDouble(stk.nextToken());
                    charge = Double.parseDouble(stk.nextToken());

                    pointsR[id] = axisR;
                    pointsZ[id] = axisZ;
                    chargeDensity[id] = charge;
                    System.out.println("axisR: "+pointsR[id]+" and axisZ: "+ pointsZ[id]+"; its corresponding charge density is: "+ chargeDensity[id]);

                    double rotatedR[] = new double[numPoints];
                    double rotatedZ[] = new double[numPoints];
                    double rotatedY[] = new double[numPoints];
                    double sumSquarePoints[] = new double[numPoints];

                    for (int j = 1; j < rotationNum+1; j++) {                   
                        alpha[j] = (double)(j-1)*2*Math.PI/(double)rotationNum; 
                        System.out.println("print alpha: "+alpha[j]);
                        rotatedR[id] = pointsR[id] - Math.cos(alpha[j])*pointsR[id];
                        rotatedZ[id] = pointsZ[id];
                        rotatedY[id] = pointsR[id] - Math.sin(alpha[j])*pointsR[id];

                        sumSquarePoints[id] = Math.sqrt(rotatedR[id]*rotatedR[id] + rotatedZ[id]*rotatedZ[id] + rotatedY[id]*rotatedY[id]);

                        ER += chargeDensity[id]*rotatedR[id]/(sumSquarePoints[id]*sumSquarePoints[id]*sumSquarePoints[id]);
                        EZ += chargeDensity[id]*rotatedZ[id]/(sumSquarePoints[id]*sumSquarePoints[id]*sumSquarePoints[id]);
                        EY += chargeDensity[id]*rotatedY[id]/(sumSquarePoints[id]*sumSquarePoints[id]*sumSquarePoints[id]);

                        System.out.println ("ER is: "+ ER);
                        System.out.println ("EZ is: "+ EZ);
                        System.out.println ("EY is: "+ EY);
                    }
                }
            }

            electricField = sigma/(4*Math.PI*epso)*Math.sqrt(ER*ER + EZ*EZ + EY*EY); 
            System.out.println("electricField is: " + electricField);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您从未初始化alpha,您只声明了它,因此您无法访问alpha[j]。对其进行初始化并确保其大小足以满足每个j:

alpha = new double[MY_SIZE];

此外,请确保您向main传递至少3个参数,以便正确分配rotationNum。

答案 1 :(得分:1)

您的类变量alpha已声明,但未初始化,因此Java为其提供了默认值null。该变量从未初始化为任何数组。

static double alpha[];

但是,除了当前值之外,您看起来并没有使用数组中的任何其他预期值。只需将其声明为本地double(不是数组),并将其用作普通变量。

double alpha = (double)(j-1)*2*Math.PI/(double)rotationNum;

然后使用alpha代替alpha[j]几行。

答案 2 :(得分:0)

您从未初始化alpha[]。就像pontsR, pointsZ, and chargeDensity一样,您需要在新的双打数组中指出alpha才能使用它。

答案 3 :(得分:0)

获取alpha = new double[rotationNum ];

后添加rotationNum