This program basically iterates a hashset collection of greetings. The greetings are stored in the manager class in a hashSet called greeters. What I have to do is create a method that iterates a over the greeters and prints it out depending on a set of variables (isHappy and greetTimes). Is happy determines whether or not the greetings will have an exclamation point or not and the greetTimes determines the number of times the greeting will show up. However, when I run the allGreet method I keep getting a null pointer exception. Any help is appreciated :) Here is the exact text of the error message:
java.lang.NullPointerException at Manager.allGreet(Manager.java:51)
Here are the classes:
import java.util.HashSet;
import java.util.*;
/**
* Class manager
*/
public class Manager
{
/**
* Constructor for objects of class Manager
*/
public Manager()
{
HashSet <Object> greeters = new HashSet<Object>();
// add elements to the hash set
greeters.add("Hola");
greeters.add("G'Day");
greeters.add("Ciao");
for(Object greeter : greeters){
System.out.println(greeter);
}
}
/**
* all the different greetings for the Greeter
*/
public static void main(Object args[]) {
Manager hi = new Manager();
hi.allGreet();
}
public String greeting = "Bonjour";
public int greetTimes = 6;
public boolean isHappy = true;
public HashSet greeters;
/**
* allGreet Method
*/
public void allGreet()
{
if(greetTimes < 0) {
System.out.println("Invalid number of greetings!");
}
else {
if(isHappy == true){
for(Object greeter : greeters){
System.out.println(greeter + "!");
}
}
else {
for(Object greeter : greeters){
System.out.println(greeter);
}
}
}
}
}
/**
* Class Greeter:
*
* Hello-world program to demonstrate BlueJ.
*/
class Greeter
{
/**
* Method that does the work
*/
public void go()
{
if(greetTimes < 0) {
System.out.println("Invalid number of greetings!");
}
else {
if(isHappy == true){
for (int i = 0; i < greetTimes; i++) {
System.out.println(greeting + "!");
}
}
else {
for (int i = 0; i < greetTimes; i++) {
System.out.println(greeting);
}
}
}
}
public String greeting = "Bonjour";
public int greetTimes = 6;
public boolean isHappy = true;
public Greeter(String greeting, int greetTimes, boolean isHappy)
{
this.greeting = greeting;
this.greetTimes = greetTimes;
this.isHappy = isHappy;
}
public Greeter()
{
this.greeting = greeting;
}
/**
* main method for testing outside BlueJ
*/
public static void main(String[] args)
{
Greeter hi = new Greeter();
hi.go();
}
public String getGreeting()
{
return greeting;
}
public void setGreeting(String greeting)
{
this.greeting = greeting;
}
public int getgreetTimes()
{
return greetTimes;
}
public void setgreetTimes(int greetTimes)
{
this.greetTimes = greetTimes;
}
public boolean getisHappy()
{
return isHappy;
}
public void setisHappy(boolean isHappy)
{
this.isHappy = isHappy;
}
}