1

I have a class Logger that uses 3 arraysas xhared variables The arrays are initialized in the contructor but when accessing them in any other method of the class, I get a NullPointerException.

I need to know the reason and the solution.

Please see comments in the code.

file Logger.java
package logger_010.standard;

import java.io.FileOutputStream;
import java.io.PrintStream;

public class Logger {

    // declaration
    private FileOutputStream[] files;
    private PrintStream[] pss;
    private String[] messages;

    public Logger() {
        // initialisation
        try {

            FileOutputStream[] files = { 
                    new FileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger0.log"),
                    new FileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger1.log"), 
                    new FileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger2.log"), 
                    };

            PrintStream[] pss = { 
                    new PrintStream(files[0]), 
                    new PrintStream(files[1]), 
                    new PrintStream(files[2]),

            };

            String[] messages = {
                    new String ("Write error message to log file 0"),
                    new String ("Write error message to log file 1 + user"),
                    new String ("Write error message to log file 2 + user+ email"),
            };

            // Arrays instanciation is OK
            System.out.println(files[0].toString());
            System.out.println(files[1].toString());
            System.out.println(files[2].toString());
            System.out.println(pss[0].toString());
            System.out.println(pss[1].toString());
            System.out.println(pss[2].toString());
            System.out.println(messages[0].toString());
            System.out.println(messages[1].toString());
            System.out.println(messages[2].toString());
            System.out.println("++++++++++++");

        } catch (Exception e) {
            System.out.println("Exception " + e.getMessage());

        } finally {

        }
    }

    public void LogMessage(int level) {

        // Here I get a Null pointer exception

        System.out.println(files[0].toString());
        System.out.println(files[1].toString());
        System.out.println(files[2].toString());
        System.out.println(pss[0].toString());
        System.out.println(pss[1].toString());
        System.out.println(pss[2].toString());
        System.out.println(messages[0].toString());
        System.out.println(messages[1].toString());
        System.out.println(messages[2].toString());
        System.out.println("++++++++++++");

        // PrintStream[] files = OpenFiles();
        WriteLogMessage(this.getPss(), level);
        CloseFiles(pss);

    }

    private void CloseFiles(PrintStream[] pss2) {
        // TODO Auto-generated method stub

    }

    private PrintStream[] OpenFiles() {
        // TODO Auto-generated method stub
        return null;
    }

    private void WriteLogMessage(PrintStream[] files, int level) {

        this.getPss()[level].println(messages[level]);
        this.getPss()[level].flush();

    }

    public FileOutputStream[] getFiles() {
        return files;
    }

    public void setFiles(FileOutputStream[] files) {
        this.files = files;
    }

    public PrintStream[] getPss() {
        return pss;
    }

    public void setPss(PrintStream[] pss) {
        this.pss = pss;
    }

    public String[] getMessages() {
        return messages;
    }

    public void setMessages(String[] messages) {
        this.messages = messages;
    }

}

this is the file containing the main function

package logger_010.standard;

public class Start {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Logger l = new Logger();
        for (int i = 0; i < 15; i++) {
            int level = i % 2;
            l.LogMessage(level);
        }
    }

}
1
  • I solved this. We don't need an explicit constructor, because we get this error in the déclaration; see here stackmirror.cn/page/rhajqg5g6983 Commented Aug 6, 2017 at 23:10

3 Answers 3

1

You are declare a new files, message, pss variable inside constructor instead of using the variable already created of class => when using in the LogMessage method, it use the variable not init => cause the error

Sign up to request clarification or add additional context in comments.

Comments

1

You never actualy bind your class attribut with the object you define in your constructor.

By defining FileOutputStream[] files = ... instead of files = ..., which is your object attribut, you are just making a local variable whose scope is only inside the constructor.

Your constructor should be :

public Logger() {
    // initialisation
    try {

        files = { 
                new FileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger0.log"),
                new FileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger1.log"), 
                new FileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger2.log"), 
                };

         pss = { 
                new PrintStream(files[0]), 
                new PrintStream(files[1]), 
                new PrintStream(files[2]),

        };

         messages = {
                new String ("Write error message to log file 0"),
                new String ("Write error message to log file 1 + user"),
                new String ("Write error message to log file 2 + user+ email"),
        };

        // Arrays instanciation is OK
        System.out.println(files[0].toString());
        System.out.println(files[1].toString());
        System.out.println(files[2].toString());
        System.out.println(pss[0].toString());
        System.out.println(pss[1].toString());
        System.out.println(pss[2].toString());
        System.out.println(messages[0].toString());
        System.out.println(messages[1].toString());
        System.out.println(messages[2].toString());
        System.out.println("++++++++++++");

    } catch (Exception e) {
        System.out.println("Exception " + e.getMessage());

    } finally {

    }
}

Comments

0

If I initialize the FileOutputStream array with a sub class of FileOutputStream and a constructor that throws a FileNotFoundException I get this compilation error

"Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor".

I finally solved the problem by using a function (makeFileOutputStream) and this function call the FileOutputStream constructor in a try/catch block

here is the code for my class Blogger

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Logger {

    // declarations

    private FileOutputStream[] files = { 
            makeFileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger0.log"),
            makeFileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger1.log"),
            makeFileOutputStream("G:\\Users\\TarekEZZAT\\Documents\\logs\\logger2.log"), 
            };

    private PrintStream[] pss = { 
            new PrintStream(files[0]), 
            new PrintStream(files[1]), 
            new PrintStream(files[2]),
    };

    private String[] messages = { 
            new String("Write error message to log file 0"),
            new String("Write error message to log file 1 + user"),
            new String("Write error message to log file 2 + user+ email"), 
            };





    private FileOutputStream makeFileOutputStream(String string) {
        // TODO Auto-generated method stub
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(string);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return fos;
    }


    public void LogMessage(int level) {

        WriteLogMessage(this.getPss(), level);

    }


    public void CloseFile(PrintStream[] files, int level){
        // TODO Auto-generated method stub
        this.getPss()[level].close();

    }

    private void WriteLogMessage(PrintStream[] files, int level) {

        this.getPss()[level].println(messages[level]);
        this.getPss()[level].flush();
    }

    // Getters and Setters
    public FileOutputStream[] getFiles() {
        return files;
    }

    public void setFiles(FileOutputStream[] files) {
        this.files = files;
    }

    public PrintStream[] getPss() {
        return pss;
    }

    public void setPss(PrintStream[] pss) {
        this.pss = pss;
    }

    public String[] getMessages() {
        return messages;
    }

    public void setMessages(String[] messages) {
        this.messages = messages;
    }

}

1 Comment

Thanks to Tuyen Nguyen and TheBakker who put me on the right track

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.