2011-09-11 9 views
0

設定iが、xとのSQLiteからY値を含むグラフを作っています。ヌル値

私のコードは次のようである:私は私のsqliteに任意のXY値を挿入していないとき

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()); 

、エラーメッセージが出てきた...私もi」がdidnのいくつかのコードを追加したいです

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()); 
    } 

が、私はそれを動作させることはできません:T Iは、このようなコードを作ってきた

..私のデータベースに任意のXY値を挿入し、チャートは、0と0値を出てきます、誰も私を解決するために私を助けることができます問題ですか?ありがとう

+0

この仕事はあなたのためでしょうか?もし(notesCursor.moveToFirst()){やる...)...しばらく);}他{mCurrentSeries.add(0.0、0.0);} –

+0

それが働いて、ウル助けをどうもありがとうございました:D – Handy

答えて

0

あなたの新しいコードはmCurrentSeries.addメソッドに渡されたときに、AとBがnullではないことを保証しません:

import java.util.HashMap; 

輸入java.util.Map;

パブリッククラスC { ダブル、B。 マップmCurrentSeries =新しい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); 
} 

}

1

値を非null(0.0)に初期化しますが、mCurrentSeries.addメソッドに値が渡される前に値がnullになることはありません。以前のコードでは、aとbがnullでない場合は、elseのelseが除外され、次にdoが実行されます。 noteCursor.getDoubleがaまたはbのいずれかにnullを戻している間は、aおよび/またはbはnullになります。何が必要なのあなたのmCurrentSeriesに到着したときにaddメソッドを経由してオブジェクトのnullでないこと、およびbのためである場合は、与えると、彼らはnullをしているとき、いくつかのデフォルト値をbにそのaddメソッドを変更する必要があります。

+0

こんにちは私は私のデータベースには何も挿入していなかったとき、aとbのデフォルト値は0.0になりますが、私は2番目のコード、aとbの操作を行う際に最初に初期化されなければならないコードを作りたい...私は何かが欠けています?前にありがとう – Handy