Hi everyone, I am having some problems with this problem. I know I am almost there but I think I am not quite getting it.
I get this error when I run it.
Exception in thread "main" java.lang.NullPointerException
at Newspapers.Subscribers.main(Subscribers.java:14)
Can you guide me to the right direction. Thank you.
Create a class named NewspaperSubscriber with fields for a subscriber’s street
address and the subscription rate. Include get and set methods for the subscriber’s
street address, and include get and set methods for the subscription rate. The set
method for the rate is abstract. Include an equals() method that indicates two
Subscribers are equal if they have the same street address. Create child classes
named SevenDaySubscriber, WeekdaySubscriber, and WeekendSubscriber. Each
child class constructor sets the rate as follows: SevenDaySubscribers pay $4.50 per
week, WeekdaySubscribers pay $3.50 per week, and WeekendSubscribers pay
$2.00 per week. Each child class should include a toString() method that returns
the street address, rate, and service type. Write an application named Subscribers
that prompts the user for the subscriber’s street address and requested service, and
then creates the appropriate object based on the service type. Do not let the user
enter more than one subscription type for any given street address. Save the files as
NewspaperSubscriber.java, WeekdaySubscriber.java, WeekendSubscriber.
java, SevenDaySubscriber.java, and Subscribers.java.
package Newspapers;
//Superclass
package Newspapers;
public abstract class NewspaperSubscriber {
protected String street_address;
protected double subscription_rate;
public void setStreet_address(String address){
street_address = address;
}
public String getStreet_address(){
return street_address;
}
public abstract void setSubscription_rate();
public double getSubscription_rate(){
return subscription_rate;
}
public boolean equals(NewspaperSubscriber subscriber){
boolean result;
if(street_address.equals(subscriber.street_address)){
result = true;
}else
result = false;
return result;
}
//Subclass
package Newspapers;
public class SevenDaySubscriber extends NewspaperSubscriber {
public String subType;
public SevenDaySubscriber(){
setSubscription_rate();
}
@Override
public void setSubscription_rate() {
subscription_rate = 4.50;
}
public String toString(){
return "The street address for subscriber is: " + street_address + "\nThe subscription rate is: " + getSubscription_rate() +
"\nThe service entered was: " + subType;
}
}
//Subclass
package Newspapers;
public class WeekdaySubscriber extends NewspaperSubscriber {
public String subType;
public WeekdaySubscriber() {
setSubscription_rate();
}
@Override
public void setSubscription_rate() {
subscription_rate = 3.50;
}
public String toString(){
return "The street address for subscriber is: " + street_address + "\nThe subscription rate is: " + getSubscription_rate() +
"\nThe service entered was: " + subType;
}
}
//Subclass
package Newspapers;
public class WeekendSubscriber extends NewspaperSubscriber {
public String subType;
public WeekendSubscriber() {
setSubscription_rate();
}
@Override
public void setSubscription_rate() {
subscription_rate = 2.00;
}
public String toString(){
return "The street address for subscriber is: " + street_address + "\nThe subscription rate is: " + getSubscription_rate() +
"\nThe service entered was: " + subType;
}
}
//testClass
package Newspapers;
import javax.swing.*;
public class Subscribers {
public static void main(String[] args) {
NewspaperSubscriber[] subscribers = new NewspaperSubscriber[6];
for (int x = 0; x < subscribers.length; x++) {
String address = JOptionPane.showInputDialog(null, "Enter your address: ");
subscribers[x].setStreet_address(address);
int userService = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the service you want (1: seven day Subscription, " +
"2: weekly subscription, 3: weekend subscription "));
if (userService == 1) {
subscribers[x] = new SevenDaySubscriber();
} else if (userService == 2) {
subscribers[x] = new WeekdaySubscriber();
} else if (userService == 3) {
subscribers[x] = new WeekendSubscriber();
}
for(int y = 0; y < x; y++){
if(subscribers[x].getStreet_address().equals(subscribers[x].getStreet_address())){
System.out.println("Sorry, you just entered a duplicate subscription!");
--x;
}
}
}
System.out.println("\n\nThe subscriptions are:\n");
for(int j = 0; j < subscribers.length; j++){
subscribers[j].toString();
}
}
}