joniles / mpxj

Primary repository for MPXJ library
http://www.mpxj.org/
GNU Lesser General Public License v2.1
250 stars 104 forks source link

MSPDIWriter writer = new MSPDIWriter(); is throwing an error #756

Closed pjacobs-git-01 closed 1 month ago

pjacobs-git-01 commented 1 month ago

I switched to MPXJ.Net version 13.4.1 The statement: MSPDIWriter writer = new MSPDIWriter(); throws an error: System.NullReferenceException: 'Object reference not set to an instance of an object.'

What an I doing wrong?

pjacobs-git-01 commented 1 month ago

Do you need to see my full code?

joniles commented 1 month ago

Hi, thanks for opening this issue. I think the problem here is that some of the libraries required as dependencies by MPXJ use exceptions under certain circumstances as part of normal control flow. It seems that by default Visual Studio picks up these exceptions and pauses execution. Hopefully you will find that when you click continue your code will run to completion without error.

Please let me know how you get on, I think this is an area which requires better documentation as it has tripped up a couple of users. The "Enable Just My Code" setting (Tools->Options->Debugging->General) in Visual Studio may prevent Visual Studio from reporting these exceptions, but I haven't had a chance to verify this myself.

pjacobs-git-01 commented 1 month ago

Hi Jon, Unfortunately the continue option doesn't work after the error is thrown. I have tried enabling and disabling the "Enable Just My Code" and that doesn't work either.

joniles commented 1 month ago

Can you provide the stack trace for the first exception you encounter?

pjacobs-git-01 commented 1 month ago
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.model.impl.ModelBuilder.ModelBuilder() Line 129    Java
[Native to Managed Transition]  
[Managed to Native Transition]  
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.model.impl.ModelBuilder.__<clinit>()   Unknown
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.model.impl.RuntimeModelBuilder.RuntimeModelBuilder()   Unknown
[Native to Managed Transition]  
[Managed to Native Transition]  
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.model.impl.RuntimeModelBuilder.__<clinit>()    Unknown
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.getTypeInfoSet() Line 403  Java
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.JAXBContextImpl(org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.JAXBContextBuilder builder) Line 251 Java
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.JAXBContextImpl(org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.JAXBContextBuilder x0, org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.1 x1) Line 77   Java
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl.JAXBContextBuilder.build() Line 1111   Java
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.ContextFactory.createContext(java.lang.Class[] classes, java.util.Map properties) Line 140 Java
org.glassfish.jaxb.runtime.dll!org.glassfish.jaxb.runtime.v2.ContextFactory.createContext(string contextPath, java.lang.ClassLoader classLoader, java.util.Map properties) Line 265 Java
[Lightweight Function]  
IKVM.Runtime.dll!IKVM.Java.Externs.sun.reflect.ReflectionFactory.FastMethodAccessorImpl.invoke(object obj, object[] args, ikvm.internal.CallerID callerID)  Unknown
IKVM.Java.dll!java.lang.reflect.Method.invoke(object obj, object[] args, ikvm.internal.CallerID value)  Unknown
jakarta.xml.bind.dll!jakarta.xml.bind.ContextFinder.newInstance(string contextPath, java.lang.Class[] contextPathClasses, java.lang.Class spFactory, java.lang.ClassLoader classLoader, java.util.Map properties) Line 187  Java
jakarta.xml.bind.dll!jakarta.xml.bind.ContextFinder.newInstance(string contextPath, java.lang.Class[] contextPathClasses, string className, java.lang.ClassLoader classLoader, java.util.Map properties) Line 145   Java
jakarta.xml.bind.dll!jakarta.xml.bind.ContextFinder.find(string factoryId, string contextPath, java.lang.ClassLoader classLoader, java.util.Map properties) Line 330    Java
jakarta.xml.bind.dll!jakarta.xml.bind.JAXBContext.newInstance(string contextPath, java.lang.ClassLoader classLoader, java.util.Map properties) Line 478 Java
jakarta.xml.bind.dll!jakarta.xml.bind.JAXBContext.newInstance(string contextPath, java.lang.ClassLoader classLoader) Line 435   Java
mpxj.dll!net.sf.mpxj.mspdi.MSPDIWriter.MSPDIWriter() Line 2673  Java
[Native to Managed Transition]  
[Managed to Native Transition]  
mpxj.dll!net.sf.mpxj.mspdi.MSPDIWriter.MSPDIWriter() Line 2721  Java
MPXJ.Net.dll!MPXJ.Net.MSPDIWriter.MSPDIWriter() Unknown

Cast.Plan.Core.dll!Cast.Plan.Core.Exporter.ExportProjectPlan(string filename, Cast.Plan.Core.ProjectPlan projectPlan) Line 86 C# Cast.Plan.Forms.dll!Cast.Plan.Forms.FormPlan.ExportToMsProject() Line 4563 C# Cast.Plan.Forms.dll!Cast.Plan.Forms.FormPlan.Export() Line 4536 C# Cast.Plan.Forms.dll!Cast.Plan.Forms.FormPlan.mnuExport_Click(object sender, System.EventArgs e) Line 4527 C# System.Windows.Forms.dll!System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs e) Unknown System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs e) Unknown System.Windows.Forms.dll!System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs e) Unknown System.Windows.Forms.dll!System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs mea) Unknown System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) Unknown System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) Unknown System.Windows.Forms.dll!System.Windows.Forms.ToolStrip.WndProc(ref System.Windows.Forms.Message m) Unknown System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(Windows.Win32.Foundation.HWND hWnd, Windows.Win32.MessageId msg, Windows.Win32.Foundation.WPARAM wparam, Windows.Win32.Foundation.LPARAM lparam) Unknown [Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.Primitives.dll!Windows.Win32.PInvoke.DispatchMessage(Windows.Win32.UI.WindowsAndMessaging.MSG lpMsg) Unknown System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.Microsoft.Office.IMsoComponentManager.FPushMessageLoop(nuint dwComponentID, Microsoft.Office.msoloop uReason, void pvLoopData) Unknown System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Microsoft.Office.msoloop reason, System.Windows.Forms.ApplicationContext context) Unknown System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Microsoft.Office.msoloop reason, System.Windows.Forms.ApplicationContext context) Unknown Cast.Plan.dll!Cast.Plan.Program.Main(string[] args) Line 63 C#

joniles commented 1 month ago

This is the code which is throwing the exception:

/**
 * Makes sure that we are running with 2.1 JAXB API,
 * and report an error if not.
 */
static {
    try {
        XmlSchema s = null;
        s.location();
    } catch (NullPointerException e) {
        // as epxected
    } catch (NoSuchMethodError e) {
        // this is not a 2.1 API. Where is it being loaded from?
        Messages res;
        if (SecureLoader.getClassClassLoader(XmlSchema.class) == null) {
            res = Messages.INCOMPATIBLE_API_VERSION_MUSTANG;
        } else {
            res = Messages.INCOMPATIBLE_API_VERSION;
        }

        throw new LinkageError( res.format(
            Which.which(XmlSchema.class),
            Which.which(ModelBuilder.class)
        ));
    }
}

As you can probably see a few lines down the NullPointerException is being caught and ignored by the code. You should be able to keep pressing continue in Visual Studio to get past these exceptions and your code will run as expected. I think you can disable breaking on particular exception types, if so I'd suggest ignoring java.lang.NullPointerException.

pjacobs-git-01 commented 1 month ago

Hi Jon, I suppressed NullPointerException errors in org.glassfish.* - that fixed the problem.

Thank you so much for your assistance. You have an excellent api.

joniles commented 1 month ago

Thank you so much for your assistance. You have an excellent api.

You're welcome, and thank you for the compliment. I have added a FAQ entry (https://www.mpxj.org/faq/#net) which will hopefully help other users to get past this problem.