How to I access the fields defined in my constructor?

Ski Mask The Slump God :

I have a picker class that picks your outfit for you, depending on why you're going out and what kind of day it is.

I'm trying to finish the chooser method that decides on what pieces of clothing to assign at each ArrayList index. Clothing is its own class which has String brand, String color, and boolean formal fields.

In the chooser method, I'm trying to do

outfit.add(jeans);

but it gives me a nullpointer exception, which it shouldn't because I define jeans in the constructor, so I tried to do

outfit.add(picker.jeans);

and it gives the error "non static field 'jeans' cannot be referenced from a static context"

The method used static boolean variables, so I tried fixing it by turning

public class picker {
static boolean formalEvent;
static boolean niceWeather;
static boolean specialDay;
static boolean workingOut;

into

public class picker {
boolean formalEvent;
boolean niceWeather;
boolean specialDay;
boolean workingOut;

but it still doesn't work, and I maybe shouldn't have had those static in the first place, but it made my main method work, where I define them based on the users input into the scanner that I also made static. My teacher said use static where you don't need multiple of it. I only need one scanner.

The beginning of my code up to the constructor looks like this, I might later make the constructor ask what kind of pants, sweatpants, shirt, etc the user has so it isn't always one defined wardrobe.

import java.util.ArrayList;
import java.util.Scanner;

public class picker {
    boolean formalEvent;
    boolean niceWeather;
    boolean specialDay;
    boolean workingOut;
    ArrayList<clothing> outfit = new ArrayList<>();
    static Scanner scnr = new Scanner(System.in);
    clothing khakis;
    clothing jeans;
    clothing shorts;
    clothing sweatpants;
    clothing sweatshirt;
    clothing gymShirt;
    clothing workShirt;
    clothing designerShirt;

    /**
     * Picker constructor.
     */
    public picker() {
        clothing khakis = new clothing("Levi", "brown", true);
        clothing jeans = new clothing("Mike Amiri", "blue", false);
        clothing shorts = new clothing("Nike", "blue", false);
        clothing sweatpants = new clothing("Adidas", "black", false);
        clothing sweatshirt = new clothing("Old navy", "red", false);
        clothing gymShirt = new clothing("Nike", "grey", false);
        clothing graphicTee = new clothing("Mike's Paving Posse", "black", false);
        clothing workShirt = new clothing("Polo", "yellow", true);
        clothing designerShirt = new clothing("Supreme", "red", false);
    }

The start of my main method looks like this

public static void main(String[] args) {
        picker todaysFit = new picker();
        String response = "";
        System.out.println("Are you going to a formal event?");
        response = scnr.next();
        if (response.equalsIgnoreCase("yes")) {
            formalEvent = true;

and now

formalEvent = true;

gets an error at formalEvent saying "Non-static field 'formalEvent' cannot be referenced from a static context." I tried making it

picker.formalEvent = true;

to fix it, but it still gives the same error.

Mureinik :

You have declared and initialized local variables in the constructor that hide the data members. Drop the type declarations to actually initialize the members:

public picker() {
    khakis = new clothing("Levi", "brown", true);
    jeans = new clothing("Mike Amiri", "blue", false);
    shorts = new clothing("Nike", "blue", false);
    sweatpants = new clothing("Adidas", "black", false);
    sweatshirt = new clothing("Old navy", "red", false);
    gymShirt = new clothing("Nike", "grey", false);
    graphicTee = new clothing("Mike's Paving Posse", "black", false);
    workShirt = new clothing("Polo", "yellow", true);
    designerShirt = new clothing("Supreme", "red", false);
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=393634&siteId=1