"Cannot instantiate the Type Main"

java, swing, user-interface

Solution

You can't instantiate abstract classes in java, Your `Main` class is abstract, make it a concrete Class, since it seems you have all the functionalists and no need to declare it as abstract class.

public class Main extends JPanel implements ActionListener, KeyListener { } //abstract keyword removed

Consider using abstract classes if any of these statements apply to your situation:

- You want to share code among several closely related classes.

- You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).

- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

Problem

I keep getting this error every time I create a new object with the class name: "Cannot instantiate the Type Main" This is the code: ``` import javax.swing.*; import java.util.Scanner; import java.applet.Applet; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.Ellipse2D; public abstract class Main extends JPanel implements ActionListener, KeyListener{ Timer t = new Timer(5, this); double x = 0, y = 0, velX = 0, velY = 0; public Main() { t.start(); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.fill(new Ellipse2D.Double(x, y, 40, 40)); } public void actionPerformed(ActionEvent e) { repaint(); x += velX; y += velY; } public void up() { velY = -1.5; velX = 0; } public void down() { velY = -1.5; velX = 0; } public void left() { velX = -1.5; velY = 0; } public void right() { velX = -1.5; velY = 0; } public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (code == KeyEvent.VK_UP) { up(); } if (code == KeyEvent.VK_DOWN) { down(); } if (code == KeyEvent.VK_LEFT) { left(); } if (code == KeyEvent.VK_RIGHT) { right(); } Main m = new Main(); JFrame f = new JFrame(); f.add(m); f.setVisible(true);; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(800,600); } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} } ``` And this is the object that keeps getting this "error": ``` Main m = new Main(); ``` (I am very very new to making UI's, infact, this is my 3rd, so I have no idea why I cannot instantiate the Type)

Original source