It's all about the answers!

Ask a question

Attach files to a work item programmatically: null pointer


Lin Ye (30632611) | asked Mar 17 '10, 4:56 p.m.
Hi,

I am trying to attach files to a work item programmatically. Here is the code

ItemProfile<IWorkItem> profile = IWorkItem.FULL_PROFILE;
IWorkItemWorkingCopyManager workingCopyManager = workItemClient.getWorkItemWorkingCopyManager();
workingCopyManager.connect(handle, profile, null);
WorkItemWorkingCopy workingCopy = workingCopyManager.getWorkingCopy(handle);
IWorkItemReferences references = workingCopy.getReferences();

URI uri = new URI("http", null, "jira-uat", -1, "/secure/attachment/10029/DupeFinder.java", null, null);
System.out.println(uri.toASCIIString());
IReference reference= IReferenceFactory.INSTANCE.createReferenceFromURI(uri, "DupeFinder.java");
if (reference != null) {
references.add(WorkItemEndPoints.ATTACHMENT, reference);
}
try{

workingCopyManager.save(new WorkItemWorkingCopy[]{workingCopy}, monitor);
}
catch (Exception e) {
e.printStackTrace();
}


Then I got the following exceptions (in the catch clause)

java.lang.NullPointerException
at com.ibm.team.workitem.client.internal.ArtifactLink.createUpdatedAttachment(ArtifactLink.java:269)
at com.ibm.team.workitem.client.internal.WorkItemWorkingCopyRegistry.saveWorkItems(WorkItemWorkingCopyRegistry.java:1745)
at com.ibm.team.workitem.client.internal.WorkItemWorkingCopyRegistry.saveAffected(WorkItemWorkingCopyRegistry.java:1667)
at com.ibm.team.workitem.client.internal.WorkItemWorkingCopyRegistry.save(WorkItemWorkingCopyRegistry.java:1568)
at com.ibm.team.workitem.client.internal.WorkItemWorkingCopyRegistry.save(WorkItemWorkingCopyRegistry.java:1539)
at com.ibm.team.workitem.client.internal.WorkItemWorkingCopyManager.save(WorkItemWorkingCopyManager.java:115)
at com.ibm.team.workitem.ide.ui.example.CreateWorkItemsInBg$1.runProtected(CreateWorkItemsInBg.java:186)
at com.ibm.team.foundation.client.util.FoundationJob.run(FoundationJob.java:68)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

Within the Eclipse client instance, I would see a "unnamed" attachment. But save operation cannot be finished since the exception caught.

Any thing wrong with the code? Or how can I debug the source code? Thanks in advance.

Lin

3 answers



permanent link
Lin Ye (30632611) | answered Mar 18 '10, 8:58 a.m.
Hi,

Thank you very much for your guys' reply. Yes it works fine after switching to file uri. Thanks.

Lin

permanent link
Patrick Streule (4.9k21) | answered Mar 18 '10, 6:01 a.m.
JAZZ DEVELOPER
You can currently only create attachments from file URLs, but not
arbitrary URLs.

--
Regards,
Patrick
Jazz Work Item Team

permanent link
Stefan Hufnagl (29411920) | answered Mar 18 '10, 5:50 a.m.
Hi Lin,

I changed your code a little bit...and it work. I'm able to attach files to a Workitem.

File file = new File("C:/tmp/sample.xml");

IReference reference= IReferenceFactory.INSTANCE.createReferenceFromURI(file.toURI(), file.getName());

if (reference != null) {
references.add(WorkItemEndPoints.ATTACHMENT, reference);
}
try{

workingCopyManager.save(new WorkItemWorkingCopy[]{workingCopy}, null);
}
catch (Exception e) {
e.printStackTrace();
}

Let me know if it works for you

Stef

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.