0

For instance, my input is
2 // this is variable T
5 2 5 // these are the 3 space separated parameters
1 2 4 5 5 // this is my array
2 3 4 // these are my 2nd set of parameters
2 9 // and my 2nd set of array

    package hills;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;


    public class hillJump {

Just the function associated with my program

static void fun(int N, int U, int D, int a[]){
    int jump=0,i,para = 1;
    for(i=0;i<4;i++){
        if((a[i+1]-a[i])==0 || ((a[i+1]-a[i])<=U && (a[i+1]-a[i])>0) || (a[i+1]-a[i])>=D &&(a[i+1]-a[i])<0 ){
            jump++;
        }
        else if(-1*(a[i+1]-a[i])>D && para==1){
            para = 0;
            jump++;
        }
        else{
            break;
        }
    }
    System.out.println(jump);
}
public static void main(String[] args)throws Exception {
    // TODO Auto-generated method stub

I am trying to read the input in the following way,

    InputStreamReader r = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(r);

    int T = Integer.parseInt(br.readLine());
    int arr[][] = new int[T][];
    int p[][] = new int[T][]; // N U D 
    String s,hills;
    String str[][] = new String[T][];
    String str2[][] = new String[T][];

    //reading from keyboard

        for(int i=0;i<T;i++){
            s = br.readLine();
            str[i] = s.trim().split("\\s+");
            hills = br.readLine();
            str2[i] = hills.trim().split("\\s+");
        }

after taking the inputs its showing
**********

Exception in thread "main" java.lang.NullPointerException
at hills.hillJump.main(hillJump.java:50)

**********

    //arranging data

    for(int i=0;i<T;i++){
        for(int j = 0;j<3;i++){
            p[i][j] = Integer.parseInt(str[i][j]); // LINE 50 with error
        }
        for(int j=0;j<str2.length;j++){
            arr[i][j] = Integer.parseInt(str2[i][j]);
        }
    }
    //output
    for(int i=0;i<T;i++){
        fun(p[i][0], p[i][1], p[i][2], arr[i]);
    }

}

}

I am stuck and don't know what to do?

I have already gone through the following answers

What is a NullPointerException, and how do I fix it?

still, I am not able to figure out what is the problem. I am using bufferedReader to get the inputs from the same line, and I doubt whether the error is caused by readLine() function.

0 Answers0