ritesh Mehandiratta
ritesh Mehandiratta

Reputation: 97

Why does this code throw an exception?

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.

Upvotes: 0

Views: 111

Answers (5)

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

Since you'r getting NullPointerException here,

 con[count].concat(k+"\n");

it means that the value of con[count] is null and you are trying to call .concat( ) on the null instance.

Here, con[] is not initialized, so it takes null by default. You need to initialize the elements of con[] array i.e say to "" and then try calling the concat method.

Upvotes: 1

When you do

String[] con=new String[test];

you create a new String-array of length test. The elements in this array, however, start out being null. Therefore, you cannot call concat on them before initializing them to a string.

This means, you should initialize the string to the empty string, "", before doing calling concat on it.

In addition, strings are immutable, so concat produces a new String, rather than modifying the existing one, so you need to save the result.

This means, you all in all want something like:

while(test>0) {
    i=scan.nextInt();
    j=scan.nextInt();
    con[count] = ""; // Initialize con[count]


    for(int k=i;k<=j;k++) {
        if(prime(k)) {
            con[count] = con[count].concat(k+"\n");
        }
    }

    test--;
    count++;
}

Upvotes: 1

Z&#233;ychin
Z&#233;ychin

Reputation: 4205

It looks like conc[count] is null, if that's where you're getting a NullPointerException. You should initialize it with a value.

Upvotes: 0

antlersoft
antlersoft

Reputation: 14786

You are not initializing each element of con[]

new String[] gives you an array of null string references. You have to set them to the empty string if you want your code to work.

Upvotes: 1

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37940

String[] con=new String[test]; creates a new String array with test elements, and assigns it to con. However, this does not create any String objects at all - you have simply created an array where all elements are null. You must ensure that con[count] actually refers to a String before calling concat() on it; you can either do this by checking if it is null and assigning "" to it before calling concat(), or you can have a separate loop that puts an empty string into each element of con.

By the way: concat() does not modify the String you call it on; it creates a new String and 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 new String, but it will assign the new String to the array element).

Upvotes: 4

Related Questions