import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
int i,j;
int count = 0;
int test=scan.nextInt();
String[] con=new String[test];
while(test>0)
{i=scan.nextInt();
j=scan.nextInt();
for(int k=i;k<=j;k++)
if(prime(k))
//***********the line below where i am getting nullpointer exception
con[count].concat(k+"\n");
test--;count++;}
for( i=0;i<con.length;i++)
System.out.printf("%s\n",con[i]);
}
private static boolean prime(int k) {
// TODO Auto-generated method stub
if(k==2)
return true;
if(k%2==0)
return false;
for(int l=3;l<=Math.sqrt(k);l=l+2)
if(k%l==0)
return false;
return true;
}
}
please somebody help me that how to get rid from this exception.
String[] con=new String[test];creates a newStringarray withtestelements, and assigns it tocon. However, this does not create anyStringobjects at all – you have simply created an array where all elements arenull. You must ensure thatcon[count]actually refers to aStringbefore callingconcat()on it; you can either do this by checking if it isnulland assigning""to it before callingconcat(), or you can have a separate loop that puts an empty string into each element ofcon.By the way:
concat()does not modify theStringyou call it on; it creates a newStringand returns it, but you don’t do anything with the return value, so it gets thrown away. You should use+=instead (which also creates a newString, but it will assign the newStringto the array element).