This question already has an answer here:
- What is a NullPointerException, and how do I fix it? 12 answers
The question is simple. I have created a class named "handler" and within its constructor it contains a parameter for "c", a JComponent. When this constructor is called on a certain JComponent, preferably a JPanel, an oval is drawn at the mouse's current coordinates. This is the source code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Handler implements MouseListener, MouseMotionListener {
Graphics g;
public Handler() {}
public Handler(JComponent c) {
if (c instanceof JPanel) {
g = c.getGraphics();
g.drawOval(mx, my, 5, 5);
}
if (c != null) {
c.addMouseListener(this);
c.addMouseMotionListener(this);
}
}
int mx, my;
public void mouseClicked(MouseEvent e) {
mx = e.getX();
my = e.getY();
}
public void mousePressed(MouseEvent e) {
mx = e.getX();
my = e.getY();
}
public void mouseReleased(MouseEvent e) {
mx = e.getX();
my = e.getY();
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
mx = e.getX();
my = e.getY();
}
public void mouseDragged(MouseEvent e) {}
}
However, this error is thrown:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Do you guys have any idea about this? If so, please post a solution.
Edit 1
I've done something new. This is my entire new code:
import javax.swing.*;
import java.awt.*;
public class Handler extends JPanel {
int mx = MouseInfo.getPointerInfo().getLocation().x;
int my = MouseInfo.getPointerInfo().getLocation().y;
public Handler(BorderLayout bl) {
this.setLayout(bl);
}
public void paintComponent(Graphics g23) {
Graphics2D g2 = (Graphics2D) g23;
g2.drawOval(mx, my, 30, 30);
}
}
I've reworked the code greatly. Now, it extends JPanel and serves as a replacement to JPanel. So instead of instantiating a new JPanel, I'd call Handler's constructor. It also implements paintComponent, yet the oval still isn't being drawn. It yields no errors, however.