I just search through the forum, but haven’t found a similar problem.
My problem is that I get a NullPointerException when using a varargs Constructor.
I just needed varargs in one of my programmes and tried this solution of my Java book, but it doesn’t work!
My code is the following:
/** The author list. */
private List<String> authors;
/**
* Instantiates a new book.
*
* @param title the book title
* @param year the year the book was written
* @param authors the authors
*/
public Book(String title, int year, String... author)
{
{...}
if (author.length < 1)
{
throw new IllegalArgumentException
("A book needs a least 1 author!");
}
for (String e : author)
{
this.authors.add(e);
}
}
I think this.authors.add(e) causes the NullPointerException, but I don’t know how to handle it.
As Iam testing it via JUnit I will just write some function calls:
(btw. all my function calls are failing)
Book b1 = new Book(“Eragon”, 1990, “Eric Cartman”, “Kenny”);
Book b2 = new Book(“Eragon”, 1990, “Eric Cartman”);
Stacktrace:
java.lang.NullPointerException
at de.imbus.training.itbbj.books.Book.<init>(Book.java:57)
at de.imbus.training.itbbj.books.BookTests.creationTestSuccessful(BookTests.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Thanks in advance 🙂
TLS
There’s no indication that you’ve ever assigned a non-null value to
this.authors. At some point you’ll want something like:Or just initialize it as part of the declaration:
private final List authors = new ArrayList();
Note that this is entirely independent of varargs. You could hard code your statements as:
and you’d get the same result.