Results 1 to 9 of 9
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Populating array doesn't work inside constructor?

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class asciiFile {
    int height;
    int width;
    Scanner input;
    char[][] poop;
    	public asciiFile(File f) throws FileNotFoundException{ //constructor
    		input = new Scanner(f);
    		height = input.nextInt();
    		width = input.nextInt();
    		for (int i = 0; i < height; i++){
    			for(int j = 0; j < width; j++){
    				int value = input.nextInt();
    				char value2 = (char)value;
    				poop[i][j] = value2;
    			}
    		}
    	}
    //methods
    public void normalPrint(){
    		for (int i = 0; i < height; i++){
    			for(int j = 0; j < width; j++){
    				System.out.print(poop[i][j]);
    		
    				}
    			System.out.println("");
    				}	
    			}
    	
    
    	public void horizontalPrint(){
    		for (int i = 0; i < height; i++){
    			for(int j = 0; j < width; j++){
    				System.out.print(poop[i][width-j-1]);
    			
    			}
    			System.out.println("");
    				}	
    			}
    
    	public void verticalPrint(){
    	for (int i = 0; i < poop.length; i++){
    		for(int j = 0; j < poop[i].length; j++){
    				System.out.print(poop[poop.length-i-1][j]);
    			
    			}
    			System.out.println("");
    				}	
    			}
    		
    
    	}
    Java Code:
    Client Program:
    import java.util.*;
    import java.io.*;
    public class main {
    public static void main(String[] args)
    throws FileNotFoundException {
    	asciiFile derp = new asciiFile(new File("homer.txt"));
    	derp.normalPrint();
    
    	derp.horizontalPrint();
    
    	derp.verticalPrint();
    	}
    
    }
    The constructor is supposed to take an ASCII file, take the numbers in the file, and populate a 2D array with the numbers in the file.
    For some reason, the for loop I use to populate the array works outside of the constructor. When I put it in one of the methods, it runs normally. However, when I keep it in the constructor, I get the following error:
    Exception in thread "main" java.lang.NullPointerException
    at asciiFile.<init>(asciiFile.java:16)
    at main.main(main.java:6)
    Last edited by Propinquity; 02-24-2014 at 01:59 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Populating array doesn't work inside constructor?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Exception in thread "main" java.lang.NullPointerException
    at asciiFile.<init>(asciiFile.java:16)
    at main.main(main.java:6)
    what variable on line 16 has a null value?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Populating array doesn't work inside constructor?

    well the variables i, j and value 2 were all declared so it doesn't seem like any of them could be null

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Populating array doesn't work inside constructor?

    That's 3 of the 4 variables used on that line. primitives can not have null values, so it wasn't them.

    What is the value of the array at that line?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Populating array doesn't work inside constructor?

    Primitives won't throw an NPE. Only when you try to use an object reference set to null will that happen.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Re: Populating array doesn't work inside constructor?

    Oh. I understand my mistake now. I forgot to declare the array.

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Populating array doesn't work inside constructor?

    Quote Originally Posted by jim829 View Post
    Primitives won't throw an NPE. Only when you try to use an object reference set to null will that happen.

    Regards,
    Jim
    Yes and no. Check this out.

    Java Code:
    public void doStuff(int stuff){
       // ...
    }
    
    
    public Integer calculateStuff(){
      return null;
    }
    Java Code:
    Integer stuff = calculateStuff();
    
    doStuff(stuff); // NPE while the parameter is a primitive int
    Autoboxing can still make it appear like a primitive is causing an NPE :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Populating array doesn't work inside constructor?

    Even if it appears that the int is throwing an NPE, I believe my statement is correct. In fact, I think your example seems to further illustrate my case. The Integer stuff is throwing the NPE because it is trying to do something like this:
    Java Code:
    dostuff(stuff.intValue());  //throws an NPE because stuff == null
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,686
    Rep Power
    5

    Default Re: Populating array doesn't work inside constructor?

    It is 100% correct! My addition was about the perception only. I failed to make that clear.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Replies: 2
    Last Post: 10-31-2011, 09:03 PM
  3. why constructor doesn't have any return type?
    By kaka in forum New To Java
    Replies: 1
    Last Post: 09-30-2010, 08:02 PM
  4. Why doesn't this work? (multi-type array)
    By 711groove in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 09:56 PM
  5. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 01:19 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
  •