title: Graphics2D object always gives NullPointerException
body: <p>I want to create simple drawing program;</p>
  
  <p>Here I my program's mousePressed and mouseDragged events:</p>
  
  <pre><code>private void mousePressed(java.awt.event.MouseEvent evt) {
      touch = evt.getPoint();
      pressed = true;
  }
  
  
  private void mouseDragged(java.awt.event.MouseEvent evt) {
      Point p = evt.getPoint();
      if(pressed){
          graphics2D.drawLine(touch.x, touch.y, p.x, p.y);
      }
      repaint();
  }
  </code></pre>
  
  <p>But when I try to draw someting, it always give <code>"Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException"</code>  in this line <code>graphics2D.drawLine(touch.x, touch.y, p.x, p.y);</code></p>
  
  <p>I also overrided the method paintComponent</p>
  
  <pre><code>public void paintComponent(Graphics g){
      if(image == null){
          image = createImage(getSize().width, getSize().height);
          graphics2D = (Graphics2D)image.getGraphics();
          graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          clear();
      }
      g.drawImage(image, 0, 0, null);
  }
  </code></pre>
  
  <p>And I the clear method is:</p>
  
  <pre><code>public void clear(){
      graphics2D.setPaint(Color.white);
      graphics2D.fillRect(0, 0, getSize().width, getSize().height);
      graphics2D.setPaint(Color.black);
      repaint();
  }
  </code></pre>
  
  <p>What should I do ?</p>
  
  <p>Thanks</p>