Results 1 to 2 of 2
  1. #1
    shaunstruwig is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Trouble with reading csv into object array

    Hi there,
    I am trying to read a csv file into an array (of objects) and then display everything in the array.. I can't seem to find what the problem is.

    here is my code:

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    
    public class Control {
    	
    	static Model[] m;
    
    	public static void readFromText() throws IOException{
    		
    	int counter=0;
    	String line;
    	BufferedReader br = new BufferedReader (new FileReader("studentcard.csv"));
    	line = br.readLine();
    
    	int a = countRows();
    	
    	
    	 Model[] m = new Model [100];
    		
    		while (line!=null){
    			String temp[];
    			temp=line.split(",");
    			
    			m [counter]= new Model (temp[0], temp[1], Integer.parseInt(temp[2]));
    			
    		
    		line=br.readLine();
    			counter ++;
    		}
    			}
    	
    	public static String listArray(){
    
    		String s="";
    
    		for(int i=0;i<3;i++){
    			s+=m[i].toString()+"\n";
    		}
    
    		return s;
    	}
    	
    public static void main(String[] args) throws IOException {
    	readFromText();
    	listArray();
    }
    }
    I get these errors:

    Exception in thread "main" java.lang.NullPointerException
    at Control.listArray(Control.java:55)
    at Control.main(Control.java:63)

    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,550
    Rep Power
    23

    Default Re: Trouble with reading csv into object array

    Exception in thread "main" java.lang.NullPointerException
    at Control.listArray(Control.java:55)
    There is a variable at line 55 with a null value that is being used to reference an object. Look at that line and find the variable with the null value and then backtrack in the code to see why it does not have a non-null value.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Java word scramble -Trouble reading input
    By dru36 in forum New To Java
    Replies: 15
    Last Post: 11-26-2011, 07:09 PM
  2. Having trouble opening and reading a txt file
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 01-29-2011, 02:55 AM
  3. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 01:11 AM
  4. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-21-2010, 11:29 PM
  5. Trouble creating object that includes array
    By Desdenova in forum New To Java
    Replies: 7
    Last Post: 05-18-2010, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •