Add Answers when creating new concept throws null pointer exception

Hey guys,

This may sound a little trivial but I can not seem to add concept answers when creating a new concept. I am working on a private project and I need a Concept whose datatype is a question and whose answers are investigations( concepts with concept class either labTest or Test). What I intend to do is to display these concepts as a tree in an OPD app such that a doctor would not have to stress much when choosing or prescribing an investigation for a patient. He could just collapse the tree and select the investigations he wanted.

This is what my current code looks like

This throws a Null Pointer Exception( I assume when saving the concept).Before reaching here, I had tried different methods.

  • I tried saving the concept before adding the answers, then saving again after I added the answers, But what it would do is create an empty concept with the name i specified, the datatype and everything else, but would not add the answers.
  • I added the answers one by one using con.addAnswer(conceptService.getAnswer[i]) in the for loop, and saving afterwards. This still did not work.

Here comes the funny part. I had succeeded in doing this before, just a couple of days ago, but i didnt commit and my machine crashed and I lost that code. I am pretty sure the first point on my bullet list is the method I used. Please I need some help ASAP on what I am missing.

PS please ignore the unnecessary prints, I was trying to keep track of what was happening and my debug server does not work for some reason.

hello @ryan97 sorry about the problem,can this help you in regards to what you are looking for https://wiki.openmrs.org/display/docs/Concept+Dictionary+Basics?

Not really. I figured out a quick fix to my situation, but I’m still looking to solve this problem cuz the fix i found involves creating the concept manually from legacy ui and this would not be sustainable if I have to share this code with someone else. I would have preferred I had code that would create the concept for me rather than directing some other person to go use legacy UI

@k.joseph @ssmusoke could you be knowing of a solution to this problem?

Why not use the DataImport module to add the concepts

you can always refer to existing tests like ConceptServiceTest.java to easily figure out what your code may be missing.

thank you @ssmusoke and @k.joseph for your assistance.:+1:
@ryan97 hope this can be of good help to you