Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Attach files to a work item programmatically: null pointer

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

0 votes



3 answers

Permanent link
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

0 votes


Permanent link
You can currently only create attachments from file URLs, but not
arbitrary URLs.

--
Regards,
Patrick
Jazz Work Item Team

0 votes


Permanent link
Hi,

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

Lin

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,951

Question asked: Mar 17 '10, 4:56 p.m.

Question was seen: 7,136 times

Last updated: Mar 17 '10, 4:56 p.m.

Confirmation Cancel Confirm