I am having an issue with a checkbox and adding items to a database in Sql. The issue is that I am getting a null pointer exception at line 171 which is:
db.addTimer(new TimerClass(tagval1, String.valueOf(startTime*60)));
this entry is basically a mirror to the following line which I use to add a default value to my database if it is now created:
db.addTimer(new TimerClass("Lipton", "180"));
that being said the checkbox value that I want to check to trigger the first line is set within the show dialog code listed below:
public void showDialog(){
cusd = new Dialog(MainActivity.this,R.style.Theme_MultiTimer_Dialog);
cusd.setContentView(R.layout.dialogbox);
canlbtn = (Button) cusd.findViewById(R.id.cancelbtn);
sbtbtn = (Button) cusd.findViewById(R.id.submitbtn);
minnp = (NumberPicker) cusd.findViewById(R.id.numberPicker1);
secnp = (NumberPicker) cusd.findViewById(R.id.numberPicker2);
tagvalue = (EditText) cusd.findViewById(R.id.tagname);
ckb = (CheckBox) cusd.findViewById(R.id.savecheckBox);
ckb.setOnClickListener(this);
canlbtn.setOnClickListener(this);
sbtbtn.setOnClickListener(this);
minnp.setMaxValue(59);
minnp.setMinValue(0);
minnp.setWrapSelectorWheel(false);
minnp.setOnValueChangedListener(this);
secnp.setMaxValue(59);
secnp.setMinValue(0);
secnp.setWrapSelectorWheel(false);
secnp.setOnValueChangedListener(this);
cusd.show();
}
now I also have in the same dialog two (2) number pickers and an EditText that I pull values from comfortably. The issue is when I try to add the entry into the database itself. Below is the addtimer code which is generic code to add to a database and as specified above it works comfortably with the second line of code that I listed above.
public void addTimer(TimerClass timers){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TAG, timers.getTag());
values.put(KEY_TIMEINSECONDS, timers.gettimeInSeconds());
db.insert(TABLE_NAME, null, values);
db.close();
}
please see below where I declared the necessary items and also the code where I instantiated the different code.
public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener, OnClickListener, OnItemSelectedListener{
//--------------------------------------------------------------------------------------------------
Button dgbtn, abbtn, exbtn, canlbtn, sbtbtn, starest, clrbtn, deltbtn;
EditText tagvalue;
TextView timeRemaining, titleTvRight, titleTvLeft;
NumberPicker minnp, secnp;
Dialog cusd;
ProgressBar titleProgressBar;
TimerDataBaseHandler db;
Spinner s;
CheckBox ckb;
protected CountDownTimer timerCountDownTimer;
private boolean timerHasStarted = false;
private TextView timeElapsedView;
protected int startTime=0;
protected int val1, val2, val3, val4;
protected String tagval1;
boolean customTitleSupported, savechkbx;
File mydb ;
//--------------------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
customTitleSupported= requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
TimerDataBaseHandler db = new TimerDataBaseHandler(getApplicationContext());
mydb = new File("/data/data/com.vertygoeclypse.multitimer/databases/TimerManager");
if(mydb.exists()){
Log.i("VertygoEclypse", "True");
} else {
Log.i("VertygoEclypse", "false");
db.addTimer(new TimerClass("Lipton", "180"));
}
//--------------------------------------------------------------------------------------------------
dgbtn = (Button) findViewById(R.id.dialogbtn);
abbtn = (Button) findViewById(R.id.aboutbtn);
exbtn = (Button) findViewById(R.id.exitbtn);
starest = (Button) findViewById(R.id.startresetbtn);
clrbtn = (Button) findViewById(R.id.clearvaluesbutton);
deltbtn = (Button) findViewById(R.id.deletevaluesbutton);
timeRemaining = (TextView) findViewById(R.id.timeremainingview);
titleTvLeft = (TextView)findViewById(R.id.titleTVLeft);
titleTvRight = (TextView)findViewById(R.id.titleTVRight);
titleProgressBar = (ProgressBar)findViewById(R.id.leadprogressBar);
s = (Spinner)findViewById(R.id.timerspinner);
//--------------------------------------------------------------------------------------------------
dgbtn.setOnClickListener(this);
abbtn.setOnClickListener(this);
exbtn.setOnClickListener(this);
starest.setOnClickListener(this);
clrbtn.setOnClickListener(this);
deltbtn.setOnClickListener(this);
s.setOnItemSelectedListener(this);
Can anyone please help on this, I am sure that the issue is something I am missing but, being new to android programming, I guess I cannot see it.
Also please see the logcat captures below, where I was pointed to line 171 as the place of the issue.
01-07 21:58:49.650 1971-1971/com.vertygoeclypse.multitimer I/VertygoEclypse﹕ True
01-07 21:58:49.678 1971-1971/com.vertygoeclypse.multitimer D/dalvikvm﹕ GC_FOR_ALLOC freed 74K, 10% free 2906K/3212K, paused 3ms, total 3ms
01-07 21:58:49.698 1971-1971/com.vertygoeclypse.multitimer D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
01-07 21:58:49.698 1971-1971/com.vertygoeclypse.multitimer D/﹕ HostConnection::get() New Host Connection established 0xb8db9bf0, tid 1971
01-07 21:58:49.710 1971-1971/com.vertygoeclypse.multitimer D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
01-07 21:58:49.710 1971-1971/com.vertygoeclypse.multitimer D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
01-07 21:58:49.774 1971-1971/com.vertygoeclypse.multitimer W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
01-07 21:58:49.774 1971-1971/com.vertygoeclypse.multitimer E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
01-07 21:58:49.782 1971-1971/com.vertygoeclypse.multitimer E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
01-07 21:58:49.782 1971-1971/com.vertygoeclypse.multitimer D/OpenGLRenderer﹕ Enabling debug mode 0
01-07 21:59:22.058 1971-1971/com.vertygoeclypse.multitimer D/dalvikvm﹕ GC_FOR_ALLOC freed 44K, 8% free 3327K/3612K, paused 7ms, total 7ms
01-07 21:59:22.070 1971-1971/com.vertygoeclypse.multitimer D/dalvikvm﹕ GC_FOR_ALLOC freed 15K, 8% free 3715K/4028K, paused 2ms, total 2ms
01-07 21:59:22.154 1971-1971/com.vertygoeclypse.multitimer W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
01-07 21:59:22.238 1971-1971/com.vertygoeclypse.multitimer V/RenderScript﹕ 0xb8eb9850 Launching thread(s), CPUs 4
01-07 21:59:23.826 1971-1971/com.vertygoeclypse.multitimer D/dalvikvm﹕ GC_FOR_ALLOC freed 60K, 7% free 4167K/4460K, paused 1ms, total 2ms
01-07 21:59:27.154 1971-1971/com.vertygoeclypse.multitimer D/AndroidRuntime﹕ Shutting down VM
01-07 21:59:27.154 1971-1971/com.vertygoeclypse.multitimer W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4bee648)
01-07 21:59:27.158 1971-1971/com.vertygoeclypse.multitimer E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.vertygoeclypse.multitimer.MainActivity.onClick(MainActivity.java:171)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Thanks in advance all who can assist me, I appreciate your time and effort.
regards
cchinchoy
The solution came to me after a lot of code trial and error. I realized that the code to the check box is wrong and that :
ckb.setOnClickListener(this);
into this:
ckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
what this does is that it brings the following into play:
ckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
savechkbx = true;
} else {
savechkbx = false;
}
}
});
I have created a declaration of the savechkbx as a Boolean and then set it to false. This is to counterbalance the fact that I had the checkbox set to true in the xml file like this:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:layout_marginRight="5sp"
android:layout_marginTop="1sp"
android:text="Saver Timer for Later use!!!"
android:id="@+id/savecheckBox"
android:checked="true" >
</CheckBox>
With that being said what then happens is that on the submit button check, I have the following:
if(savechkbx==true){
db.addTimer(new TimerClass(tagval1, temp2));
}
else it returns nothing. I have to modify the code to make it cleaner however. This solves the question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.