Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Exceptions and exception handeling

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Exceptions and exception handeling

    Hello! So I decided to take a break from the previous task and move on to the next one.In this task I'm susposed to, without coding to predict the outcome of some programs that are given to us.I did a few but this one got me thinkin'.This is the code

     
    class C {
    	static int[] values; 
     
     static int len() {
    	 return values.length;
     }
     
     public static void main(String[] args) {
    	 len();
     }
    }

    Now I need to tell what error will hapen when I run the code and why. I'm guessing a NullPointerException, because the value of values is 0,and we are trying to return an object that is pointing to 0. Am I think right, is my logic okay? If not what am I missing.Thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exceptions and exception handeling

    What other forums is this posted on?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Exceptions and exception handeling

    Actually I've only posted it here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exceptions and exception handeling

    Can you compile and execute the code to see what it does?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Exceptions and exception handeling

    Well according to the rules of the assingment, no but I kinda did and my assumption seems to be correct I get an NullPointerException;

    Exception in thread "main" java.lang.NullPointerException
    at C.len(C.java:5)
    at C.main(C.java:9)

    C:\Users\Hp\Desktop\Nova mapa>

    I think that confirms what I have said, that the value of the array is 0, so the pointer is pointing to null(0) and with this method len i'm trying to get the value of the variable but since its 0 the compiler gets confused and gives me this error? Am I understanding the null pointer exception right or not?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exceptions and exception handeling

    that the value of the array is 0,
    null is not the same as 0. 0 is a valid value, null means there is no value. values has no value ie it is null.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (May 31st, 2020)

  8. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Exceptions and exception handeling

    Yea that is what i meant by 0 that it has null value. So I guess that covers this code?

Similar Threads

  1. Please help with exceptions
    By marieanne in forum Exceptions
    Replies: 5
    Last Post: May 27th, 2014, 06:30 AM
  2. Replies: 4
    Last Post: February 20th, 2013, 10:01 AM
  3. Running with Exceptions
    By Sylis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 21st, 2012, 06:50 PM
  4. UserDefined Exceptions
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: May 8th, 2012, 05:39 AM
  5. How To add my own Exceptions
    By Newtojava in forum Object Oriented Programming
    Replies: 1
    Last Post: September 2nd, 2010, 07:43 AM