Welcome to Dream.In.Code
Become a Java Expert!

Join 150,116 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,959 people online right now. Registration is fast and FREE... Join Now!




Trying to append some text

 
Reply to this topicStart new topic

Trying to append some text

Eladior
23 Jun, 2008 - 11:04 PM
Post #1

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 20


My Contributions
Since i am yet to find any append commands in Java,i have decided to do something that would do the same job.
CODE

import java.io.*;
class col {
    public void ncd(){
        try {
            FileReader trdr=new FileReader("C:/col.txt");
            BufferedReader brdr=new BufferedReader(trdr);
            int x=0;
            String[] ptxt=new String[1000];
            String word;
            while ((word=brdr.readLine())!=null) {
                x++;
                ptxt[x]=word;
            }
            brdr.close();
            trdr.close();
            
    FileWriter twrt=new FileWriter("C:/col.txt");
    BufferedWriter bwrt=new BufferedWriter(twrt);
    for (int i=0;i<x;i++){
    bwrt.write(ptxt[i]);
    bwrt.newLine();
    }
    bwrt.write("Ha");
    
    
    bwrt.close();
    twrt.close();
        }
        catch (IOException e) {
            System.out.println(e.getMessage());
            
        }
        }
}


public class Main {

    public static void main(String[] args) {
        col a=new col();
        a.ncd();
    
    
        

}
}



The Problem is that when i run the program it says
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Unknown Source)
at col.ncd(Main.java:20)
at Main.main(Main.java:78)
any ideas of what the problem is ?

P.S i haven't written a void that is in my program but i don't think i need it
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Trying To Append Some Text
24 Jun, 2008 - 12:31 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 428



Thanked: 4 times
Dream Kudos: 150
My Contributions
Try like this:

CODE
import java.io.*;
class col {
    public void ncd(){
        try {
            FileReader trdr=new FileReader("col.txt");
            BufferedReader brdr=new BufferedReader(trdr);
            int x=0;
            String[] ptxt=new String[1000];
            String word;
            while ((word=brdr.readLine())!=null) {
                //x++;
                ptxt[x]=word;
                x++;
            }
            brdr.close();
            trdr.close();

    FileWriter twrt=new FileWriter("col.txt");
    BufferedWriter bwrt=new BufferedWriter(twrt);
    for (int i=0;i<x;i++){
    bwrt.write(ptxt[i]);
    bwrt.write("Ha");
    bwrt.newLine();

    }
   // bwrt.write("Ha");


    bwrt.close();
    twrt.close();
        }
        catch (IOException e) {
            System.out.println(e.getMessage());

        }
        }
//}

// class Main {

    public static void main(String[] args) {
        col a=new col();
        a.ncd();




}
}


As you were incrementing x before assigning the word to the array, the first array position (0) never had anything assigned to it so was still null when you came to write it out.

User is offlineProfile CardPM
+Quote Post

Eladior
RE: Trying To Append Some Text
24 Jun, 2008 - 12:58 AM
Post #3

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 20


My Contributions
it worked!
thanx you very much
now let's go back to code
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Trying To Append Some Text
24 Jun, 2008 - 01:30 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE
Since i am yet to find any append commands in Java,i have decided to do something that would do the same job.

Jesus!
You have two options:
- the + operator is overloaded for Strings, so for String s1,s2 you can write: s1 += s2; The problem with this is that the String class is not mutable, so it'll instantiate a bunch of String objects and trash the heap with them. Newer compilers or the JIT might take care of this with some optimization, so I'd say you can get away with this.
- use a StringBuilder or StringBuffer (synchronized) class instead. They have append methods, so when you finally need a String object you can call their toString method, and there ya go.

I'd suggest forget about reinventing a wheel yourself, it is just not economical in the long run.


User is offlineProfile CardPM
+Quote Post

pbl
RE: Trying To Append Some Text
24 Jun, 2008 - 01:12 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions

FileWriter has a constructor to open "append" a file

FileWriter twrt=new FileWriter("C:/col.txt", true);

User is offlineProfile CardPM
+Quote Post

1lacca
RE: Trying To Append Some Text
24 Jun, 2008 - 01:17 PM
Post #6

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Lol, I did not even notice that it was about appending to a file, I've just seen that String appending monster in the middle of the code.
*shakes head and sigh: "I'm getting too old for this.."
User is offlineProfile CardPM
+Quote Post

pbl
RE: Trying To Append Some Text
24 Jun, 2008 - 07:26 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(1lacca @ 24 Jun, 2008 - 02:17 PM) *

Lol, I did not even notice that it was about appending to a file, I've just seen that String appending monster in the middle of the code.
*shakes head and sigh: "I'm getting too old for this.."


Licca don't worry... when you'll hit the 55 (as I did) you won't even remember having respond to a post biggrin.gif so your are not that bad remembering that you posted a reply
User is offlineProfile CardPM
+Quote Post

Eladior
RE: Trying To Append Some Text
25 Jun, 2008 - 02:29 PM
Post #8

New D.I.C Head
*

Joined: 19 Jun, 2008
Posts: 20


My Contributions
QUOTE(pbl @ 24 Jun, 2008 - 02:12 PM) *

FileWriter has a constructor to open "append" a file

FileWriter twrt=new FileWriter("C:/col.txt", true);


That helped a LOT thanx smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:11AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month