I know this is really messy code but I am writing a Paint app in Java for a class and I'm getting an ERROR when I drag the mouse. MousePressed works properly (prints). MouseDragged, however, throws a NullPointerException. Please let me know what the problem is here. I'll put a comment "#ERROR:FIXME" above the problem method (way down at the bottom).

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Enumeration;

import javax.management.remote.SubjectDelegationPermission;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.sun.javafx.geom.Rectangle;
import com.sun.prism.shader.DrawEllipse_Color_AlphaTest_Loader;

public class NetpaintGUI extends JFrame {

    // Variables for selecting Color and Shape
    private JPanel selectionArea = new JPanel();
    private ButtonGroup shapeSelectorGroup = new ButtonGroup();
    private JRadioButton ellipseBtn = new JRadioButton("Ellipse");
    private JRadioButton rectangleBtn = new JRadioButton("Rectangle");
    private JRadioButton lineBtn = new JRadioButton("Line");
    private JColorChooser colorChooser = new JColorChooser();
    private Graphics2D draw;
    private Color currColor;

    private int currX, currY, prevX, prevY;

    public static void main(String[] args) {
        JFrame paintFrame = new NetpaintGUI();
        paintFrame.setVisible(true);
    }

    public NetpaintGUI() {
        setUpModel();

        this.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
            }
        });
    }

    public void drawShape(Graphics g) {

        if (ellipseBtn.isSelected()) {
            System.out.println("Ellipse, Bro");
        }

        else if (rectangleBtn.isSelected()) {
            System.out.println("Rectangle, Bro");
        }

        else if (lineBtn.isSelected()) {
            System.out.println("Line, Bro");
        }

    }

    private void setUpModel() {

        // TODO: Read info (persistence)

        // Set JFrame size, color, font, title
        this.setTitle("UA Netpaint");
        this.setSize(1200, 700);
        this.getContentPane().setBackground(Color.WHITE);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setFont(new Font("Courier", Font.BOLD, 16));

        // Add ColorSelector to Panel
        selectionArea.add(colorChooser);

        // Add RadioButtons to ButtonGroup and to Panel, and add Panel to Frame
        shapeSelectorGroup.add(ellipseBtn);
        shapeSelectorGroup.add(rectangleBtn);
        shapeSelectorGroup.add(lineBtn);
        selectionArea.add(ellipseBtn);
        selectionArea.add(rectangleBtn);
        selectionArea.add(lineBtn);
        ellipseBtn.setSelected(true);
        this.add(selectionArea, BorderLayout.SOUTH);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                prevX = e.getX();
                prevY = e.getY();
                System.out.println("X: " + prevX + "   Y: " + prevY);
            }
        });

        #ERROR:FIXME
        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                currX = e.getX();
                currY = e.getY();
                draw.setColor(colorChooser.getColor());
                if (ellipseBtn.isSelected()) {
                    draw.drawOval(prevX, prevY, currX-prevX, currY-prevY);
                }
                else if (lineBtn.isSelected()) {
                    draw.drawLine(prevX, prevY, currX, currY);
                }
                else if (rectangleBtn.isSelected()) {
                    draw.drawRect(prevX, prevY, currX-prevX, currY-prevY);
                }

                repaint();
                prevX = currX;
                prevY = currY;
            }
        });
    }
}

Looks like draw is never initialised.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.