Why am I getting Error at run time when b.initialize()
from main is called?
The problem is in the initialize()
code.
I am getting NullPointerException
when giving input for second line.
Exception in thread "main" java.lang.NullPointerException
at Bubble.initialize(Bubble.java:17)
at Bubble.main(Bubble.java:46)
import java.io.*;
public class Bubble {
private int size;
private int[] arr;
public Bubble(int N)
{
size = N;
}
public void initialize()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<size; i++)
{
try {
this.arr[i]=Integer.parseInt(br.readLine());
}
catch(IOException ioe){}
}
}
public int[] sort()
{
int max=size-1;
int temp=0;
while(max==0)
{
for(int i=0;i<max;i++)
{
if(arr[i]>arr[i+1])
{
temp = arr[i+1];
arr[i+1]=arr[i];
arr[i]=temp;
}
}
max--;
}
return arr;
}
public static void main(String[] args)
{
Bubble b = new Bubble(5);
b.initialize();
int res[] = b.sort();
for( int el : res )
System.out.println(el);
}
}
Thanks in advance ! Abhishek