Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NullPointerException with JUnit Testing

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default NullPointerException with JUnit Testing

    So I'm brushing up on my data structures and have been working on Linked Lists and Hashtables. The following method is from my Linked List class:

    public void add(String key, Model pair){
            if(key.equals(null)|| pair.equals(null)){
                System.out.println("Nothing was added");
                //return;
            }
            else{
                Node currentNode = new Node(key, pair);
                if(isEmpty()){
                    first = last = currentNode;
                    first.setNextNode(currentNode);
                    System.out.println(currentNode.toString() + " was added to "
                            + "position " + size+1);
                }
                else{
                    last.setNextNode(currentNode);
                    last = currentNode;
                    System.out.println(currentNode.toString() + " was added to "
                            + "position " + size+1);
                }
                size++;
            }
        }

    In the first part I want nothing to be added if either the key or pair object are null. However, in my test case I get an NullPointerException.

    @Test
         public void testAdd2(){
             setUp();
             list.add("Name", null);
             assertEquals(null, list.get("Name"));
             //assertEquals(0, list.printSize());
             list.put(null, pair);
             assertEquals(null, list.get("name"));
             //assertEquals(0, list.printSize());
             tearDown();
         }

    Any idea what is causing the error. I'm fairly new to JUnit also.

    Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: NullPointerException with JUnit Testing

    Post the error, copied and pasted from just as it appears at your end.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException with JUnit Testing

    java.lang.NullPointerException
    	at test.LinkedList.add(LinkedList.java:23)
    	at test.LinkedListDictionary.put(LinkedListDictionary.java:18)
    	at testCases.LinkedListDictionaryTest.testAdd2(LinkedListDictionaryTest.java:51)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: NullPointerException with JUnit Testing

    java.lang.NullPointerException
    at test.LinkedList.add(LinkedList.java:23)
    There is a variable with a null value on line 23. Look at line 23 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 23 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java automated testing tools for Unit testing
    By rameezraja in forum Member Introductions
    Replies: 2
    Last Post: April 14th, 2012, 08:51 AM
  2. JUnit Testing
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2012, 11:19 AM
  3. JUnit problems
    By degude in forum Java SE APIs
    Replies: 8
    Last Post: September 19th, 2011, 05:54 AM
  4. Junit and TestNG which one is best?
    By jammychen in forum Java Theory & Questions
    Replies: 0
    Last Post: November 7th, 2010, 05:32 AM
  5. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM