Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: NullPointerException when trying to read Strings into an array of myObject

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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

    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;
    	 }
    }
    Last edited by helloworld922; April 20th, 2010 at 07:37 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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()':

    myObject[i] = new myObject();

Similar Threads

  1. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 10:48 AM
  2. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM
  3. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  4. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM