• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to create a similar functionality like System.out.println

 
Ramu Ganapathi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a similar functionality like System.out.println()...
I have created the following code(based on my knowledge in Java).
Please have a look at it and let me know what went wrong...
*******************************************************
import java.io.*;
public class System1 {
static PrintStream out1=null;
public static void main(String args[]) {
out1.println("Hi");

}
}
**********************************************
I am getting the following error message when I execute the above code..
Exception in thread "main" java.lang.NullPointerException
at System1.main(System1.java:10)
^
 
john smith
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*Ahem*
//static PrintStream out1=null;
You are trying to call a method on an object that is null. BTW: why are you trying to receate the functionality of System.out.println? Why not just use it as is?
[ March 09, 2004: Message edited by: john smith ]
 
Ramu Ganapathi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea your are right, we can use the existing one but its interesting to replicate the same with our own class names so that it helps me for better understanding....
Is so complicated ???
Please try to help me
 
Aneesha Singh
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramu,
you are getting the NullPointerException because, as John pointed out, you have set the out1 object to null and then you invoke the println on a null object. And I agree, it may be better to use the System.out.println as it is .
Cheers!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not only be better to use System.out, you really don't have any choice. You can certainly create your own PrintStream, but you can't make one that prints to standard output except by using System.out. Now, if you want to make a PrintStream that goes to a file, or over the network, etc, then great.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic