Thoughts Media.com

 




  #1 (permalink)  
Old 11-28-2003, 07:38 PM
Intellectual
Join Date: Jul 2003
Posts: 218
Default Java Question...

Alright, I am quiet a novice in java programming. I tried to search on google for source code to upload images and tried them. But I am still getting errors when trying to run my program. So as a last resort I came here to ask you guys since I know I can always get solutions here...
Here is my sample code (sorry, for some reason the message board takes out my bracket indentations):

Quote:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Toolkit.*;

public class TestImage extends JFrame
{
private Image picture;
private Graphics gfx;
private Toolkit toolkit = Toolkit.getDefaultToolkit();
public TestImage()
{
picture = toolkit.getImage("fivecards.gif");
gfx = this.getGraphics();
gfx.drawImage(picture, 0, 0, this);
this.setSize(500, 500);
this.show();
}
public static void main(String [] args)
{
TestImage window = new TestImage();
}
}
The image is in the same directory as the file and the code compiles, but i get a run-time error (NullPointerException in "main") at lines 15 and 21 (1st line is the 1st import statement). Can someone enlighten me as to what I am doing wrong????

thanx in advance.
 
Reply With Quote
  #2 (permalink)  
Old 11-28-2003, 08:41 PM
Contributing Editor
Janak Parekh's Avatar
Join Date: Aug 2006
Posts: 14,942

I avoid graphics programming nowadays, but the line 21 error is simply chaining to the line 15 error, which is
Code:
gfx.drawImage(picture, 0, 0, this);
It means that "picture" is null. Put a test right above it to confirm. As to why it's null, it means the toolkit.getImage() call is failing. Why, I don't know, I don't have enough data to be able to tell.

--janak
 
Reply With Quote
  #3 (permalink)  
Old 11-28-2003, 09:08 PM
Ponderer
Join Date: Oct 2002
Posts: 70

Actually, what's null is the Graphics object. You can't draw on the screen before the window is shown. This code should work (i.e. It works for me):

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.Toolkit.*;

public class TestImage extends JFrame
{
	private Image picture; 
	private Graphics gfx;
	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	
	public TestImage()
	{
		picture = toolkit.getImage("fivecards.gif");
		this.setSize(500, 500);
		this.show();
	}

	public void paint(Graphics gfx) {
		gfx.drawImage(picture, 0, 0, this);
	}

	public static void main(String [] args)
	{
		TestImage window = new TestImage();
	}
}
Hope that helps,

Jon
 
Reply With Quote
  #4 (permalink)  
Old 11-28-2003, 09:10 PM
Contributing Editor
Janak Parekh's Avatar
Join Date: Aug 2006
Posts: 14,942

Quote:
Originally Posted by JonMisurda
Actually, what's null is the Graphics object. You can't draw on the screen before the window is shown. This code should work (i.e. It works for me):
D'oh! ops: I stand corrected. Like I said, I've been avoiding AWT/Swing programming for some time now, and any Swing programming I do now is strictly UI stuff...

--janak
 
Reply With Quote
  #5 (permalink)  
Old 11-29-2003, 01:49 AM
Intellectual
Join Date: Jul 2003
Posts: 218

thanx, that makes sence. I thought my Image object was some how null and I oculdn't figure it out...
 
Reply With Quote
  #6 (permalink)  
Old 12-04-2003, 01:58 AM
Intellectual
Join Date: Jul 2003
Posts: 218

I am going to get started learning some sort of 3D programming (I only do Java). Which of these two is better for a beginner:

Java 3D API
OpenGL
 
Reply With Quote
  #7 (permalink)  
Old 12-04-2003, 08:18 AM
Magi
Join Date: Jul 2003
Posts: 2,047
Send a message via ICQ to maximus

DirectX :wink:
 
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +1. The time now is 12:02 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC7
Copyright Thoughts Media Inc. 2007