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 9 of 9

Thread: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer...)

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Practicing before taking a class(Exception in thread "main" java.lang.NullPointer...)

    public class primes
    {
        // instance variables - replace the example below with your own
        private static int[] primes;
        private static int numPrimes;
        private static int ndx;
        private static int testPrime;
     
        /**
         * Constructor for objects of class primes
         */
        public primes()
        {
            // initialise instance variables
        }
     
        /**
         * An example of a method - replace this comment with your own
         * 
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         */
        public static int getPrime(int whichPrime)
        {
            int[] primes = new int[whichPrime];
     
            while (numPrimes < whichPrime)
            {
                for (ndx = 0; ndx < numPrimes && testPrime % primes[ndx] != 0; ndx++
                {
                    ;
                }
                if(ndx == numPrimes)
                {
                    primes[numPrimes++] = testPrime;
                }
                testPrime++;
            }
     
            return primes[whichPrime];
        }
    }
    -------------------------

    import java.util.Scanner;
     
    public class PrimeFinder extends primes
    {
        static public void main (String[] args)
        {
            Scanner input = new Scanner(System.in);
            int whichPrime;
            int finalprime;
     
            System.out.print("Enter which prime you want: ");
            whichPrime = input.nextInt();
     
            finalprime = getPrime(whichPrime);
     
            System.out.println("The #" + whichPrime + " prime is" + finalprime);
        }
    }

    -----------

    error:
    Exception in thread "main" java.lang.NullPointerException
    at primes.getPrime(primes.java:40)
    at PrimeFinder.main(PrimeFinder.java:21)


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    String s = null;
    System.out.println(s.length());
    The above code will generate a null pointer exception because the s variable is null. On line 40 of your code you have done something similar. You need to see what variables you are using on line 40, find out which one is null and then why it is null.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    Thank you for the answer, I was able to get my code to finish without any interferences, however it does not work correctly. The finalPrime always equals 0 for some reason, do you have any idea why this could be?


    Also, I have done this same program as a single file and it worked correctly, I am doing it now as 2 files to practice some new techniques before entering my java class.
    Last edited by JohnBowers; August 8th, 2011 at 08:44 PM.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    The code you posted above doesn't even compile.
    for (ndx = 0; ndx < numPrimes && testPrime % primes[ndx] != 0; ndx++
    {
    ;
    }
    Incomplete for loop statement. You also have a missing statement inside loop (which will compile but I have to wonder why you have it)
    Class names should begin with a capital letter.
    Giving your class and an instance variable the same name will only lead to confusion.
    You have 2 arrays called primes.
    Why does PrimeFinder extend primes? Is it a special type of primes?
    Improving the world one idiot at a time!

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    Another problem
    int[] primes = new int[whichPrime];
    return primes[whichPrime];
    This will cause an ArrayIndexOutOfBoundsException. If you create an array with a size of 10 then the valid indicies are 0 - 9 and 10 is out of bounds.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    Quote Originally Posted by Junky View Post
    Another problem
    int[] primes = new int[whichPrime];
    return primes[whichPrime];
    This will cause an ArrayIndexOutOfBoundsException. If you create an array with a size of 10 then the valid indicies are 0 - 9 and 10 is out of bounds.

    That part actually compiled okay, it's just the ending print out always comes back as 0.

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    It might compile but when you run the code it will throw the out of bounds exception. If it doesn't then the code you are running and the code you posted are different. Please post the latest version of your code.
    Improving the world one idiot at a time!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    Yes, the code will compile. But it shouldn't execute without the error.
    it's just the ending print out always comes back as 0.
    Can you show the code that returns a 0?

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Practicing before taking a class(Exception in thread "main" java.lang.NullPointer

    I have just taken the code you posted above, fixed the for loop and ran it. It throws a divide by zero error. I'm damned if I know how you are capable of running the code you posted without getting errors and having a value (any value) displayed.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  2. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  3. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  4. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM