java (android) 中用户定义的静态变量出现 NullPointerException
NullPointerException in user defined static variables in java (android)
我有一个带有静态函数的 class 我想在其他地方使用:
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 class 在单独的 Complex.java 文件中定义,获得 from here.
对于这个 Complex class 我还添加了一个无参数构造函数
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(...)
时,我在 xZ[i].set(Math.cos(omega[i]/FS), Math.sin(omega[i]/FS));
行得到一个 NullPointerException
我认为这是因为 xZ
是一个 Complex
类型的数组。我该如何解决这个 NullPointerException
?
xZ[i]
根据您的代码仍然为空。您不能对空对象调用 .set()
。您需要做的是首先使用复杂对象在某处使用 =
初始化 xZ[i]
。
例如:
for(int i=0; i<64; i ++){
xZ[i] = new Complex();
}
我有一个带有静态函数的 class 我想在其他地方使用:
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 class 在单独的 Complex.java 文件中定义,获得 from here.
对于这个 Complex class 我还添加了一个无参数构造函数
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(...)
时,我在 xZ[i].set(Math.cos(omega[i]/FS), Math.sin(omega[i]/FS));
NullPointerException
我认为这是因为 xZ
是一个 Complex
类型的数组。我该如何解决这个 NullPointerException
?
xZ[i]
根据您的代码仍然为空。您不能对空对象调用 .set()
。您需要做的是首先使用复杂对象在某处使用 =
初始化 xZ[i]
。
例如:
for(int i=0; i<64; i ++){
xZ[i] = new Complex();
}