NullPointerException when trying to read Strings into an array of myObject
Hi All,
I've created an Object that holds two Strings, myObject. I have created and array of myObject and I am trying to parse some .csv data into it using a StringTokenizer however I keep getting a null pointer exception.
If I treat myObject as a simple Object and not as an array everything works ok.
PLEASE HELP.
Steve
P.S. I'm new to this forum, please excuse me if I've copied too much of my code into this query
Code :
import java.io.IOException;
import java.util.StringTokenizer;
public class ForexAnalyzer {
public static void main (String args[])throws IOException {
FileArrayProvider bulkReader = new FileArrayProvider();
String[] rawData = bulkReader.readLines("src/EURUSD602.csv");
myObject[] parsedData = new myObject[bulkReader.getLength()];
StringTokenizer token = null;
for (int i=0; i<bulkReader.getLength(); i++){
token = new StringTokenizer(rawData[i], ",");
while (token.hasMoreTokens()){
parsedData[i].setDate(token.nextToken());
parsedData[i].setTime(token.nextToken());
System.out.println("Element # " + i + " Date is " + parsedData[i].getDate() + " Time is " + parsedData[i].getTime());
}
}
}
}
class myObject {
private String date;
private String time;
//Variable set
public void setDate(String inputDate){
date = inputDate;
}
public void setTime(String inputTime){
time = inputTime;
}
//Variable get
public String getDate(){
return date;
}
public String getTime(){
return time;
}
}
Re: NullPointerException when trying to read Strings into an array of myObject
Did you overload subscript [] operator for your own class??
If not, then try it and i hope you will get that working. Thanks
Re: NullPointerException when trying to read Strings into an array of myObject
You created an array of 'myObject', but never instantiate the objects of that array. You can do so as you loop through the 'bulkReader.length()':
Code :
myObject[i] = new myObject();