1

i'm testing another simple program for drawing a line. First problem is the error.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUI$2.paint(GUI.java:57)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)

This is the code for line 57 g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);

panel = new JPanel(){
        Point pointStart = null;
        Point pointEnd = null;
        int x = 1;
        {   
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    pointStart = e.getPoint();
                }
                public void mouseReleased(MouseEvent e){
                    pointEnd = e.getPoint();
                }
            });
            addMouseMotionListener(new MouseAdapter(){
                public void mouseMoved(MouseEvent e){
                    pointEnd = e.getPoint();
                }
                public void mouseDragged(MouseEvent e){
                    pointEnd = e.getPoint();
                    repaint();
                }
            });
        }
        public void paint(Graphics g){
            super.paint(g);
            g.setColor(a);
            g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
        }
    };

If i can ask, how do i save the lines that has been drawn? Thanks

  • You're using the field `pointStart` in your draw method with `pointStart.x` etc. but you're not assigning any value to them until the mouse is clicked, so before you click the mouse you're gonna get a lot of NullPointerExceptions. – Erwin Bolwidt Jul 05 '15 at 05:11
  • "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Jul 05 '15 at 10:26

2 Answers2

1

Try this (note that this is the paintComponent() method, not paint()):

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(a);
    if(pointStart!=null && pointEnd!=null){
        g.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y);
    }
}
Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51
1

Is your real code exactly like this? Because when your program reaches to the line 57 there wasn't any initialization for the pointStart and pointEnd members. So there is a very obvious reason for NullPointerException to happen since paint() or paintComponent() or paintComponents() methods are called so many time during the initial rendering, iconify/deiconify or resizing or ... of a panel. So this calls are very supposed to happen before user create any mouseClick or mouseDrag events to initialize those points.

May be it is better for you to have class which extends JPanel and has a constructor to get pointStart and pointEnd as arguments of the constructor to avoid this NullPointerException.

If you cannot determine in the time of creation of this panel you must either check the nullity of pointStart and pointEnd or initialize both of them with (0,0).

After your panel is added to the parent container, if the parent's paintComponents method is called (can happen for many number of reasons and so many times), your paint or paintComponents methods is called relatively.

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43