Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.lang.NullPointerException

    I've been trying for hours and I mean hours to get this to work and I keep getting this compiler error if anyone can please help me it would be greatly appreciated.

    I am getting the Exception error on this line in the MyProgram class:

    logger.setLevel(Level.INFO);



    public abstract class SimpleLogger {
     
    	protected Level level;
     
    	public void setLevel(Level l){
     
    		level = l;
     
    	}	
    	public Level getLevel(){
     
    		return level;
     
    	}
    	public abstract void log(Level logLevel, String component, String msg) throws LoggerException;
     
    	public abstract void log(Level error, String component, Throwable t) throws LoggerException;
     
    }
     
    import java.util.*;
    import java.text.*;
    import java.io.*;
     
     
    public class FileLogger extends SimpleLogger {
     
    	private File logFile;
     
        private int logEntryNo = 0;
     
        private static FileLogger instance = null;
     
        public FileLogger(String logFilePath){
            logFile = new File(logFilePath);
            try{
                logFile.createNewFile();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        public static FileLogger getInstance(){
            return instance;
        }
    	public synchronized void add(String data){
            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
            Date date = new Date();
            String dateTime = dateFormat.format(date);
     
            try{
    			if (logFile.createNewFile()){
                    FileWriter fileWriter = new FileWriter(logFile);
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.write("[" + dateTime +  "]#" + ++logEntryNo + ": " + data);
                    bufferedWriter.newLine();
                    bufferedWriter.close();
                }
                else{
                    FileWriter fileWriter = new FileWriter(logFile,true);
                    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                    bufferedWriter.write("[" + dateTime +  "]#" + ++logEntryNo + ": " + data);
                    bufferedWriter.newLine();
                    bufferedWriter.close();
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    }
     
    	@Override
    	public void log(Level logLevel, String component, String msg)
    			throws LoggerException {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void log(Level error, String component, Throwable t)
    			throws LoggerException {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
    	}
     
    public enum Level {
     
    	DEBUG(1), WARNING(2), SEVERE(3), INFO(4), ERROR(5);
    	private int level;
     
    	private Level(int level)
    	{
    		this.level = level;
    	}
    	public int getLevel(){
    		return level;
    	}
    }
     
    public class LoggerException extends Throwable {
     
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
     
    	public LoggerException(String name){
     
    		super("No attribute named \" " + name + "\" found");
     
    	}
     
    }
     
    public class LoggerFactory {
     
    	LoggerFactory factory = new LoggerFactory();
     
    	public static FileLogger getLogger(String string) {
    		if(string.equals("file"))
    		return new FileLogger(string);
    		return null;
    	}
     
     
     
     
    }
    public class MyProgram {
     
    	private SimpleLogger logger;
     
    	public static void main(String[] args) {
    	     try {
    			SimpleLogger logger = LoggerFactory.getLogger("File");
    			logger.setLevel(Level.INFO);
     
    			 logger.log(Level.INFO, "main", "system starting");
     
    			 try {
    				double res1 = doDivision(2,3);
    				 double res2 = doDivision(3,0);
    			} catch (Exception e) {
     
    				logger.log(Level.ERROR, "main", e);
    			}
    			 logger.log(Level.INFO,  "main", "system ending");
     
    		} catch (LoggerException e) {
     
    			e.printStackTrace();
    		}
     
    	}
     
    	private static double doDivision(int i, int j) {
    		{
    			double res=0;
    			 try {
    					SimpleLogger logger = LoggerFactory.getLogger("File");
     
    				//	 logger.initialize();
     
    					 logger.log(Level.INFO, "doDivision", "entering");
     
    				     if ( j == 0)
    				     {
    				    	 throw new IllegalArgumentException("Attempt to divide by  0");
    				     }
    				     else
    				     {
    				    	 res = i/j;
    				    	 logger.log(Level.INFO, "doDivision", "result is " + res);
    				     }
     
     
    				} catch (LoggerException e) {
     
    					e.printStackTrace();
    				}
     
    			 return res;
    		}
     
    	}
     
    }

  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by nickar1172 View Post
    if(string.equals("file"))

    SimpleLogger logger = LoggerFactory.getLogger("File");
    See the strings.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException

    wow so I changed it and when I run the program I am getting zero output........its supposed to show a timestamp, log level, component that the program was in when the message is logged, and message itself, or the stack trace of the Throwable (in the case of the second log method

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by nickar1172 View Post
    when I run the program I am getting zero output
    From your above code, it's evident that log methods in FileLogger are empty.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException

    how would i go about changing that

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by nickar1172 View Post
    how would i go about changing that
    Implement log methods. In FileLogger I see some writings in an add method (however there's a bit of duplication/confusion). You are quite on the right way, but you must fix (and use) these writings.
    And consider some aspects: do you need to instantiate a SimpleDateFormat every time? Do you need to open/close the file every time? Do you really need that if-else with duplicated writing?
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerException

    This is what I need to do:

    When a message is logged, the logfile should contain the following information:  A timestamp  The log level of the message  The component that the program was in when the message is logged
     The message itself, or the stack trace of the Throwable (in the case of the second log method
    2
    The logfile should always be appended to, so that log messages from successive invocations of the logged software all appear in the file. This allows developers to compare log messages over time.

Similar Threads

  1. java.lang.NullPointerException
    By mstratmann in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 2nd, 2013, 11:21 PM
  2. java.lang.NullPointerException
    By tangara in forum Exceptions
    Replies: 14
    Last Post: August 12th, 2013, 07:34 AM
  3. java.lang.NullPointerException
    By nikomania in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2013, 05:16 PM
  4. [SOLVED] java.lang.NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2011, 11:39 AM
  5. java.lang.nullPointerException
    By ridg18 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2010, 03:52 PM