Reputation: 129
So this program runs fine, its a cycling manager for learning purposes. The problem is when I try to get a list of one rider's abilities: the return values are all null, even though they are set in one of my methods. What have I done wrong? Scroll down to public void abilities, the mistake seems to be located in this area. Thanks in advance!
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");
}
}
Upvotes: 0
Views: 51
Reputation: 9862
use
c1.getName()
instead
getName()
because objects are null
by the default so you are getting values before initialize
.that's why you are getting null
c1 hold a instance of Cyclist class
. the values you set only update to instance which hold by c1 method local variable
Upvotes: 1
Reputation: 8187
You should use your Cyclist
object to get their values ,
System.out.println(c1.getName() + "s abilities:");
System.out.println("Mountain - " + c1.getMountain());
You set the values to Cyclist
object c1
and not to the getters.
Read How do getters and setters work?
Upvotes: 1