Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.NullPointerException problem

843810Jan 8 2005 — edited Jan 18 2005
Hi, need some help here since this is my first Java application.
I've read some about NullPointerExceptions so I know what it is but not how to solve it.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;

public class Grabber extends Applet {

	/*
	 *  Purpose: Grab the pixeldata from an image on an URL
	 *  stored in a textfile. Replace the URL with the data.
	 *  Problem: java.lang.NullPointerException error when grabbing the image.
	 */
	
	public static void main(String[] args) {
		System.out.println("Grabber started");
		Grabber f = new Grabber();
		String oldData = f.readMyFile();
		System.out.println(oldData); // Test
		int[] imgData = f.GrabPixels(oldData);
		System.out.println("-----");
		System.out.print(imgData); // Test
		System.out.println(""); // Test
		f.writeMyFile(imgData); // Test, Will be replaced with a loop to print each array element on it's own line.
		System.out.println("Done");
	}

	String readMyFile() {
		String imageURL = null;
		DataInputStream dis = null;
		String record = null;
		int recCount = 0;
		try {

			File f = new File("C:/mydata.txt"); // One line text file with URL
			FileInputStream fis = new FileInputStream(f);
			BufferedInputStream bis = new BufferedInputStream(fis);
			dis = new DataInputStream(bis);
			// Just grab the first line since that's where the URL string is stored
			if ((record = dis.readLine()) != null) {
				imageURL = record;
			}

		} catch (IOException e) {
			// catch io errors from FileInputStream or readLine()
			System.out.println("Uh oh, got an IOException error!"
					+ e.getMessage());

		} finally {
			// if the file opened okay, make sure we close it
			if (dis != null) {
				try {
					dis.close();
					return imageURL;
				} catch (IOException ioe) {
				}
			}
		}
		return imageURL;
	}

	public int[] GrabPixels(String imageName) {
		int imageWidth, imageHeight;
		//get image
		try {
			MediaTracker mt = new MediaTracker(this);
			URL imageURL = new URL(imageName);
			Image initialImage = getImage(imageURL); // Here's the problem.
			//add the image to the media tracker's list
			mt.addImage(initialImage, 1);
			//wait for all images in list to load before proceeding
			mt.waitForAll();

			imageWidth = initialImage.getWidth(this);
			imageHeight = initialImage.getHeight(this);

			int[] img_data = new int[imageWidth * imageHeight];

			//the PixelGrabber class is used to convert
			//Images to bytes

			PixelGrabber pg = new PixelGrabber(initialImage, 0, 0, imageWidth,
					imageHeight, img_data, 0, imageWidth);

			//copy pixels into array
			pg.grabPixels();
			System.out.println(img_data[0]); // Test
			System.out.println(img_data[1]); // Test
			return img_data;

		} catch (MalformedURLException mue) {
			System.out
					.println("Malformed URL exception when trying to load image "
							+ imageName);
		} catch (InterruptedException ie) {
			System.out.println("Interrupted exception");
		}
		return new int[0];
	}

	void writeMyFile(int[] Data) {

		DataOutputStream dos = null;
		String record = null;
		int recCount = 0;

		try {

			File f = new File("C:/mydata.txt");
			FileOutputStream fos = new FileOutputStream(f);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			dos = new DataOutputStream(bos);
			OutputStreamWriter osw = new OutputStreamWriter(dos);
			osw.write(Data.toString(), 0, Data.toString().length());

		} catch (IOException e) {
			System.out.println("Uh oh, got an IOException error!"
					+ e.getMessage());

		} finally {
			// if the file opened okay, make sure we close it
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException ioe) {
				}
			}
		}
	}
}

Error:
Exception in thread "main" java.lang.NullPointerException
	at java.applet.Applet.getAppletContext(Unknown Source)
	at java.applet.Applet.getImage(Unknown Source)
	at Grabber.GrabPixels(Grabber.java:69)
	at Grabber.main(Grabber.java:21)
Thanks for your time
/TwoD
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2005
Added on Jan 8 2005
7 comments
526 views