I'm a newbie trying to learn Java & Swing and I'm attempting to use the Jtable element to display really simple car records in CSV format in my UI. The values in my file are like so:
1970,Chevy,Chevelle,16000
1972,Oldsmobile,Cutlass,15000
1969,Chevy,Camaro,22000
1971,Plymouth,Barracuda,19500
and as far as I can tell, from the documentation i've read
( the suggested way for it to work should just be to read in the records to the JTable as a two dimensional object array. But I keep getting java.lang.NullPointerException.
what am i doing wrong? the tokens seem to print out just fine, but I can't read them into the array. i tried a lot of things, spent a good deal of time with my Java book and Mr. Google, but nothing seems to be working and i'm stuck. As far as i can tell, I should be able to:
1. use buffered file reader to read the records
2. use either split() or token to break the records out by the comma delimiter
3. pack the data into a two dimensional array
4. feed that into the JTable constructor.
right? well, I'm stuck on step 3. - here's some code:
1970,Chevy,Chevelle,16000
1972,Oldsmobile,Cutlass,15000
1969,Chevy,Camaro,22000
1971,Plymouth,Barracuda,19500
and as far as I can tell, from the documentation i've read
( the suggested way for it to work should just be to read in the records to the JTable as a two dimensional object array. But I keep getting java.lang.NullPointerException.
what am i doing wrong? the tokens seem to print out just fine, but I can't read them into the array. i tried a lot of things, spent a good deal of time with my Java book and Mr. Google, but nothing seems to be working and i'm stuck. As far as i can tell, I should be able to:
1. use buffered file reader to read the records
2. use either split() or token to break the records out by the comma delimiter
3. pack the data into a two dimensional array
4. feed that into the JTable constructor.
right? well, I'm stuck on step 3. - here's some code:
Code:
try
{
FileReader fr = new FileReader("carStock.txt");
BufferedReader br = new BufferedReader(fr);
String newline=null;
int i=0;
String myCarData[][] = null;
while((newline=br.readLine())!=null)
{
//carInfoFromFile.add(newline);
String[] theLine = newline.split(",");
for(String token:theLine)
{
System.out.println(token);
myCarData[i][0]=token;
myCarData[i][1]=token;
myCarData[i][2]=token;
myCarData[i][3]=token;
System.out.println(myCarData[i][0]);
}
}
i++;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}