I have to write a JUnit test method for a short program that lets the user add/remove/browse images in a collection. (Adding an image is done with a JFileChooser) The test method that I wrote exhibits some really bizarre behavior; it passes every so often, but fails most of the time and produces the following error message:

java.lang.NullPointerException
	at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source)
	at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source)
	at javax.swing.plaf.basic.BasicListUI.getCellBounds(Unknown Source)
	at javax.swing.JList.getCellBounds(Unknown Source)
	at javax.swing.JList.ensureIndexIsVisible(Unknown Source)
	at sun.swing.FilePane.ensureIndexIsVisible(Unknown Source)
	at sun.swing.FilePane.doDirectoryChanged(Unknown Source)
	at sun.swing.FilePane.propertyChange(Unknown Source)
	at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
	at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
	at java.awt.Component.firePropertyChange(Unknown Source)
	at javax.swing.JFileChooser.setCurrentDirectory(Unknown Source)
	at javax.swing.JFileChooser.setSelectedFile(Unknown Source)
	at GalleryPanelTest.testGalleryPanel(GalleryPanelTest.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at junit.framework.TestCase.runTest(TestCase.java:164)
	at junit.framework.TestCase.runBare(TestCase.java:130)
	at student.TestCase.runBare(TestCase.java:108)
	at student.GUITestCase.runBare(GUITestCase.java:233)
	at junit.framework.TestResult$1.protect(TestResult.java:106)
	at junit.framework.TestResult.runProtected(TestResult.java:124)
	at junit.framework.TestResult.run(TestResult.java:109)
	at junit.framework.TestCase.run(TestCase.java:120)
	at junit.framework.TestSuite.runTest(TestSuite.java:230)
	at junit.framework.TestSuite.run(TestSuite.java:225)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Since I can't reproduce the behavior by actually running and using the program, I think it's a problem with how I wrote the test method and not any part of the actual program, but I can't seem to figure out what it is. Here's the code for the test method:

public class GalleryPanelTest
    extends GUITestCase
{

    // ----------------------------------------------------------
    /**
     * Tests the GalleryPanel.
     */
    public void testGalleryPanel()
    {
        GalleryPanel panel = new GalleryPanel();
        showInFrame( panel );

        JButton add = getComponent( JButton.class, "Add" );
        JButton remove = getComponent( JButton.class, "Remove" );
        JButton next = getComponent( JButton.class, "Next" );
        JButton prev = getComponent( JButton.class, "Previous" );

        click(add);
        JFileChooser chooser = getComponent( JFileChooser.class );
        File file1 = new File("C:/Users/Eric/Desktop/test images/1.png");
        chooser.setSelectedFile(file1);
        chooser.approveSelection();

        click(add);
        chooser = getComponent( JFileChooser.class );
        File file2 = new File("C:/Users/Eric/Desktop/test images/2.png");
        chooser.setSelectedFile(file2);
        chooser.approveSelection();

        click(next);
        click(prev);
        click(remove);
        click(remove);
    }

}

Any help would be greatly appreciated.

Sorry for pointing out the obvious, but according to the dump, the code is failing at one of your calls to setSelectedFile(). It seems like either file1 or file2 are null.

Are you sure images 1.png and 2.png always exist in a folder called "test images" on your desktop?

Sorry for pointing out the obvious, but according to the dump, the code is failing at one of your calls to setSelectedFile(). It seems like either file1 or file2 are null.

Are you sure images 1.png and 2.png always exist in a folder called "test images" on your desktop?

That's the part that doesn't make any sense to me. I know that both images exist, and there are times when I can call the method, and everything will work fine (i.e. I can see both images pop up on the screen, then get removed). However, more often than not, I get that series of error messages instead.

java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.getCellBounds(Unknown Source)
at javax.swing.JList.getCellBounds(Unknown Source)
at javax.swing.JList.ensureIndexIsVisible(Unknown Source)
at sun.swing.FilePane.ensureIndexIsVisible(Unknown Source)
at sun.swing.FilePane.doDirectoryChanged(Unknown Source)
at sun.swing.FilePane.propertyChange(Unknown Source)
at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
at java.awt.Component.firePropertyChange(Unknown Source)
at javax.swing.JFileChooser.setCurrentDirectory(Unknown Source)
at javax.swing.JFileChooser.setSelectedFile(Unknown Source)
at GalleryPanelTest.testGalleryPanel(GalleryPanelTest.java:35)

At line 35 of the file: GalleryPanelTest.java, inside the method testGalleryPanel, you are using something which is null.
Try to print all the objects you are using at that line amd see what is null.

At line 35 of the file: GalleryPanelTest.java, inside the method testGalleryPanel, you are using something which is null.
Try to print all the objects you are using at that line amd see what is null.

So I printed all of the objects that were being used by the line that was generating the exception:

click(add);
        JFileChooser chooser = getComponent( JFileChooser.class );
        File file1 = new File("C:/Users/Eric/Desktop/test images/1.png");
        System.out.println(chooser);
        System.out.println(file1.getName());
        chooser.setSelectedFile(file1); //line that generates null pointer exception
        chooser.approveSelection();

However, none of them were null. The chooser wasn't null, and file1 was the image that I specified. I did go back through the actual GalleryPanel class and looked at the code for the JFileChooser and found something interesting:

if (source == add)
            {
                int returnVal = fileChooser.showOpenDialog(GalleryPanel.this);

                System.out.println(returnVal);
                System.out.println(JFileChooser.APPROVE_OPTION);

                if (returnVal == JFileChooser.APPROVE_OPTION)
                {
                    File file = fileChooser.getSelectedFile();
                    list.add(new ImageIcon(file.getPath()));
                    repaint();
                }

            }

When everything works correctly, returnVal is equal to APPROVE_OPTION (0), but when the null pointer exception happens, returnVal is equal to ERROR_OPTION (-1). I'm still at a complete loss as to why this is happening, since if I just run the program, everything works fine, and even the test method works fine sometimes.

Thanks for the link! Putting the code that dealt with the JFileChooser fixed everything.

This question has already been answered. Start a new discussion instead.