View Single Post
Old Dec 1st, 2006, 8:50 PM   #1
Jarod Silverstar
Newbie
 
Join Date: Dec 2006
Posts: 7
Rep Power: 0 Jarod Silverstar is on a distinguished road
Question Trouble inputing data to an Array

Hello all.
New programming novice here that has run into a weird issue.

Trying to create a program to input student data from a text file into an array, but getting an error when I run it. I am probably doing something wrong in a very simple way, but just not getting it.

I can run the sample program one of two ways, as input into a single class, or into an array. It works and returns the data correctly as a single class, but when I try to do the array part, it compiles okay, but throws an error on running: NullPointerException

import java.io.*;
import javax.swing.*;

public class GradeReader
{
	final int NUM_PERSON = 4;
	Grades[] data = new Grades[NUM_PERSON];
	// Grades data = new Grades();
	
	String firstName, lastName, classNum, grade;
	
	public static void main(String[] args) throws IOException
	{
		new GradeReader();
	}		
	
	public GradeReader() throws IOException  
	{
		getFile(); 		
		JOptionPane.showMessageDialog(null, "Reading data from File. Please hit 'OK' to continue.");	
		showResults();	
	}	
	
	private void getFile() throws IOException
	{
		String filename = "Grades.txt";
		FileReader freader = new FileReader(filename);
		BufferedReader inputFile = new BufferedReader(freader);

		for (int index = 0; index<3; index++)
			{
				firstName = inputFile.readLine();
				lastName = inputFile.readLine();
				classNum = inputFile.readLine();
				grade = inputFile.readLine();
				
				data[index].setGrades(firstName, lastName, classNum, grade);
				// data.setGrades(firstName, lastName, classNum, grade);
				
			}
		inputFile.close();
	}
	
	private void showResults()
		{
			JTextArea outArea;
			JScrollPane scroller;
			outArea = new JTextArea(30,35);
			outArea.setText(data.toString());
			scroller = new JScrollPane(outArea);
			JOptionPane.showMessageDialog(null,scroller);
			System.exit(0);
		}	

}

Let me know if you want to see any of the other code, or the txt list that the input is coming from.
Jarod Silverstar is offline   Reply With Quote