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 8548171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:25:58+00:00 2026-06-11T13:25:58+00:00

As a practice exercise, I am creating my own generic class that is basically

  • 0

As a practice exercise, I am creating my own generic class that is basically a copy of ArrayList. While testing the class with JUnit, I come across a NullPointerException error in the add method:

public void add(int index, T element) {
    if (index > this.size() || index < 0) {
        throw new IndexOutOfBoundsException();
    }

    if (this.size() == data.length) {
        // ^ This is the line that the error points to
        resize(this.data);
    }

    for (int i = index; i < this.size; i++) {
        this.data[i + 1] = this.data[i]; //fix
    }

    this.data[index] = element;
    size++;
}

After messing around with the class a lot, I can’t figure out where the error is coming from. I can provide any details/other parts of the class that are needed. Any guidance as to where the problem is located would be fantastic. Thank You.

The constructor for the class:

MyArrayList(int startSize) {
    // round the startSize to nearest power of 2
    int pow2 = 1;
    do {
        pow2 *= 2;
    } while (pow2 < startSize);

    startSize = pow2;
    this.size = 0;
    T[] data = (T[]) new Object[startSize];
}

The following test case tests the size, but encounters the error when it tries to add an element:

public void testSize() {
    MyArrayList<Integer> test = new MyArrayList<Integer>(); 
    ArrayList<Integer> real = new ArrayList<Integer>();
    assertEquals("Size after construction", real.size(), test.size());
    test.add(0,5);
    real.add(0,5);
    assertEquals("Size after add", real.size(), test.size());
}
  • 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-11T13:25:59+00:00Added an answer on June 11, 2026 at 1:25 pm
    T[] data = (T[]) new Object[startSize];
    

    That initializes the local variable data. Which you don’t want.

    Change it to following to make sure that you initialize the instance variable –

    this.data = (T[]) new Object[startSize];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create my own templated List class as a practice excercise. I
I am studying the SCJP, and while studying I have found an exercise that
FYI. Another practice exercise that I'm stuck on. The problem is to find the
I have been working on creating a custom allocator as a fun exercise/practice and
Good practice dictates that subroutine arguments in Fortran should each have a specified intent
Right now, I prepare for an exam (university) and thought of creating my own
For practice I am trying to display a number that increments from 0 -
Best practice dictates that my website that is heavily reliant on Jquery, and JQuery
I building a simple class that is supposed to mimic the functionality of the
package practice; class Node<T> { T data; public Node(T data) { this.data = data;

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.