• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Sorting error

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
import java.io.*;
class TestString
{
public static void main(String args[])
throws IOException
{
String str[]= new String[10];
//int array[]=new int[10];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<5;i++)
str[i]=br.readLine();
for (int i=0; i<str.length-1; i++) {
for (int j=i+1; j<str.length; j++) {
if (str[i].compareTo(str[j]) > 0) {
String temp=str[j]; str[j]=str[i]; str[i]=temp;
}
}
}
}
}
Program complies but following Run Time errors occors
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(String.java:1004)
at TestString.main(TestString.java:17)
--------------------------------------------------
please help me
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change this
String str[]= new String[10];

to this
String str[]= new String[5];

or change this
for(int i=0;i<5;i++) str[i]=br.readLine();

to this
for(int i=0;i<10;i++) str[i]=br.readLine();
reply
    Bookmark Topic Watch Topic
  • New Topic