diff --git a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java index 7f2c56e6653..95006d82352 100644 --- a/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java +++ b/src/main/java/org/jabref/logic/bibtexkeypattern/BibtexKeyPatternUtil.java @@ -524,8 +524,13 @@ public static String makeLabel(BibEntry entry, String value, Character keywordDe * form "pureauth..." which does not do this fallback * substitution of editor. */ - String authString = entry.getField(FieldName.AUTHOR) - .map(authorString -> normalize(database.resolveForStrings(authorString))).orElse(""); + String authString; + if (database != null) { + authString = entry.getField(FieldName.AUTHOR) + .map(authorString -> normalize(database.resolveForStrings(authorString))).orElse(""); + } else { + authString = entry.getField(FieldName.AUTHOR).orElse(""); + } if (val.startsWith("pure")) { // remove the "pure" prefix so the remaining @@ -534,8 +539,12 @@ public static String makeLabel(BibEntry entry, String value, Character keywordDe } if (authString.isEmpty()) { - authString = entry.getField(FieldName.EDITOR) + if (database != null) { + authString = entry.getField(FieldName.EDITOR) .map(authorString -> normalize(database.resolveForStrings(authorString))).orElse(""); + } else { + authString = entry.getField(FieldName.EDITOR).orElse(""); + } } // Gather all author-related checks, so we don't diff --git a/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithoutDatabaseTest.java b/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithoutDatabaseTest.java new file mode 100644 index 00000000000..b56e02e4623 --- /dev/null +++ b/src/test/java/org/jabref/logic/bibtexkeypattern/MakeLabelWithoutDatabaseTest.java @@ -0,0 +1,41 @@ +package org.jabref.logic.bibtexkeypattern; + +import org.jabref.model.entry.BibEntry; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MakeLabelWithoutDatabaseTest { + + private BibEntry entry; + + @Before + public void setUp() { + entry = new BibEntry(); + entry.setField("author", "John Doe"); + entry.setField("year", "2016"); + entry.setField("title", "An awesome paper on JabRef"); + } + + @Test + public void makeLabelForFileSearch() { + String label = + BibtexKeyPatternUtil.makeLabel(entry, "auth", ',', null); + assertEquals("Doe", label); + } + + @Test + public void makeEditorLabelForFileSearch() { + BibEntry localEntry = new BibEntry(); + localEntry.setField("editor", "John Doe"); + localEntry.setField("year", "2016"); + localEntry.setField("title", "An awesome paper on JabRef"); + + String label = + BibtexKeyPatternUtil.makeLabel(localEntry, "auth", ',', null); + assertEquals("Doe", label); + } + +}