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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:27:32+00:00 2026-05-31T10:27:32+00:00

So I have a null pointer exception when run. I am supposed to create

  • 0

So I have a null pointer exception when run. I am supposed to create a generic class that implements a list with chunks of arrays added as needed. Each time I add an element it is to check if there is space in the tail chunk array and if so add the element. Else it needs to add a chunk, adjust the pointers and add the element. My problem so far is that when I go to add the first element it is throwing a null pointer exception. I believe I have instantiated and object and assigned it where needed. If anyone has any insight please feel free to let me know what I am doing wrong or maybe its right in front of my face.

“myChunk.chunk_.add(element);////////////error” is where I am getting the error.

package ChunkList;


import java.util.*;
import java.io.*;

public class chunkList<T> {

    public static void main(String[] args) {

        chunkList<Integer> myList=new chunkList<Integer>();

        for(int i=1; i<24; i++)
        {
            myList.add(i);//////////////////////////////////
            System.out.println("Adding number: "+ i);
        }
        System.out.println("");

        myList.display();


    }


    private chunk head;//changed T to chunk
    private chunk tail;//changed T to chunk
    private int array_size=8;
    private int list_size;

    public chunkList()//Default Constructor
    {
        head=null;
        tail=null;
        list_size=0;
    }

    //public chunkList(chunkList copy){}// a copy constructor.... don't think I need.

    class chunk//  added <T>
    {
        //T[] chunk_arr  = new T[array_size];// illegal operation
        //ArrayList<T> chunk_ = new ArrayList<T>(array_size);
        ArrayList<T> chunk_; 

        private int chunk_size; //may need to change to public
        chunk nextChunk;//changed T to chunk
        chunk prevChunk;//changed T to chunk

        public chunk()//default constructor
        {
            chunk_ = new ArrayList<T>(array_size);
            chunk_size=0;
            nextChunk=null;
            prevChunk=null;
        }
    }

    public void add(T element)
    {
        if(this.tail==null)//empty chunk list
        {
            chunk myChunk=new chunk();//instantiate

            //myChunk.prevChunk=null;//changed from head to null
            //myChunk.nextChunk=null;//changed from tail to null
            head=myChunk;
            tail=myChunk;
            //head.nextChunk=null;
            //head.prevChunk=null;

            myChunk.chunk_.add(element);////////////error
            list_size++;
            myChunk.chunk_size=1;
        }
        else if (this.tail.chunk_size<array_size)//adds the element to the last chunk in list
        {
            this.tail.chunk_.add(element);//add element

            list_size++;
            this.tail.chunk_size++;//increase individual chunk array size
        }
        else// create new chunk, relink chunks, add element
        {
                        chunk myChunk=new chunk();

        myChunk.chunk_size=1;   
        list_size++;
        myChunk.chunk_.add(element);

        tail.nextChunk=myChunk;
        myChunk.prevChunk=tail;
        tail=myChunk;
        }}

     public int size()
         {return list_size;}

     public void display()
     {
         chunk my_chunk=head;

         if(my_chunk==null)
         {
             System.out.print("Empty Chunk List");
             return;
         }

         for(int i=0;i<list_size;  )
         {
             for(int j=0; j<my_chunk.chunk_size;  j++)
             {
                 System.out.println(my_chunk.chunk_.get(j));
                 i++;
             }
             if(my_chunk.nextChunk!=null)
             my_chunk=my_chunk.nextChunk;
         }
     }


}

So thanks Olivier Jacot-Descombes , I fixed one problem with the code and it now adds the first chunk BUT it is throwing NPE when it tries to create the next chunk. I will look at it and be back if i need more help. Thanks All.

P.S. The add method on this was incorrectly linked together in the last else statement.

  • 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-05-31T10:27:33+00:00Added an answer on May 31, 2026 at 10:27 am

    Your code is very strange

    • There is a public static void main(String[] args) inside the class chunkList<T>. This makes no sense.

    • You declare a chunkList<Integer> instead of chunkList<int>.

    • You re-declare a chunk<T> head and chunk<T> tail in the constructor. The code should simply be head = null; without chunk<T>.

    • In the constructor of chunk you do the same thing again with ArrayList<T> chunk_ = ....

    I could tell more things; however, I think that you should start by fixing these things to begin with.

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

Sidebar

Related Questions

I have a byte array that may or may not have null bytes at
I'm trying to count the number of records that have a null DateTime value.
i am trying to execute the records that have TotalTime null value from the
I have a bunch of tests that assume that my Tetris class is composed
I am currently trying to make an existing VB.NET Project run. A null pointer
I have code written for Android 2.2 that is supposed to parse xml from
I have an Activity that pulls an object from an Application extended class (application
I have a method that looks like this: try { doStuff(); } catch (Exception
I have an array list in my class and am getting the value for
My program crashes due to a null pointer exception. Snip of Stack Trace: java.lang.NullPointerException

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.