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();
        }

    }
}

Exactly which line of code is that?
NPE usually means you have an uninitialised reference variable or value returned from a method call. Look at that line of code, see what variables and method calls are used in it. If there's more than one, print them all so you can see which is null. Then follow the logic backwards from there to see why it has not been initialised.

also, just some remarks:

public boolean equals(NewspaperSubscriber subscriber){
        boolean result;
        if(street_address.equals(subscriber.street_address)){
            result = true;
        }else
            result = false;
        return result;
    }
  1. it is better to limit the number of lines code. it makes the code easier to read and understand, and it decreases the risk of typo's or other mistakes.

The above method can be reduced to:

public boolean equals(NewspaperSubscriber subscriber){
        return street_address.equals(subscriber.street_address);
    }
  1. very important: decide what the method should do. Do you want to use this as 'the ' equals method? then you should change the type of the parameter, so it will be an override of the actual method:

    public boolean equals(Object subscriber){
    if ( ! subscriber instanceof NewspaperSubscriber ){
    return false;
    }
    NewspaperSubscriber person = (NewspaperSubscriber)subscriber;
    return street_address.equals(person.street_address);
    }

otherwise you'll end up in cases (for example in sorting for a map) where you'll get "strange" results. An other sollution would be to re-name your method to isEqualSubscriber (or any other name).

First of all try usin JUnit and unit test the methods one by one

@Shulc besides the fact that this is a dead thread, how would that help anything?
to shine some light on the issue at hand:

 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);
            // rest of code

the array subscribers was initialized just fine, the elements there-in, on the other hand, were not, so are still null-values. The moment he tries/tried to set

subscribers[x].setStreet_address(address);

while subscribers[x] isn't initialized, this results in the NPE.
When putting a unit test on it, this would be the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.