joniles / MPXJ.Net

A .Net package providing a "native" wrapper around the MPXJ Java library
https://www.mpxj.org/
3 stars 2 forks source link

NullReferenceException when writing to stream in MSPDI format #12

Closed szadev closed 2 months ago

szadev commented 2 months ago

Code:

public Stream SaveToXml(ProjectFile project)
{
    var ms = new MemoryStream();
    new UniversalProjectWriter(FileFormat.MSPDI).Write(project, ms);
    return ms;
}

Problem: It works, when I use MPX format, but with XML, VisualStudio hangs completely at first, then gives a NullReferenceException. ProjectFile created from real ms project file with MPP format. Stack trace: pict

joniles commented 2 months ago

Hello! Thanks for opening the issue.

I have a suspicion that this is just down to the way the JAXB library used by MPXJ to handle XML data works, in that in some cases it uses exceptions as part of its normal flow control. If you ignore exceptions raised from the JAXB library in Visual Studio, or just allow the debugger to continue running after the exception you've trapped, the export should complete successfully. Visual Studio doesn't seem to be a fan of this particular library code, so it often seems to run very slowly when run through the debugger. Running it outside of the debugger it seems to run at a more acceptable speed.

Let me know how you get on!

szadev commented 2 months ago

Yeah, it works with try catch without debug. Thank you!

szadev commented 1 month ago

The problem also occurs when reading a file. It only happens on the first call when the application is running, and other attempts to call methods are fine. This makes debugging the application more difficult, but is not a critical issue. Also, I saw the exception messages in the debugging window:

  1. exception "java.lang.NoSuchMethodException" in IKVM.Java.dll
  2. exception "java.lang.ClassNotFoundException" int IKVM.Runtime.dll
joniles commented 1 month ago

In Visual Studio Tools->Options->Debugging->Enable Just My Code might help to keep the noise down.

szadev commented 1 week ago

Actually, Just My Code does not help, but I found that you can disable certain Java exceptions. Here it is: Ctrl+Alt+E or Debugging -> Windows -> Exception Parameters, then in the opened tab you can disable Java exceptions.

P.S. I cloned this repo and wrote tests for the UniversalProjectWriter and catched this problem while running the tests in debug.