0

I just started learning Java today, so pardon my amateur mistakes. The issue is that instead of it showing each person's favorite color, it shows 'null', I have coding experience in Pawn so my guess would be I didn't either create the string properly or just didn't fill it, somehow.

Thanks!

I've tried these codes, but I get 'null' instead of each person's favorite color.

import java.io.*;
public class Employee {

   String name;
   int age;
   String designation;
   String favoriteColor;
   double salary;

   // This is the constructor of the class Employee
   public Employee(String name) {
      this.name = name;
   }

   // Assign the age of the Employee  to the variable age.
   public void empAge(int empAge) {
      age = empAge;
   }

   /* Assign the designation to the variable designation.*/
   public void empDesignation(String empDesig) {
      designation = empDesig;
   }

   /* Assign the salary to the variable salary.*/
   public void empSalary(double empSalary) {
      salary = empSalary;
   }

   public void empFavoriteColor(String empColor) {
       favoriteColor = empColor;
   }

   /* Print the Employee details */
   public void printEmployee() {
      System.out.println("Name:"+ name );
      System.out.println("Age:" + age );
      System.out.println("Designation:" + designation );
      System.out.println("Salary:" + salary);
      System.out.println("Favorite color:" + favoriteColor);
   }
}
import java.io.*;
public class EmployeeTest {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      Employee empOne = new Employee("James Smith");
      Employee empTwo = new Employee("Mary Anne");
      Employee empThree = new Employee("Alex Johnson");

      // Invoking methods for each object created
      empOne.empAge(26);
      empOne.empDesignation("Senior Software Engineer");
      empOne.empSalary(1000);
      empOne.printEmployee();
      empOne.empFavoriteColor("Green");

      empTwo.empAge(21);
      empTwo.empDesignation("Software Engineer");
      empTwo.empSalary(500);
      empTwo.printEmployee();
      empTwo.empFavoriteColor("Blue");

      empThree.empAge(16);
      empThree.empDesignation("Software Developer" );
      empThree.empSalary(250);
      empThree.printEmployee();
      empThree.empFavoriteColor("Orange");
   }
}

...

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 7
    You're setting the colour of each one _after_ you call the method that prints stuff out. The colour will be null before you have set it. – khelwood Aug 20 '19 at 23:07
  • Some code style issues: `empAge` -> `setAge`, etc. Instead of these set-functions it's likely better to initialize fields in constructor. The fields should be private, and you should provide getters and (maybe) setters for them. –  Aug 20 '19 at 23:13
  • @khelwood thank you. dyukha thanks for the correction, I'm a total newbie, so I have no idea what you just said, but when I get to understand Java better, I'll get back to your comment. –  Aug 20 '19 at 23:14
  • @DoniSmith You can accept what answer you found best. – new Q Open Wid Aug 20 '19 at 23:18
  • Getters/setters: https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work About constructors: you can (and probably should) initialize other fields in the same way as `name`. –  Aug 20 '19 at 23:23

2 Answers2

4

You are calling printEmployee() before empFavoriteColor(), so the string is null when you print it.

SCCC
  • 341
  • 3
  • 13
4

You need to call printEmployee(); before empFavoriteColor(), like so:

import java.io.*;
public class EmployeeTest {

   public static void main(String args[]) {
      /* Create two objects using constructor */
      Employee empOne = new Employee("James Smith");
      Employee empTwo = new Employee("Mary Anne");
      Employee empThree = new Employee("Alex Johnson");

      // Invoking methods for each object created
      empOne.empAge(26);
      empOne.empDesignation("Senior Software Engineer");
      empOne.empSalary(1000);
      empOne.empFavoriteColor("Green");
      empOne.printEmployee();

      empTwo.empAge(21);
      empTwo.empDesignation("Software Engineer");
      empTwo.empSalary(500);
      empTwo.empFavoriteColor("Blue");
      empTwo.printEmployee();

      empThree.empAge(16);
      empThree.empDesignation("Software Developer" );
      empThree.empSalary(250);
      empThree.empFavoriteColor("Orange");
      empThree.printEmployee();
   }
}
Aiyuni
  • 780
  • 5
  • 15