Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:05:56+00:00 2026-06-12T04:05:56+00:00

A brief background before so we can communicate on the same wave length. I’ve

  • 0

A brief background before so we can communicate on the same wave length. I’ve had about 8-10 university courses on programming from data structure, to one on all languages, to specific ones such as java & c++. I’m a bit rusty because i usually take 2-3 month breaks from coding. This is a personal project that I started thinking of two years back.

Okay down to the details, and a specific question, I’m having problems with my mutator functions. It seems to be that I am trying to access a private variable incorrectly. The question is, am I nesting my classes too much and trying to mutate a base class variable the incorrect way. If so point me in the way of the correct literature, or confirm this is my problem so I can restudy this information. Thanks

package GroceryReceiptProgram;

import java.io.*;
import java.util.Vector;

public class Date {

    private int hour, minute, day, month, year;

    Date() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What's the hour? (Use 1-24 military notation");
            hour = Integer.parseInt(keyboard.readLine());
            System.out.println("what's the minute? ");
            minute = Integer.parseInt(keyboard.readLine());
            System.out.println("What's the day of the month?");
            day = Integer.parseInt(keyboard.readLine());
            System.out.println("Which month of the year is it, use an integer");
            month = Integer.parseInt(keyboard.readLine());
            System.out.println("What year is it?");
            year = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (IOException e) {
            System.out.println("Yo houston we have a problem");
        }

    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public void setHour() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What hour, use military notation?");
            this.hour = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getHour() {
        return hour;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public void setMinute() {
        try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.println("What minute?");
            this.minute = Integer.parseInt(keyboard.readLine());
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ": doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString() + ": minute shall not cooperate");
        } catch (NullPointerException e) {
            System.out.println(e.toString() + ": in the setMinute function of the Date class");
        }
    }

    public int getMinute() {
        return minute;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setDay() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What day 0-6?");
            this.day = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getDay() {
        return day;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public void setMonth() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What month 0-11?");
            this.month = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getMonth() {
        return month;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setYear() {
        try {
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("What year?");
            this.year = Integer.parseInt(keyboard.readLine());
            keyboard.close();
        } catch (NumberFormatException e) {
            System.out.println(e.toString() + ":doesnt seem to be a number");
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    public int getYear() {
        return year;
    }

    public void set() {
        setMinute();
        setHour();
        setDay();
        setMonth();
        setYear();
    }

    public Vector<Integer> get() {
        Vector<Integer> holder = new Vector<Integer>(5);
        holder.add(hour);
        holder.add(minute);
        holder.add(month);
        holder.add(day);
        holder.add(year);
        return holder;
    }
};

That is the Date class obviously, next is the other base class Location.

package GroceryReceiptProgram;

    import java.io.*;
    import java.util.Vector;

    public class Location {

        String streetName, state, city, country;
        int zipCode, address;

        Location() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What is the street name");
                streetName = keyboard.readLine();
                System.out.println("Which state?");
                state = keyboard.readLine();
                System.out.println("Which city?");
                city = keyboard.readLine();
                System.out.println("Which country?");
                country = keyboard.readLine();
                System.out.println("Which zipcode?");//if not u.s. continue around this step
                zipCode = Integer.parseInt(keyboard.readLine());
                System.out.println("What address?");
                address = Integer.parseInt(keyboard.readLine());
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public void setZipCode(int zipCode) {
            this.zipCode = zipCode;
        }

        public void setZipCode() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What zipCode?");
                this.zipCode = Integer.parseInt(keyboard.readLine());
                keyboard.close();
            } catch (NumberFormatException e) {
                System.out.println(e.toString() + ":doesnt seem to be a number");
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public void set() {
            setAddress();
            setCity();
            setCountry();
            setState();
            setStreetName();
            setZipCode();
        }

        public int getZipCode() {
            return zipCode;
        }

        public void setAddress(int address) {
            this.address = address;
        }

        public void setAddress() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.address = Integer.parseInt(keyboard.readLine());
                keyboard.close();
            } catch (NumberFormatException e) {
                System.out.println(e.toString() + ":doesnt seem to be a number");
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public int getAddress() {
            return address;
        }

        public void setStreetName(String streetName) {
            this.streetName = streetName;
        }

        public void setStreetName() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.streetName = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getStreetName() {
            return streetName;
        }

        public void setState(String state) {
            this.state = state;
        }

        public void setState() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.state = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getState() {
            return state;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public void setCity() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.city = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getCity() {
            return city;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public void setCountry() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.country = keyboard.readLine();
                keyboard.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getCountry() {
            return country;
        }
    };

their parent(What is the proper name?) class

package GroceryReceiptProgram;

    import java.io.*;

    public class FoodGroup {

        private int price, count;
        private Date purchaseDate, expirationDate;
        private Location location;
        private String name;

        public FoodGroup() {
            try {
                setPrice();
                setCount();
                expirationDate.set();
                purchaseDate.set();
                location.set();
            } catch (NullPointerException e) {
                System.out.println(e.toString() + ": in the constructor of the FoodGroup class");
            }
        }

        public void setPrice(int price) {
            this.price = price;
        }

        public void setPrice() {
            try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
                System.out.println("What Price?");
                price = Integer.parseInt(keyboard.readLine());

            } catch (NumberFormatException e) {
                System.out.println(e.toString() + ":doesnt seem to be a number");
            } catch (IOException e) {
                System.out.println(e.toString() + ": in the FoodGroup class, setPrice function");
            } catch (NullPointerException e) {
                System.out.println(e.toString() + ": in FoodGroup class. SetPrice()");
            }
        }

        public int getPrice() {
            return price;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public void setCount() {
            try (BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in))) {
                System.out.println("What count?");
                count = Integer.parseInt(keyboard.readLine());

            } catch (NumberFormatException e) {
                System.out.println(e.toString() + ":doesnt seem to be a number");
            } catch (IOException e) {
                System.out.println(e.toString() + ": in the FoodGroup class, setCount()");
            } catch (NullPointerException e) {
                System.out.println(e.toString() + ": in FoodGroup class, setCount");
            }
        }

        public int getCount() {
            return count;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setName() {
            try {
                BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("What minute?");
                this.name = keyboard.readLine();

            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }

        public String getName() {
            return name;
        }

        public void setLocation(Location location) {
            this.location = location;
        }

        public Location getLocation() {
            return location;
        }

        public void setPurchaseDate(Date purchaseDate) {
            this.purchaseDate = purchaseDate;
        }

        public void setPurchaseDate() {
            this.purchaseDate.set();
        }

        public Date getPurchaseDate() {
            return purchaseDate;
        }

        public void setExpirationDate(Date expirationDate) {
            this.expirationDate = expirationDate;
        }

        public void setExpirationDate() {
            this.expirationDate.set();
        }

        public Date getExpirationDate() {
            return expirationDate;
        }
    }

and finally the main class, so I can get access to all of this work.

    package GroceryReceiptProgram;


    public class NewMain {


        public static void main(String[] args) {
            FoodGroup test = new FoodGroup();
        }
    }

If anyone is further interested, here is a link the UML for this.

https://www.dropbox.com/s/1weigjnxih70tbv/GRP.dia

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:05:58+00:00Added an answer on June 12, 2026 at 4:05 am

    There is a problem in your FoodGroup constructor, you haven’t initialized some of the instance variables, so their taking the null default valu, giving you problems:

    public FoodGroup() {
        //...
        //null pointer exception here!
        expirationDate.set();
        //null pointer exception here!
        purchaseDate.set();
        //null pointer exception here!
        location.set();
        //...
    }
    

    This problem can be solved by creating those variables before using them:

    public FoodGroup() {
        //...
        expirationDate = new Date();
        //not null pointer exception!
        expirationDate.set();
        //similar to the other cases
        //...
    }
    

    Some advices for this and future projects:

    • Don’t create classes that has a similar name that classes in the Java API. For example, your Date class can easily be misunderstood with java.util.Date.
    • Don’t use the Vector class, instead use List<YourClass> list = new ArrayList<YourClass>(). More info here: What are the differences between ArrayList and Vector? and Why is Java Vector class considered obsolete or deprecated?(this one is from Jon Skeet).
    • Try to use public constructors for your classes. In your case, your program will compile and run just because all your classes are in the same package, but if they were in different packages, there will be a problem because almost all the constructors has the default visibility and can only be seen by classes of the same package. Take note about it: Controlling Access to Members of a Class
    • FoodGroup is not a parent class, it is a “has-a” relationship between 1) FoodGroup and Date, 2) FoodGroup and Location. It could be seen as a controller who controls the behavior and live of both classes instances.
    • When you post code here or other sites, please post a SSCCE instead of your whole code. In that way, it will be easier for anyone to help you.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Brief Background I'm investigating the potential of investing in Agile Toolkit for a future
BRIEF: hg clone creates path default in /.hg/hgrc, set to where you cloned from.
Brief Context: Hi, I am a university student (behind proxy 10.3.100.211:8080), new to ROR,
Brief question What command can I use to make my DataSet refresh it's connection
Brief Idea about the flow : I have say minimum 1 and maximum 18
Brief intro about my requirement. I have an empty JSF dataTable. Now, when I
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I found an interesting bug and wanted to know you think. Brief background: I've
Brief Background: My team has decided to use Microsoft's Managed Extensibility Framework ( MEF
Very Brief Background: I am using Jquery Autocomplete to lookup the the value of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.