Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock exam

 
sun par
Ranch Hand
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyClass
{
final int i ;
public static void main(String[] arguments)
{
System.out.println(new MyClass().i);
}
}
-----------------------------
Fails to compile saying that i is not initialized. Why is it so when i is an instance variable?
Thanks
 
sun par
Ranch Hand
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MyClass
{
public static void main (String args[])
{
String s1[] = new String[5];
String str = s1[0].toUpperCase();
System.out.println(str);
}
}
----------------------------------------------
this gives null pointer exception.Why is it so?
Thanks
 
Shishio San
Ranch Hand
Posts: 223
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
An instance final variable has to be initialized before being used inside a constructor or an instance initializer.
When constructing your String array, it was initiliazied to null and therefore you're trying to use null in a case where an object is required. That triggers the NullPointerException
Hope this helps
 
sun par
Ranch Hand
Posts: 257
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shisho.. am able to understand now...
 
Shishio San
Ranch Hand
Posts: 223
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good for you
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic