0

whenever I try to run my program it is saying java.lang.NullPointerException at largeNo.main(largeNo.java:15) which is the array push function. How do I fix this?

    import java.util.*;
    class chutes{
    public Stack<Integer> chute  = new Stack<Integer>();
    }


    class largeNo{
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int m = s.nextInt();
    int n = s.nextInt();
    int largest =0;
    String result = new String();
    chutes[] c = new chutes[m];
    for (int i= 0; i<m; i++ ) {
        for (int j =0; j<n; j++ ) {
                c[i].chute.push(s.nextInt());
        }
    }

    while(largest>=0){
        largest = -1;
        for (int i= 0;i<m ;i++ ) {
            if(largest<c[i].chute.peek()){
                largest  = c[i].chute.peek();
            }
        }
        for (int i= 0;i<m ;i++ ) {
            if(largest==c[i].chute.peek()){
                result += Integer.toString(c[i].chute.pop());
            }
        }
    }
    System.out.println(result);



}

}

akshay parkar
  • 85
  • 1
  • 12
  • 1
    Initializing an array does not initialize its content. You need to do that explicitely with `c[i] = new chutes()`. – Ben Jul 06 '18 at 11:41
  • 1
    Also please capitalize class names as by convention. – Ben Jul 06 '18 at 11:41

1 Answers1

0

The i position of your c array is empty (null). So NullPointerException is thrown when you try null.chute (c[i].chute).

Paco Abato
  • 3,920
  • 4
  • 31
  • 54