0
import java.util.*;

public class test
{

public static void main(String[] args)
{
    Scanner Input = new Scanner(System.in);


    int[] guess;
    guess = new int[6];

    for(int i =0 ; i<5;i++)
    {
        guess[i] = Input.nextInt();
    }

    **int[] cypher = encryption(guess);**
    System.out.print(cypher);

}

public static int[] encryption(int[] guess)
{

    int[] cypher = null;
    int end = guess.length;
    for( int i=0 ; i< end ; i++)
    {
      **cypher[i] = guess[i] + 1;**
    }
    return cypher;

}

}

I tried to use an integer array(cypher) to hold an integer array(guess) after coming out of a function(encryption). However, this program doesn't work.The below notifications came out.

Exception in thread "main" java.lang.NullPointerException

at test.encryption(test.java:31)

at test.main(test.java:19)

Why? how can i correct it?

Thank you guys

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Ricky
  • 15
  • 1
  • 6

1 Answers1

0

Your cypher array is null, you need to initialize it.

int[] cypher = new int[guess.length];
Bon
  • 3,073
  • 5
  • 21
  • 40