TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
JAVED IQBAL
NA
9
2.7k
java.nulpointerException
Jul 10 2013 3:40 AM
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);
}
}
Reply
Answers (
1
)
Insert all the selected items from the listbox to a database
Windows store app navigation