0

I've searched all of over SO but I haven't quite found exactly what I'm looking for because it's overly complicated. I want to know why I'm getting this error (posted below). My code seems logically correct but for reason I'm getting the null pointer exception.

Here's my HashTable.java file:

import java.util.*;

public class HashTable {
    public static void lastFourDigitsStudent(Student s) {
        LinkedList<Student>[] llStudent = new LinkedList[1000];

        String a = s.getId().substring(4,8);
        int lastFourDigits = Integer.parseInt(a);
        int hashFunction = lastFourDigits % 1000;

        llStudent[hashFunction].add(s);
    }
}

Here's the error I'm getting:

Type safety: The expression of type LinkedList[] 
needs unchecked conversion to conform to LinkedList<Student>[]
Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
bojack
  • 73
  • 1
  • 7
  • Where are you getting a NullPointer? What is `s`? How are you calling this method? – OneCricketeer May 24 '17 at 17:47
  • @cricket_007 then it won't compile. You can't create a array of parametised types. This does compile, but with a warning – dumbPotato21 May 24 '17 at 17:48
  • @ChandlerBing According to the user it's an error, so I suppose it's not compiling. – nbro May 24 '17 at 17:48
  • 4
    You created an array of 1000 `LinkedList` but you never initialise them – litelite May 24 '17 at 17:50
  • @litelite didn't read that. Just saw the warning message. Sorry to bother. – dumbPotato21 May 24 '17 at 17:54
  • @ChandlerBing not stress, happen to me all the time ;) – litelite May 24 '17 at 17:54
  • @cricket_007 In my main, I'm calling it like so: `lastFourDigitsStudent(student1);` where `student1` is a `Student` object. – bojack May 24 '17 at 17:56
  • 2
    @bojack Please distinguish a compile-time warning from a run-time error, i.e., (in your case:) NullPointerException. – laune May 24 '17 at 17:57
  • @laune Why on earth would you flag this question? I'm sorry if I'm not an expert in Java like you. I clearly said I searched SO for this exact problem but didn't find what I was looking for because of it being so arcane. You don't need to flex your ego like this. – bojack May 24 '17 at 18:00
  • @bojack First, I did not "flag" your question. I marked it as duplicate, for the stated reason - this and litelite's comment should be enough to help you find the problem. And I pointed out to you that, while your title and text indicate a NPE, you quote a compile-time warning which isn't very meaningful. - If all this help hurts your tender hide you should try and write better questions. -- Marking a question as duplicate is standard practice. – laune May 24 '17 at 18:35
  • @laune Yeah, "tender inside". I didn't intentionally try to make a post that's not meaningful. I'm new to Java. I didn't distinguish compile-time warning from a run-time error correctly by accident & I'm sure as hell you understood what I meant and as a result you shot down the post which just shows you're just trying to flex your muscle. You're forgetting you were once new at this as well. Eat glass. – bojack May 24 '17 at 22:44
  • @bojack What in "Marking a question as duplicate is standard practice" do you not understand? You are talking nonsense when you say "shot down the post". -- Of course it was clear to me that you didn't quite know why you got the NPE, but I'm sure you know that by now. – laune May 25 '17 at 07:29

0 Answers0