Index: /trunk/gli/src/org/greenstone/gatherer/msm/GDMDocument.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/msm/GDMDocument.java	(revision 4514)
+++ /trunk/gli/src/org/greenstone/gatherer/msm/GDMDocument.java	(revision 4515)
@@ -201,7 +201,7 @@
 	}
 	try {
-				// Retrieve the document element.
+	    // Retrieve the document element.
 	    Element directorymetadata_element = base_document.getDocumentElement();
-				// Iterate through the filesets, checking the FileName child element against 	the target file's name using regular expression matching.
+	    // Iterate through the filesets, checking the FileName child element against 	the target file's name using regular expression matching.
 	    NodeList fileset_elements = directorymetadata_element.getElementsByTagName(FILESET_ELEMENT);
 	    for(int i = 0; i < fileset_elements.getLength(); i++) {
@@ -235,41 +235,46 @@
 				else {
 				    ElementWrapper element = Gatherer.c_man.getCollection().msm.getElement(raw_element);
-				    GValueNode value = Metadata.getDefaultValueNode(element, raw_value);
-				    ///ystem.err.println("Miss. Create new metadata: " + raw_element + " -> " + raw_value + "\n");
-				    metadata = new Metadata(element, value);
-				    GDMManager.metadata_cache.put(raw_element, raw_value, metadata);
+				    if (element != null) {
+					
+					GValueNode value = Metadata.getDefaultValueNode(element, raw_value);
+					///ystem.err.println("Miss. Create new metadata: " + raw_element + " -> " + raw_value + "\n");
+					metadata = new Metadata(element, value);
+					GDMManager.metadata_cache.put(raw_element, raw_value, metadata);
 				    ///ystem.err.println("Added metadata to cache: " + raw_element + " -> " + raw_value + "\n");
-				    value = null;
-				    element = null;
+					value = null;
+					element = null;
+				    } 
 				}
+				// check whether the metadata is null
+				if (metadata != null) {
 				// We determine whether this metadata is file or folder level
-				if(filename != null) {
-				    ///ystem.err.println("Filename      = " + filename);
-				    ///ystem.err.println("filename_text = " + filename_text); 
-				    // If can only be file level if there is no folder path details in filename and if the filename matched the filename text node (it may have matched .* instead)!
-				    if(filename.indexOf(File.separator) == -1 && filename.equals(filename_text)) {
+				    if(filename != null) {
+					///ystem.err.println("Filename      = " + filename);
+					///ystem.err.println("filename_text = " + filename_text); 
+					// If can only be file level if there is no folder path details in filename and if the filename matched the filename text node (it may have matched .* instead)!
+					if(filename.indexOf(File.separator) == -1 && filename.equals(filename_text)) {
+					    metadata.setFileLevel(true);
+					    ///ystem.err.println("File level!!!");
+					}
+					else {
+					    metadata.setFileLevel(false);
+					    ///ystem.err.println("Inherited!!!");
+					}
+				    }
+				    else {
+					///ystem.err.println("Filename is null therefore this is file level metadata.");
 					metadata.setFileLevel(true);
-					///ystem.err.println("File level!!!");
+				    }
+				    metadata.setFile(file);
+				    
+				// If mode is overwrite, then remove any previous values for this metadata element.
+				    if(mode.equals("accumulate")) {
+					metadata.setAccumulate(true);
 				    }
 				    else {
-					metadata.setFileLevel(false);
-					///ystem.err.println("Inherited!!!");
-				    }
-				}
-				else {
-				    ///ystem.err.println("Filename is null therefore this is file level metadata.");
-				    metadata.setFileLevel(true);
-				}
-				metadata.setFile(file);
-
-				// If mode is overwrite, then remove any previous values for this metadata element.
-				if(mode.equals("accumulate")) {
-				    metadata.setAccumulate(true);
-				}
-				else {
-				    metadata.setAccumulate(false);
-				    ///ystem.err.println("Metadata overwrites: " + metadata);
-				    for(int m = metadatum.size() - 1; m >= 0; m--) {
-					Metadata old_metadata = (Metadata) metadatum.get(m);
+					metadata.setAccumulate(false);
+					///ystem.err.println("Metadata overwrites: " + metadata);
+					for(int m = metadatum.size() - 1; m >= 0; m--) {
+					    Metadata old_metadata = (Metadata) metadatum.get(m);
 					if(old_metadata.getElement().equals(metadata.getElement())) {
 					    metadatum.remove(m);
@@ -277,34 +282,34 @@
 					}
 					old_metadata = null;
-				    }
-				}
-				mode = null;
-
+					}
+				    }
+				    mode = null;
+				    
 				// Add the completed metadata and clean up
 				///ystem.err.println("Adding metadata: " + metadata);
-				metadatum.add(metadata);
-
+				    metadatum.add(metadata);
+				    
 				// Having found our metadata check if the value from the xml matches the one from the gvaluenode. If not update it. This happens whenever hierarchy information is involved (indexes rapidly become obsolete).
 				// If remove was set, remove it. We can only remove pure file level metadata, or folder level iff we were asked for folder level.
-				if(remove && ((filename != null && filename.matches(filename_text) && !filename_text.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME)))) {
-				    ///ystem.err.println("Removing " + metadata + " from " + file);
-				    description_element.removeChild(metadata_element);
-				    // Remove the description element if empty.
-				    if(!description_element.hasChildNodes()) {
-					fileset_element.removeChild(description_element);
+				    if(remove && ((filename != null && filename.matches(filename_text) && !filename_text.equals(DIRECTORY_FILENAME)) || (filename == null && filename_text.equals(DIRECTORY_FILENAME)))) {
+					///ystem.err.println("Removing " + metadata + " from " + file);
+					description_element.removeChild(metadata_element);
+					// Remove the description element if empty.
+					if(!description_element.hasChildNodes()) {
+					    fileset_element.removeChild(description_element);
+					}
+				    }
+				    else {
+					String current_value = metadata.getAbsoluteValue();
+					if(!raw_value.equals(current_value)) {
+					    // Remove old text
+					    while(metadata_element.hasChildNodes()) {
+						metadata_element.removeChild(metadata_element.getFirstChild());
+					    }
+					    // Add new.
+					    metadata_element.appendChild(base_document.createTextNode(current_value));
+					}
 				    }
 				}
-				else {
-				    String current_value = metadata.getAbsoluteValue();
-				    if(!raw_value.equals(current_value)) {
-					// Remove old text
-					while(metadata_element.hasChildNodes()) {
-					    metadata_element.removeChild(metadata_element.getFirstChild());
-					}
-					// Add new.
-					metadata_element.appendChild(base_document.createTextNode(current_value));
-				    }
-                                }
-
 				metadata = null;
 				raw_value = null;
