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

Thread: Help with reading from file

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Post Help with reading from file

    I am currently making a program to read each char in a text file and adding it into an array.
    The problem is that it can't find the file. I have tried to print out the absolute path and the path is correct.

    I can't see what I'm going to change.

    Here's my code:

    package trust.src;
     
    import java.awt.List;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
     
    public class Level {
     
    	private int level;
    	private char[][] tiles;
    	private String[] lines;
     
    	public Level() {
    		tiles = new char[20][30];
    		level = 1;
    		getLevel();
    	}
     
    	public void getLevel() {
    		String fileName = "level" + Integer.toString(level) + ".txt";
     
    		try {
                            //creates the file.
    			File file = new File("res/levels/" + fileName);
    			String line;
    			int index = 0;
    			BufferedReader reader = new BufferedReader(new FileReader(file));
     
                            //reads the lines and add them into a String array.
    			while ((line = reader.readLine()) != null) {
    				lines[index] = line;
    				index++;
    			}
     
                            //reads each char in each line.
    			for (int i = 0; i < lines.length; i++) {
    				for (int j = 0; j < lines[i].length(); j++) {
    					tiles[i][j] = lines[i].charAt(j);
    					System.out.println(lines[i]);
    				}
    			}
    		}catch(Exception ex) {System.out.println("Error while loading level"); ex.printStackTrace();}
    	}
    }

    and this is the output i get:

    Error while loading level
    java.lang.NullPointerException
    	at trust.src.Level.getLevel(Level.java:30)
    	at trust.src.Level.<init>(Level.java:17)
    	at trust.src.Board.<init>(Board.java:8)
    	at trust.src.Trust.<init>(Trust.java:13)
    	at trust.src.Trust.main(Trust.java:18)

    I dont know what i should do.
    Please help me!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with reading from file

    java.lang.NullPointerException
    at trust.src.Level.getLevel(Level.java:30)
    There is a variable on line 30 with a null value. Look at line 30, find the variable and then backtrack in the code to find out why the variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Dr.Code (July 2nd, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with reading from file

    Omg, this is crazy. I backtracked the error and fixed it, but when I print out each line in the line array.
    I get, instead of 20 lines, 599 lines! I have no idea why.

    here is my text file(I know it looks wierd, but there are 20 rows, and 30 chars on each row):

    ##############################
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #_______________________OC___#
    ##############################
    ##############################

    And here is the 599 rows output I get:

    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #____________________________#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    #_______________________OC___#
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################
    ##############################

    This is really wierd, almost humoristic, but It's also REALLY annoying.
    I have no idea why this happens.

    Can somebody help me!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with reading from file

    Post the new code with the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with reading from file

    Right, here's the code:

    package trust.src;
     
    import java.awt.List;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
     
    public class Level {
     
    	private int level;
    	private char[][] tiles;
    	private String[] lines;
     
    	public Level() {
    		lines = new String[20];
    		tiles = new char[20][30];
    		level = 1;
    		getLevel();
    	}
     
    	public void getLevel() {
    		String fileName = "level" + Integer.toString(level) + ".txt";
     
    		try {
    			File file = new File("res/levels/" + fileName);
    			System.out.println(file.getAbsolutePath());
    			String line;
    			int index = 0;
    			BufferedReader reader = new BufferedReader(new FileReader(file));
    			while ((line = reader.readLine()) != null) {
    				lines[index] = line;
    				index++;
    			}
     
    			for (int i = 0; i < lines.length; i++) {
    				for (int j = 0; j < lines[i].length(); j++) {
    					tiles[i][j] = lines[i].charAt(j);
    					System.out.println(lines[i]);
    				}
    			}
    		}catch(Exception ex) {System.out.println("Error while loading level"); ex.printStackTrace();}
    	}
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with reading from file

    There is a nested loop where the println is located. How many times will the println() method be called inside those loops?
    Multiply it out to get the total number of times the println() method is called.
    Is that the number of lines that you want to print?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Dr.Code (July 2nd, 2012)

  9. #7
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: Help with reading from file

    LOL! I'm so stupid...
    Thx man!

Similar Threads

  1. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  2. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 08:55 PM
  3. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 03:14 AM
  4. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM
  5. [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

Tags for this Thread