Log in

View Full Version : Java Question...


mirkazemisaman
11-28-2003, 07:38 PM
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):

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.

Janak Parekh
11-28-2003, 08:41 PM
I avoid graphics programming nowadays, but the line 21 error is simply chaining to the line 15 error, which is
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

JonMisurda
11-28-2003, 09:08 PM
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):


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

Janak Parekh
11-28-2003, 09:10 PM
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! :oops: 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

mirkazemisaman
11-29-2003, 01:49 AM
thanx, that makes sence. I thought my Image object was some how null and I oculdn't figure it out...

mirkazemisaman
12-04-2003, 01:58 AM
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

maximus
12-04-2003, 08:18 AM
DirectX :wink: