3 Replies Latest reply: May 30, 2011 5:25 PM by s3a RSS

    I have trouble running this paint program successfully!

    s3a
      The frame with a white panel area shows and then I get this error:
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
              at MyGoodPaint.annotateImage(MyGoodPaint.java:38)
              at MyGoodPaint.paintComponent(MyGoodPaint.java:92)
              at javax.swing.JComponent.paint(JComponent.java:1029)
              at javax.swing.JComponent.paintChildren(JComponent.java:862)
              at javax.swing.JComponent.paint(JComponent.java:1038)
              at javax.swing.JComponent.paintChildren(JComponent.java:862)
              at javax.swing.JComponent.paint(JComponent.java:1038)
              at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
              at javax.swing.JComponent.paintChildren(JComponent.java:862)
              at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
              at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
              at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
              at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
              at javax.swing.JComponent.paint(JComponent.java:1015)
              at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
              at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
              at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
              at java.awt.Container.paint(Container.java:1780)
              at java.awt.Window.paint(Window.java:3375)
              at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
              at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
              at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
              at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
              at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
              at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
              at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
              at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
      Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
              at MyGoodPaint.annotateImage(MyGoodPaint.java:38)
              at MyGoodPaint.paintComponent(MyGoodPaint.java:92)
              at javax.swing.JComponent.paint(JComponent.java:1029)
              at javax.swing.JComponent.paintChildren(JComponent.java:862)
              at javax.swing.JComponent.paint(JComponent.java:1038)
              at javax.swing.JComponent.paintChildren(JComponent.java:862)
              at javax.swing.JComponent.paint(JComponent.java:1038)
              at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
              at javax.swing.JComponent.paintChildren(JComponent.java:862)
              at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
              at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1479)
              at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1410)
              at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
              at javax.swing.JComponent.paint(JComponent.java:1015)
              at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
              at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
              at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
              at java.awt.Container.paint(Container.java:1780)
              at java.awt.Window.paint(Window.java:3375)
              at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
              at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
              at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
              at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
              at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
              at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
              at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
              at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
              at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
              at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
      Here is the code:
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseListener;
      import java.awt.event.MouseMotionListener;
      import java.awt.image.BufferedImage;
      import java.io.File;
      import javax.imageio.ImageIO;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      
      
      public class MyGoodPaint extends JPanel
      {
          int width = 400;
          int height = 400;
          BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
          Graphics2D g2;
      
          int previousX;
          int previousY;
          int currentX;
          int currentY;
      
          public MyGoodPaint()
          {
              JFrame frame = new JFrame();
              frame.add(this);
              frame.setSize(width,height);
              frame.setLocationRelativeTo(null);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
          }
      
          public void annotateImage()
          {
              g2.drawLine(previousX, previousY, currentX, currentY);
          }
      
          private class MyMouseMotionListener implements MouseListener, MouseMotionListener
          {
              public void mouseClicked(MouseEvent e)
              {
      
              }
      
              public void mousePressed(MouseEvent e)
              {
                  
              }
      
              public void mouseReleased(MouseEvent event)
              {
                  try
                  {
                      ImageIO.write(image, "png", new File("C:\\Users\\Deniz\\Desktop\\newberry.png"));
                  }
                  catch(Exception exception)
                  {
                      System.out.println("The exception caught is: " + exception);
                  }
              }
      
              public void mouseEntered(MouseEvent e)
              {
                  
              }
      
              public void mouseExited(MouseEvent e)
              {
                  
              }
      
              public void mouseDragged(MouseEvent e)
              {
                  currentX = e.getX();
                  currentY = e.getY();
                  repaint();
              }
      
              public void mouseMoved(MouseEvent e)
              {
                  
              }        
          }
      
          protected void paintComponent(Graphics g)
          {
              super.paintComponent(g);
              g.drawLine(previousX, previousY, currentX, currentY);
              annotateImage();
              previousX = currentX;
              previousY = currentY;
          }
      
          public static void main(String[] args)
          {
              new MyGoodPaint();
          }
      }
      I am more interested in just getting it to work but please do also tell me of any bad practices I am doing and what the better way is at least briefly.

      Any help in solving this problem would be greatly appreciated!
      Thanks in advance!
        • 1. Re: I have trouble running this paint program successfully!
          DarrylBurke
          The stack trace already tells you
          s3a wrote:
          Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
          at MyGoodPaint.annotateImage(MyGoodPaint.java:38)
          What's on line 38? Where did you ever initialize that variable?

          And that variable shouldn't be a field in the first place, since you have no reliable way to assign it a value. It should instead be passed as a parameter from <tt>paintComponent</tt>.

          Other comments: extend <tt>MouseAdapter</tt> instead of implementing <tt>MouseListener</tt> and <tt>MouseMotionListener</tt> and you won't have to write a bunch of empty methods.

          db
          • 2. Re: I have trouble running this paint program successfully!
            s3a
            I see what the issue was! Graphics2D g2; should of been Graphics2D g2 = image.createGraphics(); but now that I did that, releasing the mouse does not save any image nor does anything get drawn on the screen (not even one pixel).

            Also, I am relatively new to this so sorry if my approach is dumb (which it likely is) but my g object is from paintComponent whereas my g2 object is from image.createGraphics(). I want to use g for painting to the screen and g2 for painting to the BufferedImage that I want to save.
            • 3. Re: I have trouble running this paint program successfully!
              s3a
              Ok, I got it under control now.

              Here is the latest code (I know I have things to fix but the stuff I needed help with have been dealt with):
              import java.awt.Graphics;
              import java.awt.Graphics2D;
              import java.awt.event.MouseEvent;
              import java.awt.event.MouseListener;
              import java.awt.event.MouseMotionListener;
              import java.awt.image.BufferedImage;
              import java.io.File;
              import javax.imageio.ImageIO;
              import javax.swing.JFrame;
              import javax.swing.JPanel;
              
              
              public class MyGoodPaint extends JPanel
              {
                  int width = 400;
                  int height = 400;
                  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                  Graphics2D g2 = image.createGraphics();
              
                  int previousX;
                  int previousY;
                  int currentX;
                  int currentY;
              
                  public MyGoodPaint()
                  {
                      JFrame frame = new JFrame();
                      frame.add(this);
                      frame.setSize(width,height);
                      frame.setLocationRelativeTo(null);
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setVisible(true);
              
                      MyMouseListener myMouseListener = new MyMouseListener();
                      this.addMouseListener(myMouseListener);
                      this.addMouseMotionListener(myMouseListener);
                  }
              
                  public void annotateImage()
                  {
                      g2.drawLine(previousX, previousY, currentX, currentY);
                  }
              
                  private class MyMouseListener implements MouseListener, MouseMotionListener
                  {
                      public void mouseClicked(MouseEvent e)
                      {
              
                      }
              
                      public void mousePressed(MouseEvent e)
                      {
              
                      }
              
                      public void mouseReleased(MouseEvent event)
                      {
                          try
                          {
                              ImageIO.write(image, "png", new File("C:\\Users\\Deniz\\Desktop\\newberry.png"));
                          }
                          catch(Exception exception)
                          {
                              System.out.println("The exception caught is: " + exception);
                          }
                      }
              
                      public void mouseEntered(MouseEvent e)
                      {
              
                      }
              
                      public void mouseExited(MouseEvent e)
                      {
              
                      }
              
                      public void mouseDragged(MouseEvent e)
                      {
                          currentX = e.getX();
                          currentY = e.getY();
                          repaint();
                      }
              
                      public void mouseMoved(MouseEvent e)
                      {
              
                      }
                  }
              
                  protected void paintComponent(Graphics g)
                  {
                      super.paintComponent(g);
                      g.drawLine(previousX, previousY, currentX, currentY);
                      annotateImage();
                      previousX = currentX;
                      previousY = currentY;
                  }
              
                  public static void main(String[] args)
                  {
                      new MyGoodPaint();
                  }
              }
              Edit:
              Darryl Burke: I forgot to say thank you :).

              Edited by: s3a on May 29, 2011 1:10 PM