Girish
Girish

Reputation: 1717

Debugging using console Input

How one would be able to debug the Java program using ecllipse IDE which takes the input using scanner. I have search this on google but doesn't find any appropriate solution. The problem is that I was stuck into an null pointer exception while reading the input , so I want to debug my program.

This is my program...

package p;

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

           int T = in.nextInt();
           int[][] ar = new int[T][];
           for(int i=0;i<T;i++){
               int n;
               {
                    n = in.nextInt();
               }
           for(int j=0;j<n;j++)
               {
                ar[i][j]=in.nextInt(); /*null pointer exception occurs here*/
               }
           }


        for(int i=0;i<T;i++)
        {   int count=0,i1,k;
            for(int j=1;j<ar[i].length;j++)
            {
                k=ar[i][j];
                for(i1=j-1; i1>=0 && k<ar[i][i1]; i--)
                    ar[i][i1+1]=ar[i][i1];
                ar[i][i1+1]=k;
                count++; 
            }
        System.out.println(count);

        }
    }
}

Upvotes: 0

Views: 1067

Answers (4)

exexzian
exexzian

Reputation: 7890

You never say(by initializing) how many cols will ar[i][j] will have (so accessing those uninitialized memory block surely gives NullPointerException
do this for all i rows
ar[i] = new int[colSize]

check this link as well

Upvotes: 1

Ahmet Karakaya
Ahmet Karakaya

Reputation: 10139

Just put a break point where you want, then run it with Debug .It does not matter programme step contains Scanner or not. Even if you come across scanner statemenet, you just type a line on concole input, then enter. Programme will continue.

Upvotes: 0

Michael S
Michael S

Reputation: 1865

Check the javadoc for Scanner and use the hasNext() methods to make sure it has the input you expect

Upvotes: 0

user1231232141214124
user1231232141214124

Reputation: 1349

Check out a tutorial on debugging using eclipse.

You need to enter debugging mode after setting some break points. Break points are spots in your code you want the debugger to stop so you can view what's currently stored in various variables, etc.

Upvotes: 1

Related Questions