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 5 of 5

Thread: Problem reading correctly from a file

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

    Default Problem reading correctly from a file

    Hey guys, so I'm trying to read from a .txt file (which is written in a specific way) and then save some integers in a 2-dimensional array. The .txt file is written like this:

    5

    4 5 3 2 1
    1 3 2 4 5
    2 5 3 4 1
    1 4 3 2 5
    4 3 2 1 5

    My code is this:

    int[][] protimiseisA = null;
    in = new BufferedReader(new FileReader("prefsA.txt"));
    //reading the first number
    String str = in.readLine();
    //saving it for use later
    int sum = Integer.parseInt(str);
    //reading the empty line and discarding it
    str = in.readLine();
    //gist of the code, read the next five lines and save the integers in the array
    for (int x = 0; x < sum; x++) {
        str = in.readLine();                
        String[] str2 = str.split("\\s+");
            for (int y = 0; y < sum; y++) {
                 protimiseisA[x][y] = Integer.parseInt(str2[y]);
             }
     }

    Of course that's only part of the code but that's where the problem is imo, since it throws a NullPointerException at the line "protimiseisA[x][y] = Integer.parseInt(str2[y]);"

    Any help would be greatly appreciated!


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Problem reading correctly from a file

    So where did you initialize protimiseisA ?

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

    Default Re: Problem reading correctly from a file

    Hm, nowhere. I thought I did initialize it with this line: "int[][] protimiseisA = null;" Should I fill the array with zeros first? Sorry, I'm kinda new to programming...

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Problem reading correctly from a file

    "int[][] protimiseisA = null;" say the variable protimiseisA is an int[][] type. But assigning anything to null means no memory location has been assigned to the variable, Thus making java throw the nullPointerException. To initialize an array of arrays you will want to use "new insertTypeHere[insertSizeHere][]", if you know the second dimension, put it in, otherwise i think you may need to initialize the inner array. I say I think because its been a while, and I currently don't have a jdk installed on this computer.

  5. The Following User Says Thank You to JJeng For This Useful Post:

    stavrakas (April 6th, 2011)

  6. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem reading correctly from a file

    Wow, thx so much, I can't believe I missed that... Now time to work on the rest of the code

Similar Threads

  1. Problem in reading image file
    By ramhanuman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2011, 02:46 PM
  2. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  3. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM
  4. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM