-2

Could somebody help me please? I've done this

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Frame.Painter.paint(Main.java:399) at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264) at sun.awt.RepaintArea.paint(RepaintArea.java:240) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358) at java.awt.Component.dispatchEventImpl(Component.java:4965) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

and code in row 399 is :

class Painter extends Canvas{
    Image image;
    private URL url;
    public void setImage(String file) {
        url = null;
        try {
            url = new File(file).toURI().toURL();
        } catch (MalformedURLException ex) {
            System.out.println(ex.toString());
        }
        image = getToolkit().getImage(url);
        repaint();
    }
    public void paint(Graphics g) {
        double d = image.getHeight(this) / this.getHeight();
        double w = image.getWidth(this) / d;
        double x = this.getWidth() / 2 - w / 2;
        g.drawImage(image, (int) x, 0, (int) (w), this.getHeight(), this);
    }
}

I dont know exactly what's going on there. My program work correctly but I felt disturbing with this error every run. Please tell me what should I do.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 5
    1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! .. – Andrew Thompson Jan 27 '17 at 19:28
  • 2
    .. 3) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jan 27 '17 at 19:30
  • 2
    Continuing with the list of @AndrewThompson: 5) Don't override `paint()` method, instead use `paintComponent()` and don't forget to call `super.paintComponent()` as the first statement in it and use the `@Override` annotation. 6) Don't mix AWT Components like `Canvas` with Swing components, use a `JPanel` instead. 7) Most probably your NPE is due to the image not being found, follow the *embedded resource* link provided above and try printing its value or debug your code with break points to see if it's null – Frakcool Jan 27 '17 at 20:06
  • you will not find this kind of people anywhere else other than the GUI - its a special kind of nutso's – gpasch Jan 27 '17 at 21:48

1 Answers1

-2

I'm assuming you have the following code:

class Painter extends Canvas{
    Image image;
    private URL url;
    public void setImage(String file) {
        image = Toolkit.getDefaultToolkit().getImage(file);
        repaint();
    }
    public void paint(Graphics g) {
      if(image!=null) {
        double d = image.getHeight(this) / this.getHeight();
        double w = image.getWidth(this) / d;
        double x = this.getWidth() / 2 - w / 2;
        g.drawImage(image, (int) x, 0, (int) (w), this.getHeight(), this);
      }
    }
}

then you just have to call setImage("c:/yourfilepath/yourfilename")

gpasch
  • 2,672
  • 3
  • 10
  • 12
  • 1
    *"then you just have to call setImage("c:/yourfilepath/yourfilename")"* Except for the lack of conversion to an URL, this really adds nothing to the solution. If the path was right in the code shown in the question, it would have worked. This also fails to fix the broken paint chain mentioned in point 5 of the advice by @Frakcool. All up, little more than noise, and no better than should have been expressed in a comment. – Andrew Thompson Jan 27 '17 at 21:20
  • "Except for the lack of conversion to an URL": except you continue to pile your own crap – gpasch Jan 27 '17 at 21:26
  • @AndrewThompson Unless `paint` was called before `setImage` – MadProgrammer Jan 27 '17 at 21:35
  • @MadProgrammer Good ..comment. Short an MCVE we cannot know for sure. – Andrew Thompson Jan 27 '17 at 21:47
  • @AndrewThompson Agreed, it's all "guess" work ;) – MadProgrammer Jan 27 '17 at 21:52