简体   繁体   中英

How to avoid NullPointerException in Java executable jar?

I minimize the application to reproduce the error. I used Java 8 and IntelliJ Swing Designer to make this minimum GUI app.

public class MyGui {
private JList<String> docList;
private JPanel mainPanel;
private DefaultListModel<String> listDocModel;

public MyGui(){

    listDocModel = new DefaultListModel<>();

    try (InputStream resource = MyGui.class.getResourceAsStream("/data.csv");
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8))) {
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            listDocModel.addElement(line);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    docList.setModel(listDocModel); // I didn't initialize docList. But it works when run from the IDE.  
}

public static void main(String[] args) {
    JFrame frame = new JFrame("MyGui");
    frame.setContentPane(new MyGui().mainPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize( new Dimension( 800, 800));
    frame.pack();
    frame.setVisible(true);
}
}

Later I made a jar using maven-assembly-plugin. When I run the app as java -jar myap.jar

I got this null pointer exception:-

Exception in thread

"main" java.lang.NullPointerException
        at org.example.MyGui.<init>(MyGui.java:30)
        at org.example.MyGui.main(MyGui.java:38)

Line 30 is docList.setModel(listDocModel); So my question how come that work by IDE but doesn't work form jar. By the IDE my list is filled with data as:-

在此处输入图像描述

To fix this I tried following update:-

   docList = new JList<>(listDocModel);
   mainPanel = new JPanel();
   mainPanel.add(docList);

After this my list is empty no more data:-

在此处输入图像描述

So how do I make my list with data and make an executable jar ?

Update full project:-

https://github.com/masiboo/SwingGui

// I didn't initialize docList. But it works when run from the IDE.

In that case, it is the IDE that is the problem. Possibly doing a 'clean and build' will shake the gremlins out, but I can't provide support for IDEs.

The docList ( mainPanel etc.) all need to be instantiated before use.

In addition to that, the list needs to be put on a container that is visible on the GUI, preferably wrapped in a scroll pane (as this example does).

在此处输入图像描述

Final note: It got to the point in comments, that the 'file loading was fine'. That's a good time to factor it out of the example and use hard coded data as below. The MRE below does that - so anyone can run it to see it work (or fail). Prepare and post a minimal reproducible example like this for the best help fastest.

import java.awt.*;
import javax.swing.*;

public class MyGui {

    // what happens in the IDE is irrelevant, this needs to be instantiated
    private JList<String> docList = new JList<>();
    // .. as does this
    private JPanel mainPanel = new JPanel(new BorderLayout());
    private DefaultListModel<String> listDocModel;
    // if the problem is not I/O related, factor it out by hard coding data
    private String[] data = {
        "Apples,are,Crunchy",
        "Oranges,are,Orange",
        "Bananas,are,Bent",};

    public MyGui() {
        listDocModel = new DefaultListModel<>();

        for (String line : data) {
            listDocModel.addElement(line);
        }
        // I didn't initialize docList. But it works when run from the IDE.
        // THEN THE IDE IS THE PROBLEM. Possibly fixed with a 'clean and build'.
        docList.setModel(listDocModel); 
        
        // now add the list to the panel! 
        mainPanel.add(new JScrollPane(docList));
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyGui");
        frame.setContentPane(new MyGui().mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Avoid guesses, pack will produce the min size needed to display GUI
        //frame.setPreferredSize(new Dimension(..));
        frame.pack();
        frame.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM