0

tl;dr I just started learning java and swing and I have this problem where inside the main class if I try to call any custom method defined in my custom class which extends JPanel, i get an error which says "cannot find symbol", it's as if that method isn't even declared. I tripled checked the custom class object looks properly initiated and the method is set as public. Any idea why this happens?

At first I was using Intellij Idea but it's gui designer was so frustrating to use that i thought it was the IDE's fault but even after switching to NetBeans and rewriting code from scratch I get the same error.

I wanted to write a class called Drawing which extends JPanel that could draw my custom Circuit class objects. Basically what i wanted to do for the app is that selecting New from the File menu would create and draw that Circuit object. For that I wanted that pressing the New button would call setCircuit() method defined in the Drawing class which initializes the circuit and then the paintComponent() would draw the circuit.

Code below:

import circuit.*;
import java.awt.Graphics;
public class Drawing extends javax.swing.JPanel {
private Circuit circuit = null;

public Drawing(){

}

public void setCircuit(Circuit circuit) {
    this.circuit = circuit;
}

@Override
public void paintComponent(Graphics graphics){
    super.paintComponent(graphics);
    if(circuit == null){
        graphics.drawString("Create a new circuit.", 10, 20);
    } else{
        // draws the circuit
    }

}

}


import circuit.*;
public class GUI extends javax.swing.JFrame {
private Circuit circuit = null;

public GUI() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    drawing = new Drawing();
    menuBar = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    newMenuItem = new javax.swing.JMenuItem();
    exitMentuItem = new javax.swing.JMenuItem();
    exitMenu = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("My app");
    setName("mainFrame"); // NOI18N

    javax.swing.GroupLayout drawingLayout = new javax.swing.GroupLayout(drawing);
    drawing.setLayout(drawingLayout);
    drawingLayout.setHorizontalGroup(
        drawingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    drawingLayout.setVerticalGroup(
        drawingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );

    fileMenu.setText("File");

    newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
    newMenuItem.setText("New");
    newMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            newMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(newMenuItem);

    exitMentuItem.setText("Exit");
    exitMentuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitMentuItemActionPerformed(evt);
        }
    });
    fileMenu.add(exitMentuItem);

    menuBar.add(fileMenu);

    exitMenu.setText("Edit");
    menuBar.add(exitMenu);

    setJMenuBar(menuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(drawing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(drawing, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
    circuit = new Circuit();
    drawing.setCircuit(circuit); // this is where I get the error:
/*
cannot find symbol
symbol: setCircuit(Circuit)
location: variable drawing of type JPanel
*/
}                                           

private void exitMentuItemActionPerformed(java.awt.event.ActionEvent evt) {                                              
    System.exit(0);
}                                             

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JPanel drawing;
private javax.swing.JMenuItem exitMentuItem;
private javax.swing.JMenu exitMenu;
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem newMenuItem;
// End of variables declaration                   

}

Yokubasu
  • 25
  • 3
  • 8
  • 1
    Your drawing field is declared with the type JPanel. So the compiler can't know that it's of type Drawing. Change the type to Drawing: `private Drawing drawing;` – JB Nizet Nov 04 '17 at 16:04
  • `Drawing` extends `JPanel`, in the `initComponents()` method my drawing field is initialized as `Drawing()`. So is that really the problem? – Yokubasu Nov 04 '17 at 16:09
  • 1
    Yes, it's really the problem. The variable is of type JPanel. So, at any point, you could do `drawing = new SomeOtherClassExtendingJPanel()`. So the only thing the compiler can assume is that drawing references a JPanel. So it won't let you call any method that doesn't exist in JPanel. That's the whole point of static typing. – JB Nizet Nov 04 '17 at 16:16
  • For [example](https://stackoverflow.com/a/37063037/230513). – trashgod Nov 04 '17 at 16:42
  • @JB Nizet Yup you were right. The fix was so simple i want to cry. – Yokubasu Nov 04 '17 at 17:08

0 Answers0