CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Simple scanner problem

    I'm using this method to read records from a file in my root project directory, storing them into an array of objects.

    example of a line of text from the file:
    Texas Austin TX19759614Southwest 5

    this is the method i'm using:
    Code:
        public void readStates() {
    
            File file = new File("States.Fall2009.txt");
    
            try {
    
                Scanner console = new Scanner(file);
                Scanner lineTokenizer;
                int lineNum = 0;
    
                while (console.hasNextLine()) {
                    lineTokenizer = new Scanner(console.nextLine());
                    lineNum++;
    
                    if (lineTokenizer.hasNext())
                        a[lineNum].name = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State field is invalid", lineNum);
                        continue;
                    }
    
    etc..etc..etc...for each field
    Can anyone help me out real quick?

  2. #2
    Join Date
    Aug 2009
    Posts
    44

    Exclamation Re: Simple scanner problem

    you forgot to post youre problem

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Re: Simple scanner problem

    Quote Originally Posted by socialite View Post
    you forgot to post youre problem
    Code:
    package project1;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    /**
     *
     * @author Ryan
     */
    
    class State {
    
        public String name;
        public String capital;
        public String symbol;
        public int number;
        public String location;
        public int locationNumber;
    
        public State(String nm, String cap, String sym, int num,
                String loc, int locNum) {
            name = nm;
            capital = cap;
            symbol = sym;
            number = num;
            location = loc;
            locationNumber = locNum;
        }
    
    }
    
    class ClassDataArray {
        private State[] a;
        private int nElems;
        
        public ClassDataArray(int max) {
            a = new State[max];
            nElems = 0;
        }
    
        public void print() {
            int i = 0;
            a = new State[50];
    
            for (i=0; i<a.length; i++)
                System.out.printf("%15s%15s%15s%3s%8d%16s%2d", a[i].name, a[i].capital, a[i].symbol, a[i].number, a[i].location, a[i].locationNumber);
        }
    
        public void readStates() {
    
            File file = new File("States.Fall2009.txt");
    
            try {
    
                Scanner console = new Scanner(file);
                Scanner lineTokenizer;
                int lineNum = 0;
    
                while (console.hasNextLine()) {
                    lineTokenizer = new Scanner(console.nextLine());
                    lineNum++;
    
                    if (lineTokenizer.hasNext())
                        a[lineNum].name = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State field is invalid", lineNum);
                        continue;
                    }
    
                    if (lineTokenizer.hasNext())
                        a[lineNum].capital = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: Capital field is invalid", lineNum);
                        continue;
                    }
    
                    if (lineTokenizer.hasNext())
                        a[lineNum].symbol = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State symbol field is invalid", lineNum);
                        continue;
                    }
    
                    if (lineTokenizer.hasNextInt())
                        a[lineNum].number = lineTokenizer.nextInt();
                    else {
                        System.out.printf("line %d: State number field is invalid", lineNum);
                        continue;
                    }
    
                    if (lineTokenizer.hasNext())
                        a[lineNum].location = lineTokenizer.next();
                    else {
                        System.out.printf("line %d: State location field is invalid", lineNum);
                        continue;
                    }
    
                    if (lineTokenizer.hasNextInt())
                        a[lineNum].locationNumber = lineTokenizer.nextInt();
                    else {
                        System.out.printf("line %d: Location number field is invalid", lineNum);
                        continue;
                    }
                }
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             }
    
        }
    }
    
    public class Main {
    
        public static void main(String[] args) {
            ClassDataArray arr;
            arr = new ClassDataArray(50);
    
            arr.readStates();
            arr.print();
        }
    }
    Message:
    run:
    Exception in thread "main" java.lang.NullPointerException
    at project1.ClassDataArray.readStates(Main.java:65)
    at project1.Main.main(Main.java:119)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)


    This is all I know.

  4. #4
    Join Date
    Sep 2009
    Posts
    3

    Re: Simple scanner problem

    line 65 is
    Code:
    a[lineNum].name = lineTokenizer.next();
    line 119 is
    Code:
    arr.readStates();
    forgot to mention that

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Simple scanner problem

    You've created an array to hold 50 States, but you haven't actually put any State objects into it, so every array element is null, which means you can't access any State member variables, such as name.

    You need to create 50 State objects and put them into the array. You can either do this all at once before processing the file, or you can create each one as you read a new line from the file. I would suggest the latter, as you need to assemble all the data needed for the State constructor before you can create one.

    Ideally, you would use an ArrayList object to hold the States instead of an array, because a list is is easier to use, and dynamic rather than fixed size, so you can put as many objects as you like into it. This could save problems if someone tries to put 52 States into your file...

    Vague and nebulous is the beginning of all things, but not their end...
    K. Gibran
    Last edited by dlorde; September 21st, 2009 at 05:13 AM.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured