0

I am unsure as to what is causing the issue here, but it throws a null pointer error every time I click on an item in the SWT list I'm testing. If anyone could help, or even explain how to properly implement an SWT list listener, that would be awesome. I hate to put this much code into one question but I cannot locate the error specifically.

public class Widget {

    /**
     * Launch the application.
     * @param args
     */
    private Label FinalPricelabel;

    public Widget(Display display) {
        uI(display);
    }


    private void uI(Display display) {
        display = Display.getDefault();
        Shell shell = new Shell();
        shell.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
        shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
        shell.setSize(1065, 435);
        shell.setText("SWT Application");

        Label titlelabel = new Label(shell, SWT.NONE);
        titlelabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
        titlelabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        titlelabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 48, SWT.NORMAL));
        titlelabel.setAlignment(SWT.CENTER);
        titlelabel.setBounds(0, 0, 1049, 81);
        titlelabel.setText("Countertop Price Calculator");

        Label constructionlabel = new Label(shell, SWT.NONE);
        constructionlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        constructionlabel.setAlignment(SWT.CENTER);
        constructionlabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        constructionlabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        constructionlabel.setBounds(20, 87, 110, 22);
        constructionlabel.setText("Construction");

        List species = new List(shell, SWT.BORDER);
        species.add("African Mahogany");
        species.add("Brazilian Cherry(Jatoba)");
        species.add("Clear Cherry");
        species.add("Character Cherry");
        species.add("Douglas Fir");
        species.add("Genuine Mahogany");
        species.add("Hickory");
        species.add("Rustic Hickory");
        species.add("Maple");
        species.add("Maple with 2 Cherry Strips");
        species.add("Mixed Maple Cherry");
        species.add("Yellow Pine");
        species.add("Knotty Pine");
        species.add("Poplar");
        species.add("Purpleheart");
        species.add("Red Oak");
        species.add("Sapele");
        species.add("Spanish Cedar");
        species.add("White Oak");
        species.add("Rustic White Oak");
        species.setBounds(173, 115, 110, 68);

        Label specieslabel = new Label(shell, SWT.NONE);
        specieslabel.setText("Species");
        specieslabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        specieslabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        specieslabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        specieslabel.setAlignment(SWT.CENTER);
        specieslabel.setBounds(173, 87, 110, 22);

        Label edgelabel = new Label(shell, SWT.NONE);
        edgelabel.setText("Edge Detail");
        edgelabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        edgelabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        edgelabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        edgelabel.setAlignment(SWT.CENTER);
        edgelabel.setBounds(325, 87, 110, 22);

        List edgedet = new List(shell, SWT.BORDER);
        edgedet.add("None +$0");
        edgedet.add("1/8\" Radius");
        edgedet.add("1/4\" Radius");
        edgedet.add("7/16\" Radius");
        edgedet.add("1/2\" Radius");
        edgedet.add("Beveled Edge");
        edgedet.add("Bullnose");
        edgedet.add("Ogee Bit");
        edgedet.add("Ogee Fillet");
        edgedet.add("French Traditional");
        edgedet.add("Small Roman Ogee");
        edgedet.add("Medium Roman Ogee");
        edgedet.add("Large Roman Ogee");
        edgedet.add("Small Cove & Bead");
        edgedet.add("Large Cove & Bead");
        edgedet.add("Wavy Edge");
        edgedet.add("Classic Molding");
        edgedet.add("Small Classic");
        edgedet.add("Large Classic");
        edgedet.add("Classic Pattern");
        edgedet.add("Classic Cove");
        edgedet.add("Tabletop Small Thumbnail");
        edgedet.add("Tabletop Large Thumbnail");
        edgedet.add("Tabletop Traditional");
        edgedet.add("Tabletop Thumbnail & Bead");
        edgedet.add("Tabletop Classic");
        edgedet.add("Tabletop");
        edgedet.setBounds(325, 115, 110, 68);

        String tester = "lolcat";

        edgedet.addListener(SWT.Selection, event -> onListItemSelect(edgedet));






        List texture = new List(shell, SWT.BORDER);
        texture.add("None $0");
        texture.add("Hand Planed $5");
        texture.add("Hand Planed, Time Worn $7.50");
        texture.add("Hand Planed, Time Worn(filled) $9");
        texture.add("Hand Scraped $5");
        texture.add("Wire Brushed $5");
        texture.add("Saw Cut(circular saw) $5");
        texture.add("Saw Cut(band saw) $5");
        texture.add("Chiseled Edges $5");
        texture.add("Dings & Dents $2");
        texture.add("Worm Holes $2");
        texture.add("Distressed(select multiple)");
        texture.setBounds(477, 115, 110, 68);

        Label texturelabel = new Label(shell, SWT.NONE);
        texturelabel.setText("Texture");
        texturelabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        texturelabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        texturelabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        texturelabel.setAlignment(SWT.CENTER);
        texturelabel.setBounds(477, 87, 110, 22);

        Label lblSize = new Label(shell, SWT.NONE);
        lblSize.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
        lblSize.setAlignment(SWT.CENTER);
        lblSize.setFont(SWTResourceManager.getFont("Trebuchet MS", 36, SWT.NORMAL));
        lblSize.setBounds(782, 87, 110, 68);
        lblSize.setText("SIZE");

        Label thicknesslabel = new Label(shell, SWT.NONE);
        thicknesslabel.setText("Thickness");
        thicknesslabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        thicknesslabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        thicknesslabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        thicknesslabel.setAlignment(SWT.CENTER);
        thicknesslabel.setBounds(649, 161, 110, 22);

        Label depthlabel = new Label(shell, SWT.NONE);
        depthlabel.setText("Depth");
        depthlabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        depthlabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        depthlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        depthlabel.setAlignment(SWT.CENTER);
        depthlabel.setBounds(782, 161, 110, 22);

        Label lengthlabel = new Label(shell, SWT.NONE);
        lengthlabel.setText("Length");
        lengthlabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        lengthlabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        lengthlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        lengthlabel.setAlignment(SWT.CENTER);
        lengthlabel.setBounds(912, 161, 110, 22);

        List thickness = new List(shell, SWT.BORDER);
        thickness.setBounds(649, 202, 110, 68);

        List construct = new List(shell, SWT.BORDER);
        construct.add("Edge Grain");
        construct.add("End Grain");
        construct.add("Wide Plank");
        construct.setBounds(20, 115, 110, 68);

        Label finishlabel = new Label(shell, SWT.NONE);
        finishlabel.setText("Finish");
        finishlabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        finishlabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        finishlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        finishlabel.setAlignment(SWT.CENTER);
        finishlabel.setBounds(20, 209, 110, 22);

        List finish = new List(shell, SWT.BORDER);
        finish.setBounds(20, 237, 110, 68);

        List anglecuts = new List(shell, SWT.BORDER);
        anglecuts.setBounds(173, 237, 110, 68);

        Label anglecutlabel = new Label(shell, SWT.NONE);
        anglecutlabel.setText("Angle Cuts");
        anglecutlabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        anglecutlabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        anglecutlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        anglecutlabel.setAlignment(SWT.CENTER);
        anglecutlabel.setBounds(173, 209, 110, 22);

        List cnccuts = new List(shell, SWT.BORDER);
        cnccuts.setBounds(325, 237, 110, 68);

        Label cnclabel = new Label(shell, SWT.NONE);
        cnclabel.setText("CNC Cuts");
        cnclabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        cnclabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 12, SWT.BOLD));
        cnclabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        cnclabel.setAlignment(SWT.CENTER);
        cnclabel.setBounds(325, 209, 110, 22);

        Spinner depthspinner = new Spinner(shell, SWT.BORDER);
        depthspinner.setFont(SWTResourceManager.getFont("Segoe UI", 36, SWT.NORMAL));
        depthspinner.setBounds(782, 202, 110, 68);

        Spinner lengthspinner = new Spinner(shell, SWT.BORDER);
        lengthspinner.setFont(SWTResourceManager.getFont("Segoe UI", 36, SWT.NORMAL));
        lengthspinner.setBounds(912, 202, 110, 68);

        Label FinalPricelabel = new Label(shell, SWT.NONE);
        FinalPricelabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
        FinalPricelabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        FinalPricelabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 42, SWT.NORMAL));
        FinalPricelabel.setAlignment(SWT.CENTER);
        FinalPricelabel.setBounds(10, 324, 1029, 72);
        FinalPricelabel.setText("Final Price: " + tester);

        //if texture == "Distressed(select multiple)"

        Label distressedlabel = new Label(shell, SWT.NONE);
        distressedlabel.setText("Distressing Options");
        distressedlabel.setForeground(SWTResourceManager.getColor(192, 192, 192));
        distressedlabel.setFont(SWTResourceManager.getFont("Trebuchet MS", 11, SWT.BOLD));
        distressedlabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
        distressedlabel.setAlignment(SWT.CENTER);
        distressedlabel.setBounds(477, 189, 110, 42);

        List disstressedoptions = new List(shell, SWT.BORDER | SWT.MULTI);
        disstressedoptions.setBounds(477, 237, 110, 68);

        //if texture == "Distressed(select multiple)"

        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
    private void onListItemSelect(List list) {

        String[] items = list.getSelection();
        String holder = FinalPricelabel.getText();
        FinalPricelabel.setText(holder + items[0]);
    }
    @SuppressWarnings("unused")
    public static void main(String[] args) {

        Display display = new Display();
        Widget ex = new Widget(display);
        display.dispose();
    }
}

The error thrown is

Exception in thread "main" java.lang.NullPointerException
    at hardwood_calculator_java.Widget.onListItemSelect(Widget.java:272)
    at hardwood_calculator_java.Widget.lambda$0(Widget.java:127)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4107)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1037)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3924)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3526)
    at hardwood_calculator_java.Widget.uI(Widget.java:264)
    at hardwood_calculator_java.Widget.<init>(Widget.java:28)
    at hardwood_calculator_java.Widget.main(Widget.java:279)
  • 1
    You never assign anything to the field FinalPricelabel - only to a local variable with the same name. So that will be null in onListItemSelect – greg-449 Apr 23 '20 at 17:48

1 Answers1

0

What exactly is on line 272? One of the objects in onListItemSelect has null value, track it down, and see why it is null at this point, must be the one at this line 272