Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

File IO

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
9 replies to this topic

#1 destructivArts   Members   

205
Like
0Likes
Like

Posted 12 August 2012 - 10:43 PM

Ok this is just a quick question, I've solved it before and I have no idea what I did last time.

I'm trying to read text from a txt file, but every time I run the code, the first time it works perfectly, and then the second time it gives me a null pointer error on the line with the arrows. It will work again on the same file if I open it, backspace a character and then retype it and save.

Heres the code:
	public void readFile() throws IOException{
		InputStream stream = this.getClass().getResourceAsStream(filePath+fileName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
	  
		String line;
	  
		do{
			//Read each line of the file
			//Each line will correspond to separate entities. End characters will denote the end of an entity's data
			//Each line will be read, 'decrypted' and then set into the elements ArrayList
			line = reader.readLine();
			lines.add(line);
			System.out.println(line);
		}while(!line.equals("-END_OF_STREAM-"));   //<<<<<<<<<<
		reader.close();
		stream.close();
	}

Any help is much appreciated,
Thanks,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

#2 oggs91   Members   

421
Like
-2Likes
Like

Posted 13 August 2012 - 03:36 AM

i guess the file is still openend when u call your program the second time

#3 6510   Members   

151
Like
-2Likes
Like

Posted 13 August 2012 - 06:30 AM

Just study the documentation of BufferedReader#readLine.

#4 mrhodes   Members   

380
Like
1Likes
Like

Posted 14 August 2012 - 02:05 PM

I'm not a pro, but I would suggest not throwing that exception. You can gain some good debugging info if you print out a stacktrace.
Also, by dealing with the exceptions at the lowest level you don't have to keep throwing them... they will trickle right to the top of your code if you're not careful


Hope that helps somewhat.
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net

#5 dmatter   Members   

4647
Like
2Likes
Like

Posted 14 August 2012 - 02:36 PM

readLIne will return null if the end of the file is reached. That probably explains the NPE (NullPointerException) on the line you mentioned. Your code implies that you are expecting to encounter some specific text prior to reading the entire file; without seeing the contents of the file in question it's hard to tell if that condition should be being met. It appears that you have enough logging going on to see whether your termination text is found before the end of the file is reached.

#6 BitMaster   Members   

8647
Like
2Likes
Like

Posted 15 August 2012 - 03:54 AM

I'm not a pro, but I would suggest not throwing that exception. You can gain some good debugging info if you print out a stacktrace.


In Java, exceptions come with a complete stacktrace right out of the box.

#7 The_Neverending_Loop   Members   

635
Like
2Likes
Like

Posted 15 August 2012 - 12:56 PM

are you by anychance editing the file after the first read?, just a hunch but maybe you are accidently editing the file after the first read, and when you try to read again it doesn't exactly follow the format it was just in, which would explain why you can run the program once fine, but then running the same program again will give you a null error until you manually go change the file.

Again just a hunch :-/

#8 Bench3   Members   

126
Like
1Likes
Like

Posted 15 August 2012 - 02:31 PM

I would suggest not using a do while loop and doing the following:

Instead of a do while loop, just make it a while loop :)

Eg:

[source lang="java"] public void readFile() throws IOException{ InputStream stream = this.getClass().getResourceAsStream(filePath+fileName); BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); String line; while(!line.equals("-END_OF_STREAM-")){ //Read each line of the file //Each line will correspond to separate entities. End characters will denote the end of an entity's data //Each line will be read, 'decrypted' and then set into the elements ArrayList line = reader.readLine(); lines.add(line); System.out.println(line); } //<<<<<<<<<< reader.close(); stream.close(); }[/source]

Hope this works :)

~Ben

#9 dmatter   Members   

4647
Like
3Likes
Like

Posted 15 August 2012 - 03:04 PM

Hope this works Posted Image

Almost, the line variable is null so you can't invoke .equals without causing an NPE. You could choose to initialise the string to something other than the termination condition. Another way to go is to reverse the equality operands since equals() can correctly cope with null as a parameter, e.g.

while(!"-END_OF_STREAM-".equals(line))

Edited by dmatter, 15 August 2012 - 03:05 PM.


#10 destructivArts   Members   

205
Like
1Likes
Like

Posted 15 August 2012 - 06:45 PM

Thank you guys for the help!
I had a program that was meant to write to the file that was running at the end of the game, and was messing up the formatting which caused the game not to recognize the END_OF_STREAM tag.

Thanks again,
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.