Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7522355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:29:57+00:00 2026-05-30T02:29:57+00:00

This code throws a null pointer exception at line 20, where the compareTo() is

  • 0

This code throws a null pointer exception at line 20, where the compareTo() is called. Any advice on how to get it to work?

package exam1review;

import java.util.Random;

public class ArrayTester {

/**
 * @param args
 */
public static void main(String[] args) {

    int result, max=0;

    Integer[] myArray = new Integer[10];
    Random rand = new Random();
    for (int i = 0; i < 10; i++) {
        myArray[i] = rand.nextInt();
        System.out.println(myArray[i]);
        while (i != myArray.length - 1) {
            result = myArray[i].compareTo(myArray[i+1]);
            if (result > 0)
                max = myArray[i];
            else
                max = myArray[i+1];
        }
    }
    System.out.println(max);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:29:58+00:00Added an answer on May 30, 2026 at 2:29 am

    the problem is that you’re looping over i, but your comparison tries to compare to myArray[i+1] before it’s set.

    Populate the array first, then look for the max value.

    Like this:

    package cruft;
    
    import java.util.Random;
    
    public class ArrayTester {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            Integer[] myArray = new Integer[10];
            Random rand = new Random();
            for (int i = 0; i < myArray.length; i++) {
                myArray[i] = rand.nextInt();
            }        
            for (Integer value : myArray) {
                System.out.println(value);
            }
            System.out.println(String.format("max value: %d", findMax(myArray)));
        }
    
        public static int findMax(Integer [] values) {
            int max = Integer.MIN_VALUE;
    
            if (values != null) {
                for (Integer value : values) {
                    if (value.compareTo(max) >= 1) {
                        max = value;
                    }
                }
            }
    
            return max;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is causing a null pointer exception. I have no idea why: private
How could this code throw a null pointer exception? for (Foo f : Vector<Foo>
I have this piece of code which is throwing a Null Pointer exception at
Hi I have below pseudo code with throws an exception like this throw new
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I am writing code that catches this OutOfMemoryException and throws a new, more intuitive
i'm trying the camera preview This is my code and it doesn't throw any
This code in JS gives me a popup saying i think null is a
I'm getting a bizarre null pointer exception and I can't understand why. I narrowed
This code does not throw an error but the query fails, that is, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.