Click to See Complete Forum and Search --> : Simple scanner problem


Sinensis
September 20th, 2009, 05:09 PM
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:

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?

socialite
September 20th, 2009, 06:44 PM
you forgot to post youre problem

Sinensis
September 20th, 2009, 07:33 PM
you forgot to post youre problem


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.

Sinensis
September 20th, 2009, 07:37 PM
line 65 is a[lineNum].name = lineTokenizer.next();

line 119 is arr.readStates();

forgot to mention that

dlorde
September 21st, 2009, 04:52 AM
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... :eek:

Vague and nebulous is the beginning of all things, but not their end...
K. Gibran