0

I have a problem with this code

public static void main(String[] args) {
    final GLProfile profile = GLProfile.get(GLProfile.GL2);
    GLCapabilities capabilities = new GLCapabilities(profile);

    final GLCanvas glcanvas = new GLCanvas(capabilities);
    MainRender r = new MainRender();
    glcanvas.addGLEventListener(r);
    glcanvas.setSize(700, 400);

    final FPSAnimator animator = new FPSAnimator(glcanvas, 300, true);

    final JFrame frame = new JFrame("Render");
    frame.getContentPane().add(glcanvas);

    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            if (animator.isStarted())
                animator.stop();
            System.exit(0);
        }
    });

    frame.setSize(frame.getContentPane().getPreferredSize());

    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    JPanel p = new JPanel();
    p.setPreferredSize(new Dimension(0, 0));
    frame.add(p, BorderLayout.SOUTH);

    keyBindings(p, frame, r);
    animator.start();

    Handler h = new Handler();

    p.addMouseListener(new Handler());
    p.addMouseMotionListener(new Handler());
}

At the Handler h = new Handler(); Eclipse shows this message

No enclosing instance of type MainRender is accessible. Must qualify the allocation with an enclosing instance of type MainRender (e.g. x.new A() where x is an instance of MainRender).

Any solutions?

I. Kuzmin
  • 1
  • 1
  • 5

1 Answers1

0

The issue is that the Handler is a non-static nested class of MainReader. This means that you need an instance of MainReader to be able to instantiate the Handler. Have a look at this stackoverflow answer for more information about non-static vs static nested classes.

To solve the above issue, either you can make the Handler class static (if you can) or replace

Handler h = new Handler();

with

Handler h = r.new Handler();
Community
  • 1
  • 1
Gergely Toth
  • 6,638
  • 2
  • 38
  • 40