aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Still not working..help please. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Still not working..help please." Watch "Still not working..help please." New topic
Author

Still not working..help please.

Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
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).]
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Bryan Fagan
Greenhorn

Joined: Feb 26, 2001
Posts: 17
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

Joined: Feb 03, 2001
Posts: 142
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

Joined: Oct 18, 2000
Posts: 4121

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

Joined: Feb 03, 2001
Posts: 142
Thanks Nate!!! That was a big help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Still not working..help please.
 
Similar Threads
"database engine could not lock table"..error
win98 v. WinNT
Help with Layout
Finding base directory of main class file
Help please