1
Reply

java.nulpointerException

Ask a question
the folloeing program manipulates the median of an array but is retutning a NullPointerException.i have tried but didnt fix it.plz any1 to sortout the bug.
thanks in advance
import java.io.*;

public class media {

@SuppressWarnings("null")
public static void main(String[] args) {
int i=0,j=0,n=0;
     float median;
float t; 
    int []a=null;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));                                                      
      System.out.println("Enter the number of items\n");                  
       try {
n=Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       System.out.println("Input values \n");  
      for( i=1;i<n;i++)
try {
a[i]=Integer.parseInt(br.readLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
      
   /* Reading items in array a  */
 
                   
   /* Sorting begins */
for (i = 1 ; i <= n-1 ; i++)                            
      {     /* Trip-i begins  */
for (j = 1 ; j <= n-i ; j++)                         
         {                                                    
             if (a[j] <= a[j+1])                              
             { /* Interchanging values */
                    
               t = a[j];                                      
               a[j] = a[j+1];                                 
               a[j+1] = (int) t;                                    
             }                                                
             else continue ;                                     
         }                                                    
      } /* sorting ends */
/* calculation of median  */
if ( n % 2 == 0)                                        
         median = (float) ((a[n/2] + a[n/2+1])/2.0) ;                   
      else                                                    
         median = a[n/2 + 1];                                 
                                                              
   /*  Printing */
for (i = 1 ; i <= n ; i++)  
{
         System.out.println(a[i]); 
}
                      
                                                              
     
System.out.println("\n\nMedian is " +median);  
}

}


Answers (1)