0

I am trying to read epub book using C#.NET but I am getting the following excetion: Object reference not set to instance of an object.

I am trying epub for the first time and this is where I have referred.

Code:

static void Main(string[] args)
        {
            try
            {
                Epub book = new Epub(@"d:\test.epub"); //this is the line where i am getting error
                String Title = book.Title[0];
                String Author = book.Creator[0];

                Console.WriteLine("Name:" + Title);
                Console.WriteLine("Author:" + Author);

            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            Console.ReadKey();
        }

Any ideas?? Thanks!!

SureshS
  • 589
  • 8
  • 23
  • The `test.epub` file could be corrupted or have unexpected(from lib point of view) format. Does this happen for all the `epub` files you have? – alex.b May 15 '14 at 03:54
  • @GrantWinney I tried single step debugging and the exception was on the same line. Tried moving the file to desktop, my documents but no change. – SureshS May 15 '14 at 03:56
  • @Downvoter: I have gone through that answer before posting here but it talks about how object can be null and how to check if it is not null before accessing it. – SureshS May 15 '14 at 03:59
  • @aleksey.berezan Thanks, that helped. Tried many ways but not the one you suggested. It is working fine now.:-) – SureshS May 15 '14 at 04:00

1 Answers1

1

I've tried to run your code locally and it worked on my machine. And it works for other epub files, as you've said in comment.
Also I've taken a brief look into Epub's code-base and didn't find anything that could obviously cause the error, plus NullReferenceException means that:

  1. Your epub file is broken or corrupted
  2. Your epub file does not have some specific piece of (meta)data that Epub lib expects.

In both cases I'd recommend you to try to re-create/re-convert that not working d:\test.epub file or abandon it :)

alex.b
  • 4,547
  • 1
  • 31
  • 52