Code gives error but runs fine in eclipse


  • 1
    N

    When i run the code in eclipse IDE, the reverse of integer is correct, but when i run it on OJ, it gives a null pointer exception at the line shown below. Why is this happening!?

    public class Revno 
    

    {
    Scanner sc = new Scanner(System.in);
    int input, output, sign=0, len=0, nos[], temp;
    double res;

    public static void main(String[] args) 
    {
    	Revno obj = new Revno();
    	obj.getip();
    	obj.output = obj.reverse(obj.input);
    	double t = obj.temp;
    	obj.res = (t*10 + obj.output)*obj.sign;
    	System.out.printf("%.0f", obj.res);
    }
    
    public void getip()
    {
    	try{
    		System.out.print("Enter No: ");
    		input = sc.nextInt();
    	}
    	catch (Exception e)
    	{
    		System.out.println("Enter nos between -4294967296 and 2147483647");
    		System.exit(0);
    	}
    	
    	if(input<0)
    		sign=-1;
    	else
    		sign=1;
    	
    	input = input * sign;
    	
    	len = findlen(input);
    	nos = new int[len];
    }
    
    public int findlen(int x)
    {
    	int l=0;
    	while(x!=0)
    	{
    		l++;
    		x = x/10;
    	}
    	return l;
    }
    
    public int reverse(int x) 
    {
    	int i=0;
    	while(x!=0)
    	{
    		nos[i++] = x%10;      // NULL POINTER EXCEPTION HERE
    		x = x/10;
    	}
    	int y=0;
    	temp=0;
    	for(int j=0; j<len; j++)
    	{
    		if (j<9)
    		{
    			temp = temp*10 + nos[j];
    		}
    		else if(j>8)
    		{
    			y = y*10 + nos[j];
    		}			
    	}
    	
    	if (len<10)
    	{
    		y=temp;
    		temp=0;
    	}
    	
    	return y;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.