joniles / mpxj

Primary repository for MPXJ library

Home Page:http://www.mpxj.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding new ExpenseItem causes NullPointerException when writing project

iMohannad opened this issue · comments

Hi,

I am trying to add additional expenses on all tasks in .xer file. I try to do it this way:

// Add additional expenses to task.
                    ExpenseItem e = new ExpenseItem(currentTask);
                    e.setName("Additional Expense");
                    e.setRemainingCost(java.lang.Double.valueOf(newCost - currentCost));
                    List expenseList = null;
                    if (currentTask.getExpenseItems() == null)
                    {
                        expenseList = new ArrayList();
                    } else
                    {
                        expenseList = currentTask.getExpenseItems();
                    }
                    expenseList.add(e);
                    currentTask.setExpenseItems(expenseList);

When I try to write project in a file, I get NullPointerException using the following line:
writer.write(project, Path.Combine(filePath, outputFile));

Note that I am using .net with IKVM.

When I checked Call Stack, I found out that this exception were thrown from mpxj package. Mainly from this file: > mpxj.dll!net.sf.mpxj.primavera.PrimaveraXERFileWriter.writeExpenseItems()

Am I doing something wrong?

Hi @iMohannad thanks for opening the issue. I think the problem stems from not having a Unique ID value set for the expense item. Ideally MPXJ should do this for you (I can look at adding this functionality), but in the meantime if you supply a value I think that will get over the null pointer exception.

Also, the getExpenseItems method should always return a list, so you should just be able to say currentTask.getExpenseItems().add(e).

Thank you for getting back to me. Yes that solved the issue. I appreciate your fast response

A quick update - I've switched the ExpenseItem class to use the builder pattern. The builder will ensure that the Unique ID is populated if you do not supply an explicit value.