在我的应用中的某个地方。我需要从方法返回ContentValues。但似乎我无法做到这一点,或者在实现它时我会遗漏一些东西。当我想将这个返回值传递给另一个类方法时,它会因为nullPointerException而崩溃。
这是我调用返回的ContentValues方法并将其传递给其他人的地方:
new Thread(new Runnable() {
public void run() {
try{
Thread.sleep(3000);
generateFeatures gen=new generateFeatures(getBaseContext());
ContentValues ourContentValues=gen.letsRollOut(); //calling the method that returns smth. type ContentValues
makeDesicion md=new makeDesicion(ourContentValues,getBaseContext());
md.controlWithDB();
md.finalDesicion();
db.delete("Koordinatlar", null, null);
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}).start();
返回ContentValues的方法:
public ContentValues letsRollOut(){
boolean bitti=false;
Cursor firstTimeHandler=db.rawQuery("SELECT MIN(zaman) FROM Koordinatlar", null);
firstTimeHandler.moveToFirst();
long firstTime= firstTimeHandler.getLong(0);
long nextTime=firstTime+3000;
Cursor lastTimeHandler=db.rawQuery("SELECT MAX(zaman) FROM Koordinatlar", null);
lastTimeHandler.moveToFirst();
long lastTime=lastTimeHandler.getLong(0);
while(!bitti){
myContentValues.clear();
calculateAverage(firstTime,nextTime);
calculateStandartDev(firstTime,nextTime);
calculateAverageAbsoluteDifferance(firstTime,nextTime);
calculateAverageResultant(firstTime,nextTime);
calculateMax(firstTime,nextTime);
calculateBinnedDist(firstTime,nextTime);
firstTime=(long) (nextTime+0.00000001);
nextTime=(nextTime+3000);
if(nextTime>=lastTime){
bitti=true;
}
myContentValues.put("ortX", averageX);
myContentValues.put("ortY", averageY);
myContentValues.put("ortZ", averageZ);
myContentValues.put("stdX", stdX);
myContentValues.put("stdY", stdY);
myContentValues.put("stdZ", stdZ);
myContentValues.put("aadX", aadX);
myContentValues.put("aadY", aadY);
myContentValues.put("aadZ", aadZ);
myContentValues.put("maxX", maxX);
myContentValues.put("maxY", maxY);
myContentValues.put("maxZ", maxZ);
myContentValues.put("averageResultantAcc", averageResultantAcc);
}
return myContentValues;
}
这是我收到错误的地方。
public makeDesicion(ContentValues ourContentValues,Context con){
testContentValues.putAll(ourContentValues); // get nullpointer error in this line.
ourContext=con;
veriTabani=new VeriTabani(this.ourContext);
db=veriTabani.getWritableDatabase();
}
答案 0 :(得分:1)
您似乎忘了初始化testContentValues
:
testContentValues = new ContentValues();