7 Replies Latest reply on Mar 9, 2002 9:18 PM by 807593

    NullPointerException

    807592
      I get this Exception at runtime:

      Exception occurred during event dispatching:
      java.lang.NullPointerException
      at java.awt.image.BufferedImage.setRGB(BufferedImage.java)
      at MyApplet.paint(MyApplet.java:39)
      at java.awt.Container.update(Container.java)
      at sun.awt.RepaintArea.update(RepaintArea.java)
      at com.apple.mrj.internal.awt.VComponentPeer.reallyDoThePaintOrUpdate(VComponentPeer.java)
      at com.apple.mrj.internal.awt.VComponentPeer.handlePaintEvent(VComponentPeer.java)
      at com.apple.mrj.internal.awt.VComponentPeer.handleEvent(VComponentPeer.java)
      at java.awt.Component.dispatchEventImpl(Component.java)
      at java.awt.Container.dispatchEventImpl(Container.java)
      at java.awt.Component.dispatchEvent(Component.java)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java)
      at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java)

      This only has happened since I added a setRGB method for a BufferedImage into the code. Without that, it ran fine. Can anyone explain what's going wrong?
        • 1. Re: NullPointerException
          807592
          NullPointerException means you are using a value that has been set to null incorrectly. Post up some code so we can help you more.
          • 2. Re: NullPointerException
            807592
            Well, I can't post all the code here, but I can tell you that I've checked all the arguments of the setRGB expression and they're all legit. I have a theory:
            I called a getRGB method right before I called the setRGB method. The getRGB method filled the int[] "paste" with the colors from a BufferedImage. paste is used in the setRGB method and is not defined before the getRGB. Could it be that setRGB was started before getRGB was finished, and therefore accessed paste when it was still not defined? If so, what do I do to make the applet wait until the getRGB is done?

            Matt
            • 3. Re: NullPointerException
              807592
              Well, I've tried several different variations of this code and I always get the same NullPointerException refering to that setRGB, even when I change the references to thing that are definately not undefined or anything. Is there some problem with the setRGB itself? that's the only conclusion I can come to. It compiled fine with this, so I don't know what's happening at all at runtime. HELP!

              OR, what's another way to take an array of pixel colors from one image and put them in another (that works!)?

              Matt
              • 4. Re: NullPointerException
                807593
                is my problem just inexplicable or what?

                workarounds?

                Matt
                • 5. Re: NullPointerException
                  807593
                  What version of the JDK are you using? What constructor of BufferedImage are you using and what methods before you get the exception?

                  In 1.3.1_02, you can only get a NullPointerException inside one of the BufferedImage.setRGB methods themselves if either the object's "raster" or "colorModel" field has become null. The only way I can see of how this can happen is if you made a call to the coerceData method and it failed, or the constructor itself had failed somehow.
                  • 6. Re: NullPointerException
                    807593
                    I have version 1.3.1 (came with MacOS X), and I don't know what coerceData is. The color model reference is supposed to be filled by a getRGB method called right before I call the setRGB method. The BufferedImage is made with the following line:

                    BufferedImage play = new BufferedImage(700, 469, BufferedImage.TYPE_INT_RGB);

                    at the beginning of the applet.

                    Does any of that help?

                    Matt
                    • 7. Re: NullPointerException
                      807593
                      ok Ireally can't figure this out, so here's the code. What's wrong with it? To see the errors that I get at runtime, see the first message in this topic.

                      Matt

                      import java.awt.*;
                      import java.awt.image.*;
                      import java.applet.Applet;

                      public class HorizonFinder extends Applet {
                      Image source;
                      int horizon;
                      BufferedImage play = new BufferedImage(700, 469, BufferedImage.TYPE_INT_RGB);
                      int[] paste;
                      public void init() {
                      Toolkit kit = Toolkit.getDefaultToolkit();
                      source = kit.getImage("source3.jpg");
                      MediaTracker m = new MediaTracker(this);
                      m.addImage(source, 1);
                      try { m.waitForAll();
                      } catch (Exception exec){}
                      Graphics gp = play.getGraphics();
                      gp.drawImage(source, 0, 0, null);
                      }
                      public void paint(Graphics g) {
                      play.getRGB(0, 0, 1, 469, paste, 0, 1);
                      Color ca; Color cb;
                      int bluea; int blueb;
                      int contrast = 0;
                      for (int y=100; y<369; y++) {
                      ca = new Color(play.getRGB(1, y));
                      cb = new Color(play.getRGB(1, y+1));
                      bluea = ca.getBlue();
                      blueb = cb.getBlue();
                      if (Math.abs(bluea-blueb)>contrast) {
                      contrast = Math.abs(bluea-blueb);
                      horizon = -y;
                      }
                      }
                      //***************
                      // Bunch of errors at runtime since I added the following line, with the setRGB
                      //***************
                      play.setRGB(0, horizon, 1-horizon, 469, paste, 1-horizon, 1);
                      g.drawImage(play, 0, 0, this);
                      }
                      }