• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Intersting Question

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class j2
{
public static void main(String arg[])
{
j2 jo=new j2();
jo.me(null);
}
public void me(String s)
{
System.out.println(s);
}
}
why does the following question doesnt give the NullPointerException.....is null a String? i dont think so..then why is this working fine and printing the out put as null.
please help also lemme know under what circumstances can we get a null pointer exception...example pls...
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<html><body>
i)strings are objects.
Any class level object is automatically initialised to the null. So, when you call for them using println they always print null. Let us take an example.
<code>
public class int1{
Integer i;
public static void main(String args[]){
int1 it=new int1();
it.me();
}
public void me(){
System.out.println(i);// this will print null
}
}
</code></body></html>
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anand,
as Asim told you, this is an assignment to an Object with value null. So there is nothing special about...
You can do a without any problems....
BUT you will get a NullPointerException, if you invoke a method on that string, for example if you want to print out the length...

Hope that helps, cheers
Oliver
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what about this.

class j2
{ static char[] c;
public static void main(String arg[])
{
j2 jo=new j2();
jo.me(c);
}
public void me(char[] s)
{
System.out.println(s);
}
}
why does it give a nullpointerException
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here is the explanation.
The statement , static char[] c, initializes the char array to null.
In the main() method, this char[] c of nulls is passed as an argument to public void me(char[] s).
The statement, System.out.println(s), tries to print a char[] array of nulls and hence NullPointerException is thrown.
Java API on java.io.PrintStream:
public void println(char[] x)
Prints an array of characters and then terminates the line. This method behaves as though it invokes print(char[])and then println().
public void print(char[] s)
Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
s - The array of chars to be printed
Throws:
NullPointerException - If s is null
Hope its clear.
FYI: You may try this interesting code.
class j3
{
public static void main(String arg[])
{
j3 jo=new j3();
jo.me(null);

System.out.println(jo);

j3 jo1=null;
System.out.println(jo1);
// System.out.println(null); - will give Compile time error
}

public void me(String s)
{
System.out.println(s);

}
}
The output of the above program is:
null
j3@xxxxxxx ==> hashcode value
null
Note:
The following code when executed will give compile-time error.
class NullTest1
{
public static void main(String arg[])
{
System.out.println(null);
}
}
- Suresh Selvaraj

 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic