This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock  exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mock  exam" Watch "mock  exam" New topic
Author

mock exam

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
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


Sunita<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
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

Joined: Aug 29, 2002
Posts: 223
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


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks Shisho.. am able to understand now...
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Good for you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mock exam
 
It's not a secret anymore!