Results 1 to 8 of 8
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default NullPointer adding an item to a LinkedList

    Hello,

    I am trying to perform the following operation:

    Java Code:
    temp[WhichPositionInList].add(posPlusVal);
    Just before doing so, I am doing a println to verify the size of my LinkedListArray (temp), the index that im adding at(WhichPostionInList), and the contents of my string (posPlusVal). Any ideas why I could be getting a null pointer?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Either temp or temp[WhichPositionInList] or possibly posPlusVal is null. Difficult to tell without seeing more code.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Angry

    thats the frustrating part, I have:

    Java Code:
    System.out.println("adding at position: "+WhichPositionInList);
                           System.out.println("temp size is: "+temp.length);
                           System.out.println("posPlusVal is: "+posPlusVal);
                           temp[WhichPositionInList].add(posPlusVal);
    All 3 of my values return what I expect:

    WhichPositionInList = 0;
    temp size = 2;
    posPlusVal ="10 5";

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by sehudson View Post
    All 3 of my values return what I expect:
    But you haven't tested the most important one.
    Java Code:
    System.out.println(temp[WhichPositionInList]);

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    well....I thought I was adding an element to the WhichPositionInList(th) list in my array?

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    so I guess what I'm saying is, if I have:

    LinkedListArray myArray = new LinkedList[4];

    when I do:

    myArray[somePosition].add(String) it shouldnt matter whats in the list, im just appending a node, I didnt think you had to initialize each list in the listARray

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    figured it out...
    I have to create the lists individually, and I was trying to stuff a string into a list that hadnt been created..duh.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Exactly.

    You were doing the equivalent of:
    Java Code:
    String[] words = new String[5];
    System.out.println(words[0]);
    and expecting "blue" to be displayed.

Similar Threads

  1. NullPointer Exeption, but why?
    By warchieflll in forum New To Java
    Replies: 10
    Last Post: 12-09-2010, 04:34 PM
  2. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-28-2010, 11:19 PM
  3. Nullpointer error
    By teppel in forum New To Java
    Replies: 0
    Last Post: 07-10-2010, 08:38 PM
  4. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM
  5. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •