Results 1 to 3 of 3
  1. #1
    Intrebute is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Problem with strings and arrays

    I'm fairly new to this, so I'm pretty sure I'm over-complicating things.
    I'm having this really annoying error, but I really can't tell why it happens. What I'm trying to do is make a Page class hold an array of Strings. I tried to make it so no matter what is input, at the end of the function the page would be full, and if the input String wasn't long enough, it would fill up the rest with spaces, to fill the entire page. But for some reason I get a nullPointerException. These are the methods involved:
    This one just takes an input String and sets the first lineSize number of characters and puts it in the array.
    Java Code:
    public void setLine(String newLine, int index) {
    	if (newLine.length()>=lineSize)
    	{
            if (index<lineAmount)
            {
                lines[index]=newLine.substring(0,lineSize);
            }
        }
    	else
    	{
    		if ((index<lineAmount)&&(index>=0))
    		{
                if (newLine.length()>0)
                {
                    lines[index]=newLine;
                }
                else
                {
                    lines[index]="";
                }
            }
            for (int i=newLine.length();i<lineSize;i++)
    		{
    			lines[index]=lines[index]+" ";
    		}
        }
    }
    This one just runs through all the arrays fitting the String one piece at a time.

    Java Code:
    public void setPage(String newPage) {
         for (int i=0;i<lineAmount;i++) 
    	 {
            if (newPage.length()>((i)*lineSize))
            {
                System.out.println("Line "+i+" begun creation.");
                setLine(newPage.substring(i*lineSize,(i)*lineSize),i);
                System.out.println("Line "+i+" created.");
            }
            else
            {
                setLine(" ",i);
            }
        }
    }
    This is just the Page constructor

    Java Code:
    public Page(String filler) {
    	setPage(filler);
    }
    And the main function

    Java Code:
    public static void main(String[] args) {
        Page page = new Page("aaaaaaaaaa");
        page.printSelf(true);
    }
    These are the only methods involved.
    At line 19, setLine throws a NullPointerException. I honestly don't get why. Any help?

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

    Default Re: Problem with strings and arrays

    Java Code:
    lines[index]="";
    If that is the line causing the NPE then your lines variable is null which means you have not created the array.
    Last edited by Junky; 11-29-2011 at 12:43 AM.

  3. #3
    Intrebute is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Problem with strings and arrays

    I never knew arrays themselves were also objects.
    Thank you verily.

Similar Threads

  1. Replies: 1
    Last Post: 10-08-2011, 05:41 AM
  2. strings into arrays
    By minusten in forum New To Java
    Replies: 2
    Last Post: 04-27-2011, 05:56 AM
  3. byte arrays and strings
    By 7wb in forum New To Java
    Replies: 1
    Last Post: 11-14-2010, 09:51 PM
  4. using random strings from arrays insult generator
    By carden2 in forum New To Java
    Replies: 5
    Last Post: 04-05-2010, 05:51 AM
  5. arrays strings and doubles
    By rgvbabe in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 11:26 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
  •