aspose file tools*
The moose likes Beginning Java and the fly likes Write to File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Write to File" Watch "Write to File" New topic
Author

Write to File

Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
Hi all.
I am editing an old project of mine which registered and dropped students from classes in a university to write information to a text file. My University file compiles fine but when I run the driver, I get this error:

--Printing out University info--
University Name: University of Florida
Current Term: Spring
Current Year: 2012
Number of Students: 9
Number of Instructors: 3
Number of Courses: 3
Exception in thread "main" java.lang.NullPointerException
at University.writeToFile(University.java:128)
at TestDriver1.main(TestDriver1.java:109)
Kbs-MacBook:Project1_Sol kb$ javac University.java

Students is a subclass of the class Person which has the method getUFID. I want to cycle through the array of students in the University class and write to the file the UFIDs of each student. Could anyone help me figure out what is wrong? Do I need to check for null objects in the array or is there something wrong with my method of writing the student's ID to the file?



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

The error message is telling you exactly what's wrong.

What's line 128 of University.java?
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
I fixed it! It was an error with null objects.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kd Martin wrote:I fixed it! It was an error with null objects.


No it wasn't. There's no such thing as a null object in Java. It was a null reference that was the problem, and we could see that from the NullPointerException in the first post.

Glad you got it fixed though!
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
Right, null pointer. I'm having trouble understanding the concept of writing to a file. Where do I find the file that I wrote to?

EDIT: Figured it out thank you!!
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
Could anyone please help me with this part of the code? I have to cycle through the array of Student objects and print out their IDs but I have to account for any null reference. I want my output to be in this format:
UFID1, UFID2, UFID3
123456, 234567, 345678

As you can see there should be commas between each ID but the last one of the list should not have a comma at the end. I thought the code I have written below would work but sometimes it doesn't because of the null references in the array. Does anyone have any idea of what I can do to fix this? Thanks!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
I suggest you use as few instructions to write to disc as possible. Join all your output with a StringBuilder and write whole lines or longer bits of text. File writing is a performance overhead.
You can add a comma only if you haven’t reached the end of the line.
Or [inelegant hack ] you can add the comma regardless, and check whether your last character is a comma and delete it. Something a StringBuilder can do very quickly.
Kd Martin
Ranch Hand

Joined: Nov 28, 2011
Posts: 59
How would I check if I have reached the end of the line? Or check if the last character is a string? I know how to do that for a String but I'm not sure how to do it for something that is written to a file. Thank you so much for your help so far!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
You can’t simply delete the last character from a file. You can’t delete it from a String, but you can quite easily create a new String with the last character removed; it is easiest from a StringBuilder.
What does “if the last character is a string” mean?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write to File