Как вернуть объект списка массивов в конце условия if? - PullRequest
0 голосов
/ 22 ноября 2018

У меня есть список массивов, и он должен вернуть объект allteams в конце, но я не могу этого сделать, и при передаче значения 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;
}

Пожалуйста, помогите мне решить эту проблему, что я могу сделать, чтобы избавиться от этой проблемы в конце, вместо нуля, она должна возвращать все команды.

Ответы [ 3 ]

0 голосов
/ 22 ноября 2018

А вы хотите вернуть массив, если ничего не соответствует условию?Поскольку вы возвращаете 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 в начало метода.Вы можете попробовать, если это работает.Но если вы действительно хотите вернуть null , когда нет условий.Но вам нужно проверить, не является ли возвращаемый allTeams пустым.

0 голосов
/ 22 ноября 2018
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*
}
0 голосов
/ 22 ноября 2018

Этим кодом, если он не соответствует ни одной вещи, его возвращаемое значение NULL для этих случаев. Добавьте Last else и задайте ему значения по умолчанию.

...