程序运行,仅将空值作为返回值

时间:2014-10-23 09:48:23

标签: java null

所以这个程序运行正常,它是一个学习目的的自行车经理。问题是当我试图得到一个骑手的能力列表时:返回值都是空的,即使它们是在我的一个方法中设置的。我做错了什么?向下滚动到公共虚空能力,错误似乎位于此区域。提前谢谢!

import java.util.Scanner;

public class CyclingManager2 {
public static void main(String[] args) {

    //menyvalgene
    Menu m = new Menu();
    m.choice();

    }
}

class Menu {
Scanner in = new Scanner(System.in);
Cyclist cy = new Cyclist();

//choices
public void choice() {

    int choice = -1;
    while (choice != 0) {

        System.out.println("Choose something: ");
        System.out.println("-0 will exit the program" + "\n-Pressing 1 will open the database menu");
        choice = in.nextInt();
        switch(choice) {
            case 0: choice = 0; break;
            case 1: cy.database(); break;
            default: System.out.println("You have to choose either 0 or 1"); break;
        }

    }
}
}

class Cyclist {

private String name;
private int mountain;
private int timeTrial;
private int sprint;
private int age;



public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}


public void setMountain(int mountain) {
    this.mountain = mountain;
}

public int getMountain() {
    return mountain;
}


public void setTimeTrial(int timeTrial) {
    this.timeTrial = timeTrial;
}

public int getTimeTrial() {
    return timeTrial;
}

public void setSprint(int sprint) {
    this.sprint = sprint;
}

public int getSprint() {
    return sprint;
}



public void setAge(int age) {
    this.age = age;
}


public int getAge() {
    return age;
}




public void abilities() {

    //Pardilla blir til!
    Cyclist c1 = new Cyclist();

    c1.setName("Sergio Pardilla");
    c1.setMountain(75);
    c1.setTimeTrial(60);
    c1.setSprint(60);
    c1.setAge(30);

    System.out.println(getName() + "s abilities:");
    System.out.println("Mountain - " + getMountain());
    System.out.println("TimeTrial - " + getTimeTrial());
    System.out.println("Sprint - " + getSprint());
    System.out.println("Age - " +getAge());


    }

//databasemenu

public void database() {

    Scanner in = new Scanner(System.in);

    System.out.println("Welcome to the database \nThese are the options:\n0 = Quit\n1: Abilities");
    int dbChoice = -1;

    while (dbChoice != 0) {

        System.out.println();
        dbChoice = in.nextInt();
        switch(dbChoice) {
            case 0: dbChoice = 0; break;
            case 1: abilities(); break;
            default: System.out.println("Choose either 0 or 1"); break;
        }

    } in.close();
}


public void riders() {


    System.out.println("Following riders are available in this database");
}



}

2 个答案:

答案 0 :(得分:1)

使用

c1.getName()

而不是

getName()

因为默认情况下对象为null,所以您在initialize之前获得了值。这就是您获得null的原因

c1持有Cyclist class的实例。您设置的值仅更新为由c1方法局部变量

保存的实例

答案 1 :(得分:1)

您应该使用Cyclist对象来获取其值,

System.out.println(c1.getName() + "s abilities:");
System.out.println("Mountain - " + c1.getMountain());

您将值设置为Cyclist对象c1,而不是吸气剂。

阅读How do getters and setters work?