![]() |
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.*;Let me know if you want to see any of the other code, or the txt list that the input is coming from. |
:
Grades[] data = new Grades[NUM_PERSON];:
data[index].setGrades(firstName, lastName, classNum, grade);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 { |
Okay, that got me past the NullPointer error. But I am not getting my output now.
Here is the output from my Grades class: :
And here is where I am trying to call it: :
private void showResults()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. |
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. |
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.
|
| All times are GMT -5. The time now is 12:35 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC