ThePeskyWabbit ThePeskyWabbit - 18 days ago 8
Android Question

Unable to start activity ComponentInfo: java.lang.NumberFormatException: For input string: ""

I have a list of check boxes and if checked, they add the day of the week to an array list. I want to pass that arraylist to another class as seen in the sendTo()

here is my code from the Sel_Dates class:

package com.example.joshpc.bluetoothattendee;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import java.util.ArrayList;

public class Sel_Dates extends AppCompatActivity {
CheckBox mon;
CheckBox tues;
CheckBox wed;
CheckBox thur;
CheckBox fri;
Button selectBut;
ArrayList<String> days = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sel_dates);

mon = (CheckBox) findViewById(R.id.cbMon);
tues = (CheckBox) findViewById(R.id.cbTues);
wed = (CheckBox) findViewById(R.id.cbWed);
thur = (CheckBox) findViewById(R.id.cbThurs);
fri = (CheckBox) findViewById(R.id.cbFri);
selectBut = (Button) findViewById(R.id.bSelectDate);

selectBut.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
getDates();
sendTo();
}
});
}

private void getDates(){
if(mon.isChecked())
days.add("monday");
if(tues.isChecked())
days.add("tuesday");
if(wed.isChecked())
days.add("wednesday");
if(thur.isChecked())
days.add("thursday");
if(fri.isChecked())
days.add("friday");
}
private void sendTo(){
Intent intent = new Intent(this, Create_Class.class);
intent.putStringArrayListExtra("days", days);
startActivity(intent);
}
}


and my debug output:

FATAL EXCEPTION: main
Process: com.example.joshpc.bluetoothattendee, PID: 2318
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.joshpc.bluetoothattendee/com.example.joshpc.bluetoothattendee.Create_Class}: java.lang.NumberFormatException: For input string: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at com.example.joshpc.bluetoothattendee.Create_Class.onCreate(Create_Class.java:38)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 


Create_Class code:

package com.example.joshpc.bluetoothattendee;

import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

public class Create_Class extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_class);

final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference fbRef = FirebaseDatabase.getInstance().getReference();

Button createBut = (Button) findViewById(R.id.bCreate);
EditText etstartTime = (EditText) findViewById(R.id.etStartTime);
EditText etclassID = (EditText) findViewById(R.id.etID);
EditText etname = (EditText) findViewById(R.id.etClassName);

final ArrayList<String> days = getIntent().getStringArrayListExtra("days");
final String teacherID = user.getUid();
final String name = etname.getText().toString();
final String classID = etclassID.getText().toString();
final int startTime = Integer.parseInt(etstartTime.getText().toString());

createBut.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ClassObj newClass = new ClassObj(name, classID, teacherID, startTime, days);
fbRef.child(user.getUid()).setValue(newClass);
sendTo(Main__Menu.class);
}
});

}

private void sendTo(Class s){
Intent intent = new Intent(this, s);
startActivity(intent);
}
}


edit: I think the problem is coming from these lines of code

final int startTime = Integer.parseInt(etstartTime.getText().toString());

createBut.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
ClassObj newClass = new ClassObj(name, classID, teacherID, startTime, days);
fbRef.child(user.getUid()).setValue(newClass);
sendTo(Main__Menu.class);
}
});


specifically where I parse startTime which is read in as a string in the format HH:MM. going to change this to a string value rather than an int and see what happens.

Answer

you can't put empty string to a number data, if empty it should be 0.

Comments