This question has already been answered:

When solving a problem, an error occurs:

"Exception in thread" main "java.lang.NullPointerException"

Tell me, please, what is wrong and how to fix it?

class Plural { int [] Array; int level=5; int k; public Plural () { int [] Array = new int[k]; } void funct(int k) { if (k != 0 && level > 0) { Array[k] = 0; level=level-1; funct(k - 1); Array[k] = 1; level=level-1; funct(k - 1); } else { for (int i = 0; i <5; i++) { System.out.println(Array[i] + ' '); } } } } class Inter { public static void main(String[] args) { Plural obj1=new Plural(); obj1.funct(5); } } 

Marked as a duplicate by the participants Sergey Gornostaev , Suvitruf , Community Spirit 29 Oct '17 at 20:43 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • The full text of the error is written in which line the exception occurs. - Sergey Gornostaev
  • Full error text Exception in thread "main" java.lang.NullPointerException at Plural.funct (Inter.java:15) at Inter.main (Inter.java:37) - Pretty_Duck
  • @lDrakonl assigned, does not help - Pretty_Duck
  • As written earlier, you have an error line that contains an exception. Also in your IDE there should be a debager. Now is the time to get to know him - Viktorov

2 answers 2

 class Plural { int level=5; // уровни рекурсии int k; int [] Array; public Plural (int k) { this.k = k; Array = new int[k]; } void funct() { if (k != 0 && level > 0) { Array[k-1] = 0; level=level-1; funct(k - 1); Array[k-1] = 1; level=level-1; funct(k - 1); } else { for (int number: Array) { System.out.println(number); } } } void funct(int k) { if (k != 0 && level > 0) { Array[k-1] = 0; level=level-1; funct(k - 1); Array[k-1] = 1; level=level-1; funct(k - 1); } else { for (int number: Array) { System.out.println(number); } } } } class Inter { public static void main(String[] args) { Plural obj1=new Plural(5); obj1.funct(); } } 
  • Initialize the array in the constructor, and do not create a new one.
  • It is more logical to transfer the variable k to the constructor. Overload the funct function funct that for the first time it works without taking arguments, and all subsequent recursive calls accept k-1 . Then in main you can pass k to the constructor once and call funct with no arguments.
  • You have an array of size k , its last element is k-1 , so that accessing the k -th element will cause the array to overflow. Instead, refer to the k-1 element.
  • System.out.println(Array[i] + ' ') here you add char to the number. Char is converted to a number (ASCII code), added to the element of the array and displayed. You can fix this by casting an array element to a string. Although it is not at all necessary for you to add a space to a line, the output each time comes from a new line.
  • To output an array, it is better to use a for each loop.

Here you can test the code written by me .

  • Thank you very much, everything is clear, but apparently my code itself is not written correctly because in your example the wrong numbers are displayed - Pretty_Duck
  • @Pretty_Duck I fixed this error too - pinguin
  • @ Zvyagintsev Denis Diko, I apologize for the simple mistakes I just started to learn programming, thanks for being understanding and helping - Pretty_Duck
  • @Pretty_Duck if everything works correctly, mark the answer as correct - pinguin
  • It seems that yes) it is necessary to translate for clarity in Arrays.toString for clarity - Pretty_Duck

All primitives are initialized with zeros, objects are null. Of the objects you have here only an array.

As suggested by the error most likely in the initialization of the array, since you have k = 0 when creating a Plural object.

But it is strange, because the idea is to create an array of length 0, and not to leave the field null ..


In the constructor, you do not initialize the array, but create a new one. Change the constructor to this

 public Plural (){ Array = new int[k]; } 
  • Set the value of k, the error as it was and remained - Pretty_Duck
  • @Pretty_Duck supplemented the answer - Victor
  • Thanks NullPointerException was gone but a new error appeared)) it looks like the recursion goes deep, but I set the level of the recursion to be incomprehensible to Krč) - Pretty_Duck
  • @Pretty_Duck I in my answer explained where this error came from. - pinguin
  • @Pretty_Duck It all depends on 'k', you are trying to refer to the 5th element, which is not. Add the condition if (k> = Array.length ()) and write the logic to it, what to do if you ask for an element that does not exist. - Victor