Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Pointer Exception on first element add to my Generic ChunkList
    primarykey
    data
    text
    <p>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. </p> <p>"myChunk.chunk_.add(element);////////////error" is where I am getting the error. </p> <pre><code>package ChunkList; import java.util.*; import java.io.*; public class chunkList&lt;T&gt; { public static void main(String[] args) { chunkList&lt;Integer&gt; myList=new chunkList&lt;Integer&gt;(); for(int i=1; i&lt;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 &lt;T&gt; { //T[] chunk_arr = new T[array_size];// illegal operation //ArrayList&lt;T&gt; chunk_ = new ArrayList&lt;T&gt;(array_size); ArrayList&lt;T&gt; 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&lt;T&gt;(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&lt;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&lt;list_size; ) { for(int j=0; j&lt;my_chunk.chunk_size; j++) { System.out.println(my_chunk.chunk_.get(j)); i++; } if(my_chunk.nextChunk!=null) my_chunk=my_chunk.nextChunk; } } } </code></pre> <p>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> <p>P.S. The add method on this was incorrectly linked together in the last else statement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload