0

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

        }
    }
}
1
  • Do you mean, Debug this java program which gets input from a scanner, using eclipse IDE? Commented Sep 15, 2013 at 18:32

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

@downvoter care to comment what was wrong may be I missed something
yes its working now, previously I was using unintialized or unallocated memory that's why i was getting the Null Pointer exception but stil working on to decrease time complexity.
@girishlalwani2010 If it did solved your NPException then you can mark this answer as approved - that would be beneficial for future reference
1

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.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.