Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    Sorry, I hadn't posted code before I started trying to learn Java and at the risk of sounding like an idiot I didn't realise what code tags were. Sorry for wasting time and space. Here is the properly formatted method including debugging code and the messages I get after trying to run it.



    XML Code:
        private static void HorseSort() throws ParseException, FileNotFoundException, UnsupportedEncodingException 
         {
            
            
            String DataPath = "D:/Work/java/Mine/Ratings/Data Files";
            int Index = 0;
            
            
            String strHrsNames [];
            strHrsNames = new String [240];       
            Scanner diskScanner = new Scanner(new File(DataPath + "/Index Files/Test.txt"));
            File file = new File(DataPath + "/Index Files/Test.txt");
            System.out.println("Exist is " + file.exists());
            System.out.println("Length is " + file.length());
            System.out.println("Absolute path is " + file.getAbsolutePath());
            
            while (diskScanner.hasNextLine())  {
           
                System.out.println("First Name is : " + strHrsNames[0]);
                strHrsNames[Index]= diskScanner.nextLine();
                System.out.println("First Name is : " + strHrsNames[0]);
                Index++;
               }
            
            System.out.println("First Name is : " + strHrsNames[0]);
            System.out.println("Second Name is : " + strHrsNames[1]);
            System.out.println("Index is : " + Index);
           
            
            
            
            Arrays.sort(strHrsNames);
            System.out.println("First Name is : " + strHrsNames[0]);
            System.out.println("Second Name is : " + strHrsNames[1]);
            
            
    
        }

    Exist is true
    Length is 312
    Absolute path is D:\Work\java\Mine\Ratings\Data Files\Index Files\Test.txt
    First Name is : null
    Second Name is : null
    Index is : 0
    Exception in thread "main" java.lang.NullPointerException
    at java.util.ComparableTimSort.countRunAndMakeAscendi ng(ComparableTimSort.java:290)
    at java.util.ComparableTimSort.sort(ComparableTimSort .java:171)
    at java.util.ComparableTimSort.sort(ComparableTimSort .java:146)
    at java.util.Arrays.sort(Arrays.java:472)
    at horseratings.HorseRatings.HorseSort(HorseRatings.j ava:100)
    at horseratings.HorseRatings.main(HorseRatings.java:6 5)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

  2. #22
    peter_62au is offline Member
    Join Date
    Dec 2013
    Posts
    18
    Rep Power
    0

    Default Re: Reading a line from a file

    Ok, I've worked out what is wrong although I don't understand it.
    The problem is the file I am trying to read, it looks like a normal text file however when, having first made a copy, I delete the contents and re- type it with exactly the same data it runs fine. If i use the copy it doesn't, but whith both open in an editor they look identical.
    Thanks everyone for persevering I'll know next time to fiddle with the data file first if I have problems.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. reading particular line from textfile file
    By karrydhawan in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 12:22 PM
  2. Reading Next Line of File
    By smitsky in forum New To Java
    Replies: 5
    Last Post: 12-04-2011, 05:35 PM
  3. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  4. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 02:57 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •