I am writing a code which reads the following input
3
Ruby
Diamond
Sapphire
Here is my program
import java.util.Scanner;
public class GemStones {
private int numOfStones;
private String[] gemArray;
public void solve() throws Exception{
Scanner in = new Scanner(System.in);
//Reading the integer
numOfStones = Integer.parseInt(in.nextLine());
//in.nextLine();
System.out.println(numOfStones);
//reading the strings
for(int i=0;i<numOfStones;i++){
gemArray[i] = in.nextLine();
System.out.println(gemArray[i]);
}
for(int i=0;i<numOfStones;i++){
System.out.println(gemArray[i]);
}
in.close();
}
public static void main(String[] args) throws Exception {
GemStones check = new GemStones();
check.solve();
}
}
I have a problem reading the strings following it. Whenever I try to read the strings it shows me error! please help me..
The following is the error I get in the console
3
Ruby
Diamond
Sapphire3Exception in thread "main"
java.lang.NullPointerException
at com.sudarabisheck.easy.GemStones.solve(GemStones.java:23)
at com.sudarabisheck.easy.GemStones.main(GemStones.java:37)