• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Still not working..help please.

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to get a file from my computer and put it on a canvas.
Since, I'm a little green, I cane to a dead end and the API wasn't able to help me. I can do what is asked through an applet, but not through stand alone app. thanks in advance.
In this example I keep catching a null point exception. The .gif file is in that folder!! and my class path is set up correctly. I am also cmpiling and running from withing the folder where the .gif and .class are in.
Many thanks in advance.
Here's a snippet of code from a larger application. I must use the AWT since I am studying for the exam.

[This message has been edited by Brian Snyder (edited February 25, 2001).]
[This message has been edited by Brian Snyder (edited February 25, 2001).]
[This message has been edited by Brian Snyder (edited February 25, 2001).]
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,
You are not getting the null pointer exception from the image, you are getting it from the Graphics object canvasG... if you try to get a graphics object from something before it is displayed, you will get null since the component does not have a graphics context yet... The way that I would fix it would be to put an if statement in the paint method that looks like this...

This assumes that can, canvasG and im are able to be accessed in the paint method... i.e. that they are declared as attributes of the class ( which they should be in this case... don't want to create new objects in the paint() method... it gets called alot. Creating the Toolkit object inside paint() is OK since it only needs to be used if the if statement is run. )
HTH,
-Nate
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian:
Did you define Paint? I can see your constructor, but I don't see paint defined. I did a test and left out the paint method. I received a null exception. When I defined paint everything worked fine.
can = new Canvas();
Toolkit toolkit = Toolkit.getDefaultToolkit();
im = toolkit.getImage("c:\\computr2.GIF");
try {

this.setSize(110,110);
this.setVisible(true);
getContentPane().add(can);
repaint();

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});

} catch (NullPointerException e) {

System.out.println("Can't find file:" + e.getMessage());

}

}
public void paint(Graphics g) {
g.drawImage(im, 0, 0, 100, 100, this);
}
public static void main(String[] args) {
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bryan and Nate!!!
I've got it working now. I didn't realize that I have to "roll out" the canvas before I could paint on it. This was a big step for me. I really appreciate the time you guys took to help out.
Bryan, I was able to get yours to work.
Nate, I was still getting a null pointer exception. Here's all my code, with Bryan's code in it, if you feel like searching. If not, do not worry about it. It'll be a good learning experience for me.
Thanks again to both!!

Hopefully, I can return the favor someday soon.


[This message has been edited by Brian Snyder (edited February 26, 2001).]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,
In the code you gave you are painting directly to the Alg Frame not the canvas. ( To see that this is true, comment out all the lines relating to Canvas can... the program will still work the same! ) There is not really a problem doing it this way if it works fine... just remove the Canvas since it is not being used...
The code I posted probably gave a null pointer exception because I didn't post the whole thing... Here it is in completion... it is painting to the Canvas on the frame...

HTH,
-Nate
 
Brian Snyder
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nate!!! That was a big help.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic