Jump to content

[1.7.2] Null pointer exeption when getting array


Bugzoo

Recommended Posts

I make two variables and store 'array[1]' in the first and 'array[2]' in the second, but when I use 'array[2]' I get a NullPointerExeption

	static File path = new File(Minecraft.getMinecraft().mcDataDir, "//TVMod"); 
public static File[] filesList = path.listFiles();

static File imagePath = new File(Minecraft.getMinecraft().mcDataDir, "//TVMod//TVTexture.png");
static File overlayLocation = new File(Minecraft.getMinecraft().mcDataDir, "//TVMod//");


static File overlayPath1 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\" + filesList[1].getName()); 
static File overlayPath2 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\" + filesList[2].getName()); 


public static BufferedImage overlay;
public static BufferedImage image;

        public static void PictureOverlay1() throws IOException{
	overlay = ImageIO.read(new File(overlayPath2, ""));
	image = ImageIO.read(new File(imagePath, ""));

	if(!path.exists()){
	System.out.println("Creating File");
	path.mkdir();

	} else {
		System.out.println("Already Created");

			// create the new image, canvas size is the max. of both image sizes
			int w = Math.max(image.getWidth(), overlay.getWidth());
			int h = Math.max(image.getHeight(), overlay.getHeight());
			BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

			// paint both images, preserving the alpha channels
			Graphics g = combined.getGraphics();
			g.drawImage(image, 0, 0, null);
			g.drawImage(overlay, 0, 0, null);

			// Save as new image
			ImageIO.write(combined, "PNG", new File("C:/Users/Daniel/Documents/Brain Bang/Coding/Minecraft Modding/New  Mods/1.6.4 Television Mod/forge/mcp/src/minecraft/assets/tvmod/textures/blocks/combined.png"));
		}
		}

Link to comment
Share on other sites

path.listFiles() probably return an array of 2 elements, but mind that you count from 0 when using arrays (or in programming in general), so filesList[0] and filesList[1] get you the first and second elements. That might not be the whole problem though, because I suppose the error would have been IndexOutOfBoundsException, not NullPointerException. Are you sure the exception raises only on filesList[2]?

Check out my blog!

http://www.whov.altervista.org

Link to comment
Share on other sites

Hi

 

I think there might be several problems causing your error (assuming it's an ArrayOutOfBounds and not a NullPointerException) -

the first is that array counting starts from 0 not 1 like Whov said.

the second is - what happens if path.listFiles() doesn't return any entries, perhaps because the directory is empty?

the third is that all those static variable initialisations might occur before mcDataDir has been initialised, so the directory may be invalid.  Better to initialise them in the constructor, and construct the object in your preInitialisation phase.

 

If you fix those things and it still doesn't work, show us your error log?

 

-TGG

 

 

Link to comment
Share on other sites

Hi

 

I think there might be several problems causing your error (assuming it's an ArrayOutOfBounds and not a NullPointerException) -

the first is that array counting starts from 0 not 1 like Whov said.

the second is - what happens if path.listFiles() doesn't return any entries, perhaps because the directory is empty?

the third is that all those static variable initialisations might occur before mcDataDir has been initialised, so the directory may be invalid.  Better to initialise them in the constructor, and construct the object in your preInitialisation phase.

 

If you fix those things and it still doesn't work, show us your error log?

 

-TGG

 

 

 

 

Thanks I fixed it

 

 if (pictureList != null && pictureList.length>0){
  overlay1 = new File(Minecraft.getMinecraft().mcDataDir, "\\TVMod\\Pictures\\" + pictureList[0].getName());
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.