Results 1 to 3 of 3
  1. #1
    JordiLaForge is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Exclamation An array giving a null point exception...

    Hello,

    In this class I am having a file read, and I want it to return an array that corresponds to a new line in the text file. It reads the file fine, and it prints the info, but when I want
    to assign the string to the array it gives me a null point exception. Please help :)


    Java Code:
    import java.io.BufferedReader ;
    import java.io.DataInputStream ;
    import java.io.File ;
    import java.io.FileInputStream ;
    import java.io.FileReader ;
    import java.io.IOException ;
    import java.io.InputStreamReader ;
    import java.util.Scanner ;
    
    
    public class inputLoad {
        
        public static String[] inputMethod;
        
        public static int counter; 
        
        public static String[] main( String fileName )  {
    	    
    	BufferedReader br = null;
    	 
    	try {
    
    		String sCurrentLine;
    
    		br = new BufferedReader(new FileReader( "C:\\Users\\Adama\\Documents\\Programming\\MainComputer\\inputMethod\\" + fileName + ".txt" ));
    
    		counter = 0;
    		
    		while ((sCurrentLine = br.readLine()) != null) {
    		    
    		    	mainIntrerupator.printText ( sCurrentLine );
    		    
    			inputMethod[ counter ] = new String( sCurrentLine );
    			
    			counter = counter + 1;
    			
    		}
    
    	} catch ( IOException e ) {
    	    
    		e.printStackTrace();
    		
    	} finally {
    	    
    		try {
    		    
    		    if (br != null)br.close();
    		    
    		} catch ( IOException ex ) {
    		    
    		    ex.printStackTrace();
    		    
    		}
    		
    	}
    	
    	return inputMethod;
    	
        }
        
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: An array giving a null point exception...

    Java Code:
    public static String[] inputMethod;
    That only declares the variable. Where do you actually create the array?

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: An array giving a null point exception...

    Junky has given you the answer.... you have only declared a refference variable... it still doesn't point to any array(hence nullPointer)
    I'm just curious though... Why did you name this method "main"? And why did you name an array "inputMethod"?
    your code becomes impossible to read... you should try to comeUp with easier names

Similar Threads

  1. Replies: 3
    Last Post: 11-25-2011, 10:27 PM
  2. Null Pointer Exception in an Array of JCheckBoxes
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 04-02-2011, 04:50 PM
  3. cant print 2d array(giving null values)
    By fneeks in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 08:48 PM
  4. Null point exception Error
    By morya123 in forum New To Java
    Replies: 10
    Last Post: 11-25-2009, 11:22 AM
  5. null point exception in array lists
    By c_walker in forum New To Java
    Replies: 3
    Last Post: 10-17-2009, 05:38 AM

Posting Permissions

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