Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:28:22+00:00 2026-06-01T04:28:22+00:00

I just search through the forum, but haven’t found a similar problem. My problem

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:28:23+00:00Added an answer on June 1, 2026 at 4:28 am

    There’s no indication that you’ve ever assigned a non-null value to this.authors. At some point you’ll want something like:

    this.authors = new ArrayList<String>();
    

    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:

    this.authors.add("Foo");
    this.authors.add("Bar");
    

    and you’d get the same result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have search through google and found that a lot of people is struggling
Is there a way in Ant to just use regex to search through a
I added a function in my .vimrc that's just few search and replace commands.
I'm sure this is just a google search away, but I can't find the
I was just wondering if anyone has come across anything offering similar search functionality
I've made some search on the forum without any good answers for my problem.
I've been trying to figure out a command that will search through 13+ files
I've done a lot of Googling, but haven't found anything, so I'm really sorry
I'm just getting started using Mustache and I have this rendering problem. I send
I am using a Google map inside of vb.net application. The problem is that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.