用户定义的java中的静态变量中的NullPointerException(android)

时间:2015-01-29 01:45:47

标签: java android nullpointerexception

我有一个带有静态函数的类我想在其他地方使用:

public class MathBiquadFncMatlab {

final static int N_CHANNEL = 64;
// jj (?)       -> sqrt(-1), imaginary number

final static int FREQ_FIRST = 125, FREQ_INC = 125, FREQ_FIN = 8000;


// s-domain coefficients
static double eh2 = 0.0, eh1 = 0.0, eh0 = 0.0,
        be2 = 1.0, be1 = 1.0, be0 = 1.0;

// z-domain coefficients
static double b0 = 1.0, b1 = 0.0, b2 = 0.0,
        a0 = 0.0, a1 = 0.0, a2 = 0.0;

//  #1. s-domain
static double A, Q, w;

// needed as temporary variables later
// it might be possible to modify the program so that these variables are not nedded at all
// think about this later if needed
static double b2a, b1a, b0a, a2a, a1a, a0a;

static double T, gDenominator;

static double[] omega = new double[64];
// static double[] xZ_r = new double[64];
// static double[] xZ_i = new double[64];
static Complex[] xZ = new Complex[64];
static Complex[] xZTemp1 = new Complex[64];
static Complex[] xZTemp2 = new Complex[64];
static Complex[] xY_num = new Complex[64];
static Complex[] xY_den= new Complex[64];
static Complex[] xY = new Complex[64];

static double[] Y64 = new double[64];

// --------------- file handling for troubleshooting ---------------
static DataOutputStream outShort;
private static final String mRcordFilePath = Environment.getExternalStorageDirectory().toString() + "/inMathBiquadFncMatlabY64.dat";
// -----------------------------------------------------------------

static double[]  fnMathBiquad(double FS, int iFunction, double gFreq, double iGain, double iQ ){

    try {
         outShort = new DataOutputStream(new FileOutputStream(mRcordFilePath));
        // streamWriteShort = new FileOutputStream(mRcordFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    double xY_denRealTemp, xY_numRealTemp; 
    A = Math.pow(10.0, iGain/20.0);         // converted real value from input dB value
    Q = Math.pow(10.0, iQ/20.0);            // converted real value from input dB value
    w = 2.0 * Math.PI * gFreq;

    for(int i=0; i<64; i ++){
        omega[i] = 2.0 * (Math.PI) * (i+1) * FREQ_FIRST; 
        // xZ_r[i] = Math.cos(omega[i]/FS);
        // xZ_i[i] = Math.sin(omega[i]/FS);

        xZ[i].set(Math.cos(omega[i]/FS), Math.sin(omega[i]/FS));
        xZTemp1[i] = xZ[i];
        xZTemp2[i] = xZ[i];
    }  
    .  
    .
    .

Complex类在单独的Complex.java文件中定义,获得from here 对于这个Complex类,我还添加了一个无参数构造函数

public class Complex {
//    private final double re;   // the real part
//    private final double im;   // the imaginary part

private double re;   // the real part
private double im;   // the imaginary part


// create a new object with the given real and imaginary parts
public Complex() {
    re = 0.0;
    im = 0.0;
}


// create a new object with the given real and imaginary parts
public Complex(double real, double imag) {
    re = real;
    im = imag;
}
.
.
.

但是,在我的主程序中调用MathBiquadFncMatlab.fnMathBiquad(...)时,我会在NullPointerException

行获得xZ[i].set(Math.cos(omega[i]/FS), Math.sin(omega[i]/FS));

我认为这是由于xZ Complex类型数组而发生的。如何修复此NullPointerException

1 个答案:

答案 0 :(得分:1)

xZ[i]根据您的代码仍然为空。您无法在空对象上调用.set()。您需要做的是首先使用xZ[i]使用复杂对象初始化=

例如:

 for(int i=0; i<64; i ++){
    xZ[i] = new Complex();
 }