user569127
user569127

Reputation: 65

Java Class subclass variable reference

I keep running into the 'cannot find symbol' error on my class. The variable is clearly declared in the super class, yet the subclass can not see it. I receive no errors except on the new constructor of JLabel in the subclass RecordViewer.

 class RecordViewer extends JDialog{
     private JButton next;
     private JButton prev;
     private JLabel label;
     private int current;

     public RecordViewer(CDinventoryItem [] array){
         super();
         current = 0;
         final CDinventoryItem [] items = array;

         label = new JLabel(items[getCurrent()]);

Predefined toString from my CDinventoryItem class...

        @Override public String toString(){

        //  Decimal foramting for the inventory values
        NumberFormat dformat = new DecimalFormat("#0.00");

        //  Formatting for the inventory output
        StringBuilder ouput  = new StringBuilder();
        String New_Line = System.getProperty("line.separator");

            ouput.append("The product number of my CD is: ").append(iPitemNumber).append (New_Line);
            ouput.append("The title of the CD is: ").append(sPtitle).append (New_Line);
            ouput.append("I have ").append(iPnumberofUnits).append(" units in stock.").append (New_Line);
            ouput.append("The total value of my inventory on this product is: ").append(dformat.format(stockValue())).append (New_Line);
            return ouput.toString();
        }

Upvotes: 2

Views: 526

Answers (3)

Bozho
Bozho

Reputation: 597076

  • organize your imports, so that JLabel is imported properly
  • JLabel does not define a constructor taking your custom type. You need to pass a string there.

Upvotes: 2

Jems
Jems

Reputation: 11620

Is that the standard Java JLabel? You are trying to pass an object of type CDinventoryItem, and the JLabel won't have a constructor to handle that kind of argument, unless it is extending the String or Icon classes.

Upvotes: 3

Michael Berry
Michael Berry

Reputation: 72284

Just a guess, but your question wording implies you're trying to use a private field directly from a subclass? private fields can't be seen anywhere other than the class that they're declared in, including classes in the same inheritance hierarchy. You could make it protected, but this is frowned upon - better to either provide a mutator method to label or better still, initialise it in the superclass constructor.

Upvotes: 0

Related Questions