-2
I’m developing a web application where a photographer does upload of an image. After uploading the image, I will scan the entire Metadata in order to capture some information from directories (Exif, IPTC, XMP, JFIF, PNG, BMP, JPG, etc.), I am using the library Metadata Extractor. Ok, I learned to use it. First I need to receive the directories:
IEnumerable<MetadataExtractor.Directory> directories = iptcReader.ReturnMetadataFile(Path.Combine(StorageRootPOOL, imagePool.File_name[i]));
Here I already have all the directories of my image, after that I need to declare variables of each directory I need to capture in my case:
var IPTCDirectory = directories.OfType<IptcDirectory>().FirstOrDefault();
var JPEGDirectory = directories.OfType<JpegDirectory>().FirstOrDefault();
var JFIFDirectory = directories.OfType<JfifDirectory>().FirstOrDefault();
var EXIFDirectory = directories.OfType<ExifIfd0Directory>().FirstOrDefault();
var EXIFSubDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
All values of directories ARE NOT NULL even if there is no directory in the image, the problem is in the tags that exist within them. To capture the tags inside the directories I use the following code:
imageFileInfo.Keywords = IPTCDirectory.GetDescription(IptcDirectory.TagKeywords);
imageFileInfo
is the class where I will be storing all the data. My problem is, if for example there is no TAG IptcDirectory.TagKeywords
I will receive a Nullexception, so far nothing out of the ordinary, but if I will prevent this error with an if for example:
if (IPTCDirectory.GetDescription(IptcDirectory.TagKeywords) == null)
{
}
I get another one NullException
, there’s the problem, I wanted to set a default value for tags that don’t exist, for example:
if (IPTCDirectory.GetDescription(IptcDirectory.TagKeywords) == null)
{
imageFileInfo.Keywords = "valor padrão";
}
Remembering that it is no use I use the following code, because I need to know which tags are null and not which directories, because directories always exist, but some tags nay:
if (EXIFDirectory != null && ExifIfd0Directory != null) {
//Os diretórios nunca são nulos e sim as tags, então esse exemplo não iria se aplica ao meu problema.
}
Bigown, alright, I will review what is correct or not, but the problem in this case is that there are certain tags for example from
EXIFDirectory
that it doesn’t exist, and I fall into that problem you understand? Has image that has and image that does not have, for those that do not have for example the tag "Datetimeoriginal"imageFileInfo
, Do you have any idea how I can resolve this without using the treatment cited as wrong?– Leonardo Bonetti
What I mean is, the directory itself is never null, but some tags inside it are, and I can’t deal with them with if because within the if expression I get an Exception null.
– Leonardo Bonetti
None of this is in your question, if you said the default value I would put in my code. I gave a base by not processing the item that is null. Your
if
is completely wrong, mine is probably right, just do not guarantee because the question has no details, there is no way I test. Fix theif
instead of using the wrong mechanism.– Maniero
I am reformulating the question more clearly, I thank you already (really), I prefer to follow the more laborious and correct side :)
– Leonardo Bonetti
There is nothing more laborious, unless you are referring to the work of learning to do the right, which I strongly recommend, exception is almost never the solution, most programmers do not know this, most believe it is good practice to make exception at all, what I already question the idea of good practice (https://answall.com/search?q=user%3A101+boas+pr%C3%A1ticas), the more that is good.
– Maniero
I agree Bigown, I even questioned the guy with the "right" question, but I’ve corrected my mistake. I rephrased the question, please see if it was clearer, your answer did not solve my problem (perhaps because I had not made clear the problem).
– Leonardo Bonetti
You made a nice change to the question, not only added details. There are new things that didn’t exist before and even what you had disappeared. I’m thinking of closing as unclear, because it’s getting more confusing, maybe the problem is the confusion.
– Maniero
There is no Getproperty feature in this library: github.com/drewnoakes/Metadata-extractor
– Leonardo Bonetti
I changed it because the other answer used it and you accepted it. So I thought that was correct in your code. If you accept an answer that doesn’t work, you are creating wrong information for the community. Was the old way (https://answall.com/revisions/221991/5) I did it right? I changed because I thought I had done the right thing, but the other acceptance made me see that I was wrong.
– Maniero
I really confused the balls, sorry, the old one doesn’t work either, because I fall into that IF that returns me null. For now it only works with Try catch, I’m going back because I don’t want you to have the wrong answer for the community.
– Leonardo Bonetti
I don’t fall for
if
some because there is noif
in my reply. If none of this works, I think it’s best to close the question because you can’t provide enough information to help. You’re making a lot of trouble and that’s why you don’t have an answer to help you.– Maniero
Dude, I made it clear in the question issue, including, your answer as I already said 300x does not solve, that comparison
== null
activates the nullException exception as I have already said.– Leonardo Bonetti
is the only correct solution, if it does not solve, or lack information in the question, or is doing wrong.
– Maniero
No, it’s not the right solution, if it returns an error because it would be correct? I don’t want to be rude, but it’s just not correct. I’ll even try it again and post the print.
– Leonardo Bonetti
Okay, so if you know more than me, do as you please. Everyone who knows how to program in C# knows that this is the correct solution, that using exception is not correct. If it doesn’t work for you, it’s because your question doesn’t tell you correctly what you’re doing or implementing it wrong. When you realize that you will have the right solution, if you do not want to do it this way, do it the wrong way, it is your code that will get bad and it is your right to do it wrong. Right and works are very different things, what works one day has problems because it was not the right one. Every programmer should know this.
– Maniero
That’s the code, well-voted for who wants to learn to do right. That may not work s because the question does not clearly say where the error is.
– Maniero
Bigown, I didn’t say that an if is better than a Try catch, or something like, I said that that stretch you passed, is not correct, it returns me the same error when I do
if (IPTCDirectory.GetDescription(IptcDirectory.TagKeywords) == null)
 {
 }
You understand it a million times more than I do, I’m just saying it’s not solved with that answer.– Leonardo Bonetti
Because the question is not clear, so I closed it, if it was clear I would put the code that prevents it.
– Maniero
If that’s not a clear question, I don’t know what it is anymore... but beauty.
– Leonardo Bonetti
If you don’t have the information to give you a proper response, it’s not clear, but you don’t want to hear it so beauty, the only one who’s hurt is you, only you won’t have the proper code for what you need. I answered what was asked, if you need something that is not in the question has no way to answer.
– Maniero