• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Shouldn't it throw a NPE.....

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following class will print '2' when compiled and run.
class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}
Answer is :True.
But I feel that array reference expression should produce a reference to an array and if it produces null, it should a NullPointerException.
Hence it should throw a runtime exception.
please let me know whether I am right or wrong?
Sonir
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are calling the method getArray[index=2]++
getArray[2]++ which is saying to increment the third member of the array. and though the member is null, i don't know what would incrementing it produce ? can someone tell me what would happen if you are trying to increment something that is null ?
But this does not result in any exception though.

Originally posted by sonir shah:
The following class will print '2' when compiled and run.
class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}
Answer is :True.
But I feel that array reference expression should produce a reference to an array and if it produces null, it should a NullPointerException.
Hence it should throw a runtime exception.
please let me know whether I am right or wrong?
Sonir

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sonir shah:
The following class will print '2' when compiled and run.
class Test
{
public static int[ ] getArray() { return null; }
public static void main(String[] args)
{
int index = 1;
try
{
getArray()[index=2]++;
}
//1 add one line here
catch (Exception e){ } //empty catch
System.out.println("index = " + index);
}
}
Answer is :True.
But I feel that array reference expression should produce a reference to an array and if it produces null, it should a NullPointerException.
Hence it should throw a runtime exception.
please let me know whether I am right or wrong?
Sonir


Sonir,
If you add one line
catch (NullPointerException e) {
System.out.println("NPE thrown");} on //1
of your previous code, you should see NPE indeed be thrown.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer is :True.
But I feel that array reference expression should produce a reference to an array and if it produces null, it should a NullPointerException.
Hence it should throw a runtime exception.
please let me know whether I am right or wrong?
Sonir


Well, first of all, yes your program isthrowing a null pointer exception. But you are catching it, in your catch() block. a NullPointerException inherits from Exception, that is, a NullPointerException is-a Exception, so it will be caught by the catch block.
The NullPointerException never gets propigated further because you have caught it, so it is considered "handled." In your catch block, try adding "throw e" to see that you are indeed re-throwing a NullPointerException.
As for why 2 prints, it is first of all because you catch the exception, and thus prevent the program from exiting...that's why you actually get to the println() statement. The reason that index is 2 is because the [] has the highest precedence of the operators on that line, so it gets evaluated first, and in evalutating it, index gets assigned the value of 2. THEN your getArray() method gets evaluated, and at this point, a NullPointerException will be thrown because there is no element #2 in this array..in fact, there is no array at all- it's null! So that's when you get the NPE.
Rob
[ January 21, 2002: Message edited by: Rob Ross ]
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
I tried to modify the code a bit.
in place of catch I inserted a NPE and then I threw the exception..
It just worked well..!!
and the output was NPE
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently a prehistoric post, 19 years old, for real?

But I just came across the same question in an exam bank and I'm puzzled how to read the line
How is able to receive that line? I'm confused with the part being outside of getArray(). I would expect an array as parameter would have to be passed.

How to read this?
 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pieter Beernink wrote:
But I just came across the same question in an exam bank and I'm puzzled how to read the line
How is able to receive that line? I'm confused with the part being outside of getArray(). I would expect an array as parameter would have to be passed.

How to read this?



Other languages, some days it feels ALL of the other languages    allow you to index all sorts of things with [ ], lots and lots of them.

For Java, quite literally only Array types can have [ ] on them.

I don't understand the "an array as parameter" part of your question.

getArray() returns an array type, literally int[] in fact, so the syntax is just fine.  Now, the implementation of getArray() shown returns NULL, so it goes BOOM when you try to execute it, but there is no problem at all with the Syntax.  getArray() returns some kind of Array, Arrays can be indexed using [ ], and will blow up at runtime if they are NULL or you try to access an index outside of [0, Array.length)
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic