开发者

setting null value

i'm making a chart which contain x and y value from sqlite.

my code is like this :

   Double a, b;

       notesCursor.moveToFirst();
   do {
       a = notesCursor.getDouble(notesCursor.getColumnIndex(DbAdapter.KEY_ROWID));
       b = notesCursor.getDouble(notesCursor.getColumnIndex(DbAdapter.KEY_RESULT));  
       mCurrentSeries.add(a, b);
   }while(notesCursor.moveToNext());

when i didn't insert any x y value to my sqlite, the errors message came out... i want to add some code which when even i didn't insert any x y value to my d开发者_如何学编程atabase, the chart will come out with 0, 0 value..

i've been making a code like this :

Double a, b;
   if(a==null && b==null){
       mCurrentSeries.add(0.0, 0.0);
   }
   else{      
       notesCursor.moveToFirst();
   do {
       a = notesCursor.getDouble(notesCursor.getColumnIndex(DbAdapter.KEY_ROWID));
       b = notesCursor.getDouble(notesCursor.getColumnIndex(DbAdapter.KEY_RESULT));  
       mCurrentSeries.add(a, b);
   }while(notesCursor.moveToNext());
   }

but i can't make it work, anyone can help me to solve this problem? thank you


You code INITIALISES the values to non null (0.0) but it never guarantees that they won't be rest to null before they are given to the mCurrentSeries.add method. In you previous code, if a and b start as not null, then the else in the if else will be exected, then the do while. If in the do while the noteCursor.getDouble returns null for either a or b, then a and/or b will be null. If what you need is for a and b to not be null when arriving into your mCurrentSeries object via the add method, you should modify that add method to give a and b some default values when they're null.


Your new code does not guarantee that a and b will be not null when passed to the mCurrentSeries.add method:

import java.util.HashMap;

import java.util.Map;

public class C { Double a, b; Map mCurrentSeries = new HashMap(); NotesCursor notesCursor = new NotesCursor();

public void yourMethod() {
    if (a == null && b == null) {
        mCurrentSeries.put(0.0, 0.0);
    } else {
        // notesCursor.moveToFirst();
        do {
            a = notesCursor.getDouble();
            b = notesCursor.getDouble();
            mCurrentSeries.put(a, b);
        } while (notesCursor.moveToNext());
    }
}

private static class NotesCursor {
    boolean b = false;

    public Double getDouble() {
        return null;
    }

    public boolean moveToNext() {
        return !b;
    }
}

public static void main(String... args) {
    C c = new C();
    c.yourMethod();
    System.out.println("a="+c.a);
    System.out.println("b="+c.b);
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜