设置为空值

问题描述:

我正在制作一个包含来自sqlite的x和y值的图表。设置为空值

我的代码是这样的:

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

的时候我没有插入任何XY值到我的sqlite的,错误的消息出来了......我想添加一些代码,当即便我没有”牛逼插入任何XY值到我的数据库,图表将推出0,0值..

我一直在做这样的代码:

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

,但我不能让它工作任何人都可以帮助我解决问题是问题吗?谢谢

+0

将这项工作吗? if(notesCursor.moveToFirst()){do ... while ...));} else {mCurrentSeries.add(0.0,0.0);} –

+0

它的工作,非常感谢你的帮助:D – Handy

您的新密码并不能保证一个并传递给mCurrentSeries.add方法时,B将被不为空:

import java.util.HashMap; 

进口java.util.Map;

公共课C {Double35,Double2,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); 
} 

}

您将代码初始化为非null(0.0)的值,但它不会保证它们在被赋予mCurrentSeries.add方法之前不会空闲为空。在你以前的代码中,如果a和b开始为非空,那么else中的else将被执行,然后执行while。如果在noteCursor.getDouble为a或b返回null的情况下,则a和/或b将为null。如果通过add方法到达mCurrentSeries对象时,如果需要的是a和b不为空,则应该修改该add方法,以便在a和b为null时为其提供一些默认值。

+0

嗨,我想要做一个代码,当我没有插入任何东西到我的数据库中,a和b的默认值是0.0,但是当我做第二个代码时,a和b必须先初始化...我是否错过了一些东西?以前感谢 – Handy