Reputation:
I am importing records to my Solr database, and am using a .bsh script to pre-process the URL field. The script works fine except for the cases where the record does not have a URL field, and then it throws the following error:
Error while trying to evaluate script: getURLs.bsh
java.lang.IllegalArgumentException: Error while trying to evaluate script: getURLs.bsh
at org.solrmarc.index.SolrIndexer.handleScript(SolrIndexer.java:1170)
at org.solrmarc.index.SolrIndexer.addFieldValueToMap(SolrIndexer.java:914)
at org.solrmarc.index.SolrIndexer.map(SolrIndexer.java:821)
at org.solrmarc.marc.MarcImporter.addToIndex(MarcImporter.java:398)
at org.solrmarc.marc.MarcImporter.importRecords(MarcImporter.java:312)
at org.solrmarc.marc.MarcImporter.handleAll(MarcImporter.java:605)
at org.solrmarc.marc.MarcImporter.main(MarcImporter.java:865)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.simontuffs.onejar.Boot.run(Boot.java:334)
at com.simontuffs.onejar.Boot.main(Boot.java:170)
Caused by: Incorrect type returned from method: getURLDOICan't assign void value to java.lang.String: Can't assign void value to java.lang.String: <at unknown location>
at bsh.UtilEvalError.toEvalError(Unknown Source)
at bsh.BshMethod.invokeImpl(Unknown Source)
at bsh.BshMethod.invoke(Unknown Source)
at bsh.BshMethod.invoke(Unknown Source)
at org.solrmarc.index.SolrIndexer.handleScript(SolrIndexer.java:1162)
... 12 more
My script is as follows:
import org.marc4j.marc.Record;
public String getURLDOI(Record record) {
String url = indexer.getFirstFieldVal(record,"856u");
if (url != null) {
if (url.substring(0,7).equals("urn:doi")) {
String result = "http://hdl.handle.net/"+url.substring(9);
return result;
} else {
String result = url;
return result;
}
return null;
}
}
How would I handle those records that don't have the URL field ("856u"
)? I've tried setting result = null
at the start of the method, and then returning the result (rather than return null
) at the end, but I still get the same error. Thank you for any tips or pointers you can provide.
Upvotes: 0
Views: 1694
Reputation: 22553
If the function has a string as the return type, you must return one or throw an exception. As the other post says, you can return an empty string or some other special value and then have the calling code deal with it.
Your other problem is that the function you provided won't compile. I rewrote it to get rid of the dependency and I get:
public static String getURLDOI(String record) {
String url = record;
if (url != null) {
if (url.substring(0,7).equals("urn:doi")) {
String result = "http://hdl.handle.net/"+url.substring(9);
return result;
} else {
String result = url;
return result;
}
return "";
}
}
And these errors when I try to compile:
Main.java:18: error: unreachable statement
return "";
^
Main.java:20: error: missing return statement
}
^
Following what I think is your intention, I would rewrite your function like this:
public static String getURLDOI(Record record) {
String url = indexer.getFirstFieldVal(record,"856u");
if (url == null)
return "";
if (url.substring(0,7).equals("urn:doi"))
return "http://hdl.handle.net/"+url.substring(9);
return url;
}
It reads much nicer than with the nested if statements, which you made a mistake with! It's easy to do, that's why you shouldn't nest them.
Upvotes: 1