简体   繁体   中英

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.

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).

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 ie say to "" and then try calling the concat method.

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.

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++;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM