我有一个数组列表,它需要在最后返回allteams对象,但我无法做到这一点,当传递null时,它会给出null指针异常,请帮助我解决此问题。这是代码示例:
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
return allTeams;
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
return allTeams;
}
return null;
}
请帮助我解决此问题,我该怎么办才能消除此问题,而不是将其返回null,而是返回null。
答案 0 :(得分:0)
通过这些代码,如果不匹配任何东西,则在这种情况下返回null添加其他else并将其设置为默认值。无论它为null还是不匹配,都会执行else语句。
if()
{
}
else if()
{
}
else {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
ArrayList<Team> allTeams = new ArrayList<Team>();
allTeams.add(t1);
return allTeams;
}
答案 1 :(得分:0)
如果没有任何条件符合条件,您想返回一个数组吗? 因为最后要返回null,所以如果没有对应的内容,则可以尝试此操作。
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
ArrayList<Team> allTeams = new ArrayList<Team>();
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
return allTeams;
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
return allTeams;
}
return allTeams;
}
我将allTeams数组放在方法的开头。您可以尝试是否可行。但是,如果您真的想在无条件的情况下返回空值。但是您将需要检查返回的allTeam是否不为空。
答案 2 :(得分:0)
private ArrayList<Team> getData() {
Intent localIntent = getIntent();
localIntent.getIntExtra("det", 0);
ArrayList<Team> allTeams = new ArrayList<Team>();
if (localIntent.hasExtra("beg")) {
Team t1 = new Team("Day 1: Chest, Back, Shoulders, Legs, Biceps, Triceps");
t1.players.add("Chest – Barbell Bench Press – 4 sets of 8 reps");
t1.players.add("Back – Lat-pulldowns – 4 sets of 10 reps");
t1.players.add("Shoulders – Seated Dumbbell Press – 4 sets of 10 reps");
t1.players.add("Legs – Leg Extensions – 4 sets of 10 reps");
t1.players.add("Biceps – Barbell Bbicep Curls – 3 sets of 10 reps");
t1.players.add("Triceps – Triceps Rope Pushdowns – 3 sets of 15 reps");
Team t2 = new Team("Day 2: Legs, Triceps, Biceps, Chest, Back, Shoulder");
t2.players.add("Legs – Leg Press Machine – 4 sets of 8 reps");
t2.players.add("Triceps – Overhead Bar Extensions – 3 sets of 20 reps");
t2.players.add("Biceps – EZ Bar Curls – 4 sets of 10 reps");
t2.players.add("Chest – Machine Chest Press – 4 sets of 10 reps");
t2.players.add("Back – T-Bar Row – 4 sets of 10 reps");
t2.players.add("Shoulders – Lateral Raises – 3 sets of 20 reps");
Team t3 = new Team("Day 3: Shoulders, Back, Chest, Legs, Triceps, Biceps");
t3.players.add("Shoulders – Upright Rows – 3 sets of 15 reps");
t3.players.add("Back – Close-Grip Pulldowns – 4 sets of 12 reps");
t3.players.add("Chest – Fly – 4 sets of 10 reps");
t3.players.add("Legs – Lunges – 3 sets of 10 reps per leg");
t3.players.add("Triceps – Skullcrushers – 3 sets of 15 reps");
t3.players.add("Biceps – Hammer Curls – 3 sets of 12 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
} else if (localIntent.hasExtra("inter")) {
Team t1 = new Team("Day 1: Chest, Shoulders and Triceps");
t1.players.add("Bench Press – 3 sets of 10, 10, 8 (adding weight) reps");
t1.players.add("Incline Dumbbell Bench Press – 3 sets of 10 reps");
t1.players.add("Chest Dip – 3 sets of MAX reps");
t1.players.add("Skullcrushers – 3 sets of 8-10 Reps");
t1.players.add("One Arm Dumbbell Extension – 3 sets of 10 reps");
t1.players.add("Tricep Extension – 3 sets of 10 reps");
t1.players.add("Barbell Front Raise – 4 sets of 12 reps");
t1.players.add("Dumbbell Lateral Raise – 4 sets of 15, 12, 8, 8 (adding weight) reps");
Team t2 = new Team("Day 2: Back and Biceps");
t2.players.add("Wide Grip Pull Up 3 sets of MAX reps");
t2.players.add("Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Straight Arm Lat Pull Down – 3 sets of 10 reps");
t2.players.add("Standing Barbell Curl – 3 sets of 8-10 reps");
t2.players.add("Preacher Curl – 3 sets of 10 reps");
t2.players.add("Incline Dumbbell Curl – 3 sets of 10 reps");
Team t3 = new Team("Day 3: Legs");
t3.players.add("Squat – 4 sets of 10,10,8,8 reps");
t3.players.add("Dumbbell Lunge – 3 sets of 8 on each leg");
t3.players.add("Leg Press – 3 sets of 12 reps");
t3.players.add("Leg Curl – 3 sets of 15 reps");
t3.players.add("Leg Extension – 3 sets of 15 reps");
t3.players.add("Standing Calf Raise – 5 sets of 10,8,8,8,6 (heavy)reps");
t3.players.add("Seated Calf Raise – 5 sets of 15 (light) reps");
Team t4 = new Team("Day 4: Shoulders, chest, and Triceps");
t4.players.add("Barbell Bench Press – 3 sets of 10, 10, 8 reps");
t4.players.add("Dumbbell Flys – 3 sets of 10 reps");
t4.players.add("Cable Crossovers – 3 sets of 10 reps");
t4.players.add("Close Grip Bench Press – 4 sets of 10, 10, 8, 6 reps");
t4.players.add("Lying Dumbbell Extension – 3 sets of 10 reps");
t4.players.add("Tricep Kickback – 3 sets of 10 reps");
t4.players.add("Seated Dumbbell Press – 4 sets of 10, 10, 8, 8 reps");
t4.players.add("One Arm Cable Lateral Raise – 3 sets of 12 reps");
Team t5 = new Team("Day 5: Back and Biceps");
t5.players.add("Seated Row – 4 sets of 10 reps");
t5.players.add("Bent Over Barbell Row – 3 sets of 10 reps");
t5.players.add("Bent Over Row – 3 sets of 12 reps");
t5.players.add("Smith Machine Upright Row – 3 sets of 8-10 reps");
t5.players.add("Cable Curl – 4 sets of 8-10 reps");
t5.players.add("Concentration Curl – 3 sets of 10 reps");
t5.players.add("Reverse Barbell Curl – 3 sets of 10 reps");
allTeams.add(t1);
allTeams.add(t2);
allTeams.add(t3);
allTeams.add(t4);
allTeams.add(t5);
}
return allTeams;
}
尝试此方法,以及在将要使用此方法的任何地方,然后仅使用检查
ArrayList teamList = getData()
if(teamList.isNotEmpty()){
*do your stuff with teamList here*
}