#Java Generics

1 messages · Page 1 of 1 (latest)

queen hemlock
#

So I am trying to work with Generics with Java and am doing something with finding the maximum element of an Array, but it is going out of bounds which is forcing me to encounter a Null Point error and am not exactly sure as to why?

iron laurelBOT
#

<@&987246399047479336> please have a look, thanks.

iron laurelBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

queen hemlock
#
  public static <E extends Comparable<E>> E max(E[] list){
        E max = list[0];

        for (E element : list){
            if (max.compareTo(element) < 0)
                max = element;
        }



        return max;
    }

It'll continue going through list, but will go one out of the list where there is no value and is "null" and then hits the error when trying to compareTo a null Value giving the Null Pointer error.

iron laurelBOT
graceful oar
#

What do you mean by "one out of the list"

#

Your list can contain nulls inside it and you should account for it

queen hemlock
#

Hmm I see... I hadnt thought about that 🤔

queen hemlock
#

I feel so damn stupid... 🤦‍♂️ I did assign ways for it to check for null which I believe works, but I realized the reason I was getting the null was because I had a damn larger array then what I was supposed to have... I had a 100 array still when I forgot to downsize it for my test... I'm dumb...

graceful oar
#

The method shouldn't really care about the size of the array

#

It also should work properly for empty arrays, and as of now you'll get an exception

queen hemlock
#

yeaahh. I need to fully flush out checking for nulls. But the case of the problem, I shouldnt worry about nulls since it fills up the entire array with random numbers between 0 and 10,000 inclusively

#

I'm prob gonna try to figure out the null casing and everything after I get it to work with finding the array. Right now it seems my code doesn't save the max value very well since it will go into it regardless if its greater then or less than max. And I've went ahead and changed it to " > 0" and "< 0" but still no change unfortunately 🥲 I feel like I'm missing something completely basic

graceful oar
#

I can't say anything without seeing code

queen hemlock
#

Ahh I wasn't aware you didnt see the freshly formatted code. My apologies. I'll repost it.

#
public static <E extends Comparable<E>> E max(E[] list) {
  E max = list[0] ;
  for (E element : list) {
    if (max.compareTo(element) < 0) max = element;
  }
  return max;
}
#

The main just populates the array with random numbers and passes the array to the max method

graceful oar
#

Well I don't see anything wrong there

#

Show entire code

queen hemlock
#

alrighty

#
public class Ex19_5 {
  public static <E extends Comparable<E>> E max(E[] list) {
    E max = list[0] ;
    for (E element : list) {
      if (max.compareTo(element) < 0) max = element;
    }
    return max;
  }
  public static void main(String[] args) {
    Integer[] intArray = new Integer[5] ;
    Random numRand = new Random();
    int min = 0;
    int max = 10000;
    for (int i = 0; i < 5; i++) {
      intArray[i]  = numRand.nextInt(max - min) + min;
      System.out.println(i + 1 + " - " + intArray[i] );
    }
    System.out.println(max(intArray));
  }
}
iron laurelBOT
# queen hemlock ```java public class Ex19_5 { public static <E extends Comparable<E>> E max(E[...

Detected code, here are some useful tools:

Formatted code
public class Ex19_5 {
  public static <E extends Comparable<E>> E max(E[] list) {
    E max = list[0] ;
    for (E element : list) {
      if (max.compareTo(element) < 0) max = element;
    }
    return max;
  }
  public static void main(String[] args) {
    Integer[] intArray = new Integer[5] ;
    Random numRand = new Random();
    int min = 0;
    int max = 10000;
    for (int i = 0; i < 5; i++) {
      intArray[i]  = numRand.nextInt(max - min) + min;
      System.out.println(i + 1 + " - " + intArray[i] );
    }
    System.out.println(max(intArray));
  }
}
queen hemlock
#

This is an example output when I was running it. [6109, 8792, 6191, 2645, 4789]. Then it would find the max to be 4789.

steel walrus
#

check your if

queen hemlock
#

the one that checks the compareTo? I switched it to check if it was greater than and less than 0 to make sure I wasn't just mixing it up, and it still just goes into it.

graceful oar
#

I can only recommend running it on debug mode

#

Since I don't notice anything suspicious

#

Run it step by step

queen hemlock
#

I did run it through the debugger. And it came up with this: (I apologize. I sent a screenshot so you could see the debugging mode)

#

To me this is odd cause regardless if I switched it to >0 or <0, it'll always see it as false and go into the else statement and return 1. (This is within the just normal compareTo function that java has access to)

queen hemlock
#

I found my issue... damn... I thought proper syntax needed a semicolon on single line if statements... But nope... it doesnt... That's why it was doing that...

graceful oar
#

uhhh

#

where?

queen hemlock
#

that is a good question. In my actual code on my computer had a semicolon after the if statement. And looking back at my original posting, I didnt have one there. I dont know why it didnt work then, but at least it works now. 🤔 I probably had retyped it at some point while troubleshooting and accidentally put a semicolon in