This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need to create a similar functionality like System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need to create a similar functionality like System.out.println" Watch "Need to create a similar functionality like System.out.println" New topic
Author

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

Ramu Ganapathi
Greenhorn

Joined: Mar 09, 2004
Posts: 2
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)
^


Thanks and Regards<br />Ramu Ganapathi
john smith
Ranch Hand

Joined: Mar 04, 2004
Posts: 75
*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

Joined: Mar 09, 2004
Posts: 2
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

Joined: Jan 14, 2002
Posts: 47
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to create a similar functionality like System.out.println
 
Similar Threads
Conditional operator '?'
Ternary operators in System.out.println()
output!
JQplus
RandomAccessFile