1 Replies - 171 Views - Last Post: 14 April 2013 - 06:06 PM Rate Topic: -----

#1 Doughboy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-October 12

Null Pointer Exception

Posted 14 April 2013 - 05:54 PM

Hello,

I am writing a program that will add a DataElement to a linkedlist in a hashmap.

I am getting a null pointer exception when I try to add a DataElement to the linkedlist.

Here is what I have for a addWord method in my Dictionary class:
public class Dictionary {
    HashMap<Integer, LinkedList> hm = new HashMap<Integer, LinkedList>(26);
    LinkedList L, L0, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11 = new LinkedList();
    LinkedList L12, L13, L14, L15, L16, L17, L18, L19, L20, L21 = new LinkedList();
    LinkedList L22, L23, L24, L25 = new LinkedList();
    
    public String addWord(String word, String definition){
        DataElement element = new DataElement(word, definition);
        String ADDED = ("The word has been added.");
        String ERROR = ("The word has not been added.");
        String result = "";
        int A = 65, B = 66, C = 67, D = 68, E = 69, F = 70, G = 71, H = 72;
        int I = 73, J = 74, K = 75, L = 76, M = 77, N = 78, O = 79, P = 80;
        int Q = 81, R = 82, S = 83, T = 84, U = 85, V = 86, W = 87, X = 88;
        int Y = 89, Z = 90, MODULUS = 26;
        if(word.startsWith("A") || word.startsWith("a")) {
            int count = 0;
            result = ADDED;
            if(count == 0){
                hm.put(A % MODULUS, L13);
                L13.add(element);
                count++;
            }else{
                L13.add(element);
            }
        }return result;


Here is what I have for my main:
public static void main(String [] args){
        Dictionary d = new Dictionary();
        d.addWord("Apple", "a fruit");
    }



I am getting the exception with the code above.

Any help would be greatly appreciated.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Null Pointer Exception

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Null Pointer Exception

Posted 14 April 2013 - 06:06 PM

You are better to learn how to use array before writing such an horror

Anyhow
   LinkedList L, L0, L1, L2, L3, L4, L5, L6, L7, L8, L9, L10, L11 = new LinkedList();


this creates the LinkedList L11. All the other are null.

And LinkedList are generic so should be
new LinkedList<DataElement>();

And this is useless


        int A = 65, B = 66, C = 67, D = 68, E = 69, F = 70, G = 71, H = 72;
        int I = 73, J = 74, K = 75, L = 76, M = 77, N = 78, O = 79, P = 80;
        int Q = 81, R = 82, S = 83, T = 84, U = 85, V = 86, W = 87, X = 88;
        int Y = 89, Z = 90



int x = 'A'; x will value 65
Again you should learn how to use array

          if(count == 0){
                hm.put(A % MODULUS, L13);
                L13.add(element);
                count++;
            }else{
                L13.add(element);
            }


if both case you L13.add(element) so why the else
          if(count == 0){
                hm.put(A % MODULUS, L13);
                count++;
           }
           L13.add(element);
            


This post has been edited by pbl: 14 April 2013 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1