Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 1st, 2006, 9: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
Old Dec 1st, 2006, 10:58 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 928
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Grades[] data = new Grades[NUM_PERSON];
This code creates an array capable of storing NUM_PERSON Grades objects. By default, each index is initialized to null.

data[index].setGrades(firstName, lastName, classNum, grade);
You are attempting to call the method setGrades on null, hence the NullPointerException.

One solution is to initialize each index of data to a new instance of Grades in the GradeReader class' constructor, which will be called when a new instance of GradeReader is created:
public class GradeReader {

      public GradeReader() {
            for (int i = 0; i < NUM_PERSON; i++)
                  data[i] = new Grades();
      }

...
titaniumdecoy is offline   Reply With Quote
Old Dec 2nd, 2006, 4:23 AM   #3
Jarod Silverstar
Newbie
 
Join Date: Dec 2006
Posts: 7
Rep Power: 0 Jarod Silverstar is on a distinguished road
Okay, that got me past the NullPointer error. But I am not getting my output now.

Here is the output from my Grades class:
	
public String toString()
		{
			String studentStr = "Student name: " + getFirstName() 
			+ " " + getLastName()
			+ "\nClass: " + getClassNum() 
			+ "\nGrade: " + getGrade();
			
			return studentStr;
		}

And here is where I am trying to call it:

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);
		}

Again, I got the output doing a single object, but now that I do have mutliple objects in the array, I get junk returned.

I assume I need to do something with this line:
outArea.setText(data.toString());
But so far I have not been able to come up with the right option.

Thank you.
Jarod Silverstar is offline   Reply With Quote
Old Dec 2nd, 2006, 5:01 AM   #4
Jarod Silverstar
Newbie
 
Join Date: Dec 2006
Posts: 7
Rep Power: 0 Jarod Silverstar is on a distinguished road
Well, I partly figured it out.

When I do this:
outArea.setText(data[0].toString() + data[1].toString());

I get my output. But it seems to be that there must be a much better way of doing this.
Jarod Silverstar is offline   Reply With Quote
Old Dec 2nd, 2006, 3:19 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 928
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
If you want to print every item in the list, use a loop. Consider cocatenating each item into a temporary String variable, or using JTextArea's append method.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read unknown total of int data from text file to a 2-dim array in C ladyscarlet99 C 2 Sep 9th, 2005 3:28 AM
How to read unknown total of int data from text file to a 2-dim array in C++? ladyscarlet99 C++ 2 Sep 9th, 2005 2:01 AM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 1:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 1:27 PM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC