1

I am trying to print strings into a file. What have I done wrong and it always gives me a NullPointException? I believe my exceptions catch something or an argument is needed and I dont enter it. But where?

I have writen this code, that contains the main function.

EDIT: Getting error in the second line from the bottom some.items[0]="Testing One!";.

import java.io.*;



public class StringPrinter {
            public String[] items;
            public File file;
            public StringPrinter(String fileName){
                    file = new File(fileName);}


            public void toFile(){
                    try{
                        PrintWriter pw = new PrintWriter(new FileWriter(file, false));
                            for (String st:items){ 
                                    pw.println(st);
                            }
                    }
                    catch(Exception exception){}
            }



    public static void main(String args[]){

        StringPrinter some=new StringPrinter("Workyou.txt");




        some.items[0]="Testing One!";
        some.items[1]="Testing Two!";

        some.toFile();


    }
}
george mano
  • 5,948
  • 6
  • 33
  • 43

6 Answers6

5

It seems you are getting Exception here

some.items[0]="Testing One!";

this is because you did not initialize

public String[] items;

initialize it something like this in your constructor

public StringPrinter(String fileName){
         file = new File(fileName);
         items = new String[SIZE];
}
Akram
  • 7,548
  • 8
  • 45
  • 72
3

first : As all said you have to initialize the array. Second : Why not print data to file Solution : In method ToFile() after the for loop printing the string[] value, you need to close the Printer Writer

               PrintWriter pw = new PrintWriter(new FileWriter(file, false));
               for (String st:items){ 
                          pw.println(st);
               }
               **pw.close()**

It will print your data to file.

Tej Kiran
  • 2,218
  • 5
  • 21
  • 42
2

You're trying to set the string Testing One! to the first position of the array items, but you did not initialize that string array

some.items[0]="Testing One!";

If you change this line.

public String[] items;

to this one

public String[] items = new String[2];

then it will work. Notice that you must predefine the size of the array. Notice that the array size is fixed. If you don't want the array size to be fixed, I suggest you use the wrapper class ArrayList, which size can be expanded.

import java.io.*;
import java.util.ArrayList;

public class StringPrinter {

    public ArrayList<String> items = new ArrayList<String>();
    public File file;

    public StringPrinter(String filename) {
        file = new File(filename);
    }

    public void toFile() {
        try {
            PrintWriter pw = new PrintWriter(new FileWriter(file, false));
            for (String st : items) { 
                pw.println(st);
            }
            pw.close();
        }
        catch(Exception exception) { }
    }

    public static void main(String args[]) {
        StringPrinter some = new StringPrinter("Workyou.txt");
        some.items.add("Testing One!");
        some.items.add("Testing Two!");
        some.toFile();
    }
}
Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

The items array is not initialized. You got to initialize it before actually assigning some values. If you do not want to create a fixed size classical array then you can try to use ArrayList.

Rizwan Yousuf
  • 13
  • 1
  • 4
0

Try with this , this should print the text in a file

public class StringPrinter {
        public String[] items = new String [2];
        public File file;
        public StringPrinter(String fileName){
                file = new File(fileName);}


        public void toFile(){
                try{
                    PrintWriter pw = new PrintWriter(new FileWriter(file, false));
                    for (String st:items){ 
                        pw.print(st);
                         pw.flush();

                      }

                }
                catch(Exception exception){}
        }



public static void main(String args[]){

    StringPrinter some=new StringPrinter("Workyou.txt");

    some.items[0]="Testing One!";
    some.items[1]="Testing Two!";

    some.toFile();


}

}

Documentation for flush . Not required to close the writers compulsorily to see the content in a file , just calling the flush to the writer it will add the content to the files.

thar45
  • 3,518
  • 4
  • 31
  • 48
0

Try this, the complete solved code:

import java.io.*;

public class StringPrinter {

 public String[] items;
public File file;

public StringPrinter(String filename) {
    file = new File(filename);
}

public void toFile() {
    try {
        PrintWriter pw = new PrintWriter(new FileWriter(file, false));
        for (String st : items){ 
            pw.println(st);
        }
pw.close();       //needed to close the writer stream to write data in file
    }
    catch(Exception exception) {}
}

public static void main(String args[]) {
    StringPrinter some = new StringPrinter("Workyou.txt");
some.items = new String[2];        //needed to initialize array with size
    some.items[0]="Testing One!";
    some.items[1]="Testing Two!";
    some.toFile();
}

}