user2565811
user2565811

Reputation: 13

cannot find method but finds another method

I'm writing coding for a class project. I have many separate classes that all work together to display a series of GUI's which have a menu of services that can be bought. A second GUI gets the customer's information (name, credit card, etc.) and a third GUI displays a receipt. Each GUI has its own class. In order to display the receipt, my third GUI (GUIBill) must call methods from my second GUI (GUICheckout). So I have several get methods at the bottom of my GUICheckout. For some reason the getName method works perfectly. However, the rest of the methods (getStreet, getCity, so on) are not working in my GUIBill. I keep getting an error that these methods cannot be found and I'm racking my brain trying to figure out why. For now, I will only post the GUICheckout and GUIBill classes, but if for some reason the other classes are needed, let me know.

The first 75% of GUICheckout is probably irrelevant, but I posted the whole code in case the issue has something to do with when I create my JLabels.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUICheckout extends JFrame{

private static GUIShopping theGUI = new GUIShopping();
private CompleteSale sale = new CompleteSale();

/*Widgets for second window, where customer enters their information*/  
private JLabel customerName = new JLabel("Name", JLabel.CENTER);
private JLabel customerStreet = new JLabel("Street Address", JLabel.CENTER);
private JLabel customerCity = new JLabel("City", JLabel.CENTER);
private JLabel customerState = new JLabel("State", JLabel.CENTER);
private JLabel customerZip = new JLabel("Zip", JLabel.CENTER);
private JLabel creditCard = new JLabel("Credit Card Type", JLabel.CENTER);
private JLabel creditCardNumber = new JLabel("Credit Card Number", JLabel.CENTER);
private JLabel creditExpiration = new JLabel("Expiration Date", JLabel.CENTER);
private JTextField customerNameField = new JTextField("");
private JTextField customerStreetField = new JTextField("");
private JTextField customerCityField = new JTextField("");
private JTextField customerStateField = new JTextField("");
private JTextField customerZipField = new JTextField("");
private JTextField creditCardNumberField = new JTextField("");
private JButton proceed = new JButton("Pay and View Bill");
private JButton goBack = new JButton("Return to Menu");
private String[] creditCardTypes = { "Visa", "MasterCard", "American Express" };
private String[] monthList = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
private String[] yearList = { "13", "14", "15", "16", "17", "18", "19", "20" };
private JComboBox creditCardList = new JComboBox(creditCardTypes);
private JComboBox expirationMonth = new JComboBox(monthList);
private JComboBox expirationYear = new JComboBox(yearList);

public GUICheckout(){
    JPanel dataPanel = new JPanel(new GridLayout(5, 4, 5, 20));
        dataPanel.add(customerName);
        dataPanel.add(customerNameField);
        dataPanel.add(customerState);
        dataPanel.add(customerStateField);
        dataPanel.add(customerStreet);
        dataPanel.add(customerStreetField);
        dataPanel.add(customerZip);
        dataPanel.add(customerZipField);
        dataPanel.add(customerCity);
        dataPanel.add(customerCityField);
        dataPanel.add(creditCard);
        dataPanel.add(creditCardList);
        dataPanel.add(creditCardNumber);
        dataPanel.add(creditCardNumberField);
        dataPanel.add(new JLabel(""));
        dataPanel.add(new JLabel(""));
        dataPanel.add(creditExpiration);
        dataPanel.add(expirationMonth);
        dataPanel.add(expirationYear);
    JPanel buttonPanel = new JPanel(new GridLayout(1,3,12,6));
        buttonPanel.add(goBack);
        buttonPanel.add(proceed);
    JPanel topPanel = new JPanel();
    JPanel leftPanel = new JPanel();
    JPanel rightPanel = new JPanel();   
        Container container = getContentPane();
        container.add(dataPanel, BorderLayout.CENTER);
        container.add(topPanel, BorderLayout.NORTH);
        container.add(buttonPanel, BorderLayout.SOUTH);
        container.add(rightPanel, BorderLayout.EAST);
        container.add(leftPanel, BorderLayout.WEST);
        goBack.addActionListener(new GoBackListener());
        proceed.addActionListener(new ProceedListener());   
    }

private class GoBackListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    JFrame theFirstGUI = GUIShopping.getCheckoutMainFrame();
    theFirstGUI.dispose();
    theGUI.setTitle("Iowa Computer Service and Repair");
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theGUI.setSize(600,300);
    theGUI.setVisible(true);

    }
}

private class ProceedListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    sale.setName(customerNameField.getText());
    sale.setAddress(customerStreetField.getText(), customerCityField.getText(), customerStateField.getText(), customerZipField.getText());
    String cardType = (String)creditCardList.getSelectedItem();
    String expMonth = (String)expirationMonth.getSelectedItem();
    String expYear = (String)expirationYear.getSelectedItem();
    sale.setCardInfo(cardType, creditCardNumberField.getText(), expMonth, expYear);
    JFrame theFirstGUI = GUIShopping.getCheckoutMainFrame();
    theFirstGUI.dispose();
    GUIBill theBillGUI = new GUIBill();
    theBillGUI.setTitle("Iowa Computer Service and Repair");
    theBillGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    theBillGUI.setSize(600,300);
    theBillGUI.setVisible(true);

    }
}

public static JFrame getShoppingMainFrame(){
    return theGUI;
}


public String getName(){                                    //Here are all of the get Methods. the getName works fine but the rest do not
    String name = customerNameField.getText();
    return name;
}

public String getStreet(){
    String street = customerStreetField.getText();
    return street;          
}

public String getCity(){
    String city = customerCityField.getText();
    return city;
}

public String getCustState(){
    String state = customerStateField.getText();
    return state;
}

public String getZip(){
    String zip = (String)customerZipField.getText();
    return zip;
}

public String getCardType(){
    String type = (String)creditCardList.getSelectedItem();
    return type;
}

public String getNumber(){
    String number = (String)creditCardNumberField.getText();
    return number;
}

public String getExpMonth(){
    String expMonth = (String)expirationMonth.getSelectedItem();
    return expMonth;
}

public String getExpYear(){
    String expYear = (String)expirationYear.getSelectedItem();
    return expYear;
}
}

And here is the GUIBill class which is supposed to call methods from GUICheckout.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUIBill extends JFrame{

//private static GUICheckout theGUI = new GUICheckout();
private static JFrame theGUI = GUIShopping.getCheckoutMainFrame();

private JLabel thankYou = new JLabel("Thank you for choosing Iowa Computer Service and Repair!", JLabel.CENTER);
private JLabel name = new JLabel(theGUI.getName()); //this is the getName method that works fine
//private JLabel street = new JLabel(theGUI.getStreet());               /*all JLabels which are commented out are the ones that have methods
//private JLabel city = new JLabel(theGUI.getCity());                       which mysteriously cannot be found.*/
//private JLabel state = new JLabel(theGUI.getCustState());             
//private JLabel zip = new JLabel(theGUI.getZip());                  
private JLabel itemsBoughtLabel = new JLabel("Services Purchased", JLabel.CENTER);
private JLabel itemPricesLabel = new JLabel("Price", JLabel.CENTER);
private JLabel tax = new JLabel();
private JLabel totalPrice = new JLabel();

public GUIBill(){
    JPanel thankYouPanel = new JPanel();
        thankYouPanel.add(thankYou);
    JPanel namePanel = new JPanel();
        namePanel.add(name);
    /*JPanel addressPanel = new JPanel(new GridLayout(4,1,6,12));
        addressPanel.add(streetLabel);
        addressPanel.add(cityLabel);
        addressPanel.add(stateLabel);
        addressPanel.add(zipLabel);*/
    JPanel dataPanel = new JPanel(new GridLayout(4,1,6,12));
        dataPanel.add(itemsBoughtLabel);
        dataPanel.add(itemPricesLabel);
    /*JPanel cardPanel = new JPanel(new GridLayout(4,1,6,12));
        cardPanel.add(typeLabel);
        cardPanel.add(numberLabel);
        cardPanel.add(expMonthLabel);
        cardPanel.add(expYearLabel);*/
        Container container = getContentPane();
        container.add(thankYouPanel, BorderLayout.NORTH);
        container.add(namePanel, BorderLayout.WEST);
        //container.add(addressPanel, BorderLayout.EAST);
        container.add(dataPanel, BorderLayout.CENTER);
        //container.add(cardPanel, BorderLayout.SOUTH);
}

}

and here is the error that I get if I try to compile GUIBill with one of the faulty methods not commented out:

GUIBill.java:12: error: cannot find symbol
private JLabel street = new JLabel(theGUI.getStreet());                                                          ^
symbol:   method getStreet()
location: variable theGUI of type JFrame
1 error

Upvotes: 0

Views: 198

Answers (1)

snrlx
snrlx

Reputation: 5107

You declared the GUI object as follows:

private static JFrame theGUI = GUIShopping.getCheckoutMainFrame();

So it is of type JFrame, which does not know of the methods that you declared in the GUICheckout-class. Instead just declare the GUI like below and you should be able to call the methods:

private static GUICheckout theGUI = GUIShopping.getCheckoutMainFrame();

I hope this helps you!

Upvotes: 2

Related Questions