Null Pointer Exception in Eclipse using JUnit and Comparator

I've been working on a processing sketch for pixel sorting in the Eclipse IDE. It's getting large and I'm not sure if everything is working correctly. I was hoping to start using JUnit test on small pieces. However, I can't get the the first test to run. I'm hoping someone on here has done something similar.

Here is the code the JUnit test:

package graham.wilfred.pixelsort;

import static org.junit.Assert.assertEquals;
import java.util.Comparator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import processing.core.PApplet;

public class AlphaComparatorTest extends PApplet {

    @ BeforeAll
    public static void initAll() {
        PApplet.main("graham.wilfred.pixelsort.AlphaComparatorTest");
    }

    @ Test
    @ DisplayName("AlphaComparator -- Same Alpha Value Test")
    void sameAlphaValue() {
        Comparator<Integer> c = new AlphaComparator(this);
        int testColor1, testColor2;
        for (int i = 0; i <= 255; i++) {
            testColor1 = color(i, i);
            testColor2 = color(255 - i, i);
            println("testColor1 in sameAlphaValue: " + testColor1);
            println("testColor2 in sameAlphaValue: " + testColor2);
    E>      int compareValue = c.compare(testColor1, testColor2);
            assertEquals(compareValue, 0);
        }
    }

}

Here is the code for the AlphaComparator Class:

package graham.wilfred.pixelsort;

import java.util.Comparator;
import processing.core.PApplet;

public class AlphaComparator implements Comparator<Integer> {

    PApplet parent;

    public AlphaComparator(PApplet p) {
        parent = p;
    }

    public int compare(Integer o1, Integer o2) {
        System.out.println("Compare Integer input: " + o1);
        System.out.println("Compare Integer input: " + o2);
        System.out.println("parent PApplet in AlphaComparator Class: " + parent);
        System.out.println(parent.color(0));
    E>  Float b1 = parent.alpha(o1);
        Float b2 = parent.alpha(o2);
        return b1.compareTo(b2);
    }

}

The console output is:

testColor1 in sameAlphaValue: 0
testColor2 in sameAlphaValue: 16777215
Compare Integer input: 0
Compare Integer input: 16777215
parent PApplet in AlphaComparator Class: graham.wilfred.pixelsort.AlphaComparatorTest@5a1c0542
-16777216

When I run in debug mode it gets into the compare method and I can see that parent contains all the normal stuff you would have in PApplet class.

The error is a Null Pointer Exception and it happens on E> line in the AlphaComparator Class. I've narrowed it down to the parent.alpha() call. As you can see the parent.color() works. And when I run a normal processing sketch with an image it will literally call the compare method millions of times without a problem. So I am stumped. I suppose it could be Integer vs int but I tried using .intValue() and that didn't change anything.

Any advice would be appreciated.

Answers

  • Answer ✓

    My guess is that this is caused by the alpha() function requiring the internal g variable to be initialized, which only happens when you run your sketch. You can see what I'm talking about here.

    It feels a little weird to pass a PApplet into a Comparator like this. Can you perhaps compare the float alpha values instead? Alternatively, you're going to have to actually run a sketch to fully test it, which also feels a little weird.

  • Thanks Kevin. That really helped me out.

    It is a little silly but I've been learning about JUnit in my Java class and I wanted to see if I could use it in my processing projects. It's not that practical in most cases but I like having the option.

    Here is the working code I came up with if anyone ends up doing something similar:

    package graham.wilfred.pixelsort;
    
    import static org.junit.Assert.assertEquals;
    import java.util.Comparator;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Test;
    import processing.core.PApplet;
    
    public class AlphaComparatorTest {
    
        PApplet app;
        Comparator<Integer> instance;
    
        @BeforeEach
        public void before() {
            app = new PApplet();
            String[] args = { "graham.wilfred.pixelsort.AlphaComparatorTest" };
            PApplet.runSketch(args, app);
            instance = new AlphaComparator(app);;
        }
    
        @Test
        @DisplayName("AlphaComparator -- Equal Alpha Value Test")
        void equalAlpha() {
            int testColor1, testColor2;
            for (int i = 0; i <= 255; i++) {
                testColor1 = app.color(i, i);
                testColor2 = app.color(255 - i, i);
                assertEquals(instance.compare(testColor1, testColor2), 0);
            }
        }
    
        @AfterEach
        public void after() {
            app.stop();
        }
    
    }
    

    And the Comparator:

    package graham.wilfred.pixelsort;
    
    import java.util.Comparator;
    import processing.core.PApplet;
    
    public class AlphaComparator implements Comparator<Integer> {
    
        PApplet parent;
    
        public AlphaComparator(PApplet p) {
            parent = p;
        }
    
        public int compare(Integer o1, Integer o2) {
            Float b1 = parent.alpha(o1);
            Float b2 = parent.alpha(o2);
            return b1.compareTo(b2);
        }
    
    }
    
  • Thxs for sharing @httpwilfred

    Kf

Sign In or Register to comment.