2

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();
        }
1
  • 1
    Please provide the whole error message. Commented Jan 24, 2011 at 20:39

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Yes just the standard JLabel. CDinventory class is an array of String, int, int, double
So it doesn't extend the String or Icon class? Then the constructor isn't going to work. extend doesn't mean it contains a String variable/array of Strings as a member field, it means you used the keyword extend in your class declaration; CDinventory would then be a subclass of String. That probably isn't what you are trying to do though- maybe you want to do something like items[getCurrent()].getName() as your argument, where getName would be some method returning a String.
OK a label = this.setLabel(items[Current()]); removed the error. I let NetBeans create a method for the setLabel.
Thank you. But I still am running into another error. You are referring to the other problem I am having as well. getLabel().setText(items[getCurrent()]); returns a 'cannot be applied to given types'. I just want it to display the array in the JLabel, but tells me I cannot because it is of String type.
Again, this is because a CDinventoryItem isn't a String. That method expects a String as an argument. You need to make a method for the CDinventoryItem class that returns a String containing whatever text you need for the JLabel, and then have that method called as the argument for setText
|
2
  • 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.

2 Comments

Yes i have the import javax.swing.*; at the top. I just added the import javax.swing.JLabel; as you suggested. Still no change.
@user569127 are you using an IDE?
0

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.

2 Comments

Yes even if I initialize it in the superclass it doesn't see it. Was one of the first things I tried.
OK a label = this.setLabel(items[Current()]); removed the error. I let NetBeans create a method for the setLabel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.