java.lang.NullPointerException | SoloLearn: Learn to code for FREE!

+1

java.lang.NullPointerException

I have Implemented this code as shown below but i've seen this error many times: Exception in thread "main" java.lang.NullPointerException at ArrayMethods.addElement(ArrayMethods.java:24) code : // QUEUE IMPLEMENTATION public class Queue{ // INIT QUEUE ARRAY private int[] array; // GET THE CLASS FOR WORK WITH ARRAYS; ArrayMethods am = new ArrayMethods(); // ENQUEUE public int[] enqueue(int elem){ // Something goes here... array = am.addElement(array,elem); return array; } // DEQUEUE public String dequeue(){ // Something goes here... if(array.length == 0) { return "Array is empty"; } else { array=am.removeElement(array,array.length); return "Dequeue!"; } } // PRINT ALL ELEMENTS public int[] elements(){ if(array.length == 0){ return array; } else { return array; } } }

5/13/2020 4:44:26 AM

Mouad Taoussi

1 Answer

New Answer

+1

Here is the ArrayMethods class: // ARRAY FUNCTIONS public class ArrayMethods /*implements ArrayMethods*/ { public static int[] addElement(int[] array, int elem){ // Something goes here... // Init output array int[] arr = new int[array.length +1]; // Copy the elements of the array // to an output array and the extra one for(int i = 0,k = 0; i < array.length; i++){ arr[k++] = array[i]; // Check if array filled for add new element if (arr.length-1 == array.length){ arr[k] = elem; } else{ continue; } } // Retrun to the output return arr; } }