Re: Simple scanner problem
I'd say it's cause a hasn't been declared. Other than that, I can't really see anything else wrong (I'm assuming you're closing the curly brackets at the bottom somewhere, and have the appropriate catch block).
Re: Simple scanner problem
i feel like i already have, this time i'll include all of what i have so far, here it is:
Code :
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();
}
}
thank you for your help
Re: Simple scanner problem
"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)
"
that is my error code and line 65 is
Code :
a[lineNum].name = lineTokenizer.next();
and line 119 is
Re: Simple scanner problem
Ahh, i see your problem. You initialized an array of states, but the array contains all nulls. Change that line of code to this (line 65):
Code :
a[lineNum].name = new State();
a[lineNum].name = lineTokenizer.next();
I also would add an empty constructor to the State class.
Another thing I noticed is that you advanced lineNum before putting stuff into the state. This will give you an error if you try reading in max # of states.