0

I'm trying to build a Java-based Network manager for unix-based systems, but I ran into some trouble I cant quite find the source of:

Exception in thread "main" java.lang.NullPointerException
    at net.argvarg.NetworkManual.SelectConfDialog.loadConfList(SelectConfDialog.java:51)
    at net.argvarg.NetworkManual.SelectConfDialog.<init>(SelectConfDialog.java:20)
    at net.argvarg.NetworkManual.Main.run(Main.java:16)
    at net.argvarg.NetworkManual.Main.main(Main.java:7)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Here are the the parts of the file that are giving an error:

package net.argvarg.NetworkManual;

import javax.swing.*;
import java.awt.event.*;
import java.io.File;

public class SelectConfDialog extends JDialog {

    private DefaultListModel confList = new DefaultListModel();
    private JList confListContainer = new JList(confList);
    private File[] confs = new File(Main.absolutePathToConfsDir).listFiles();

    private void loadConfList() {
        for (int i = 0; i < confs.length - 1; i++) {
            confList.addElement(confs[i].getName());
        }
    }
}

Any help would be greatly appreciated, thank you.

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
argarevarg
  • 31
  • 4
  • 2
    `confs` could be `null` if the path `Main.absolutePathToConfsDir` does not exits... – MadProgrammer Dec 16 '14 at 06:46
  • Which line is line 51 in SelectConfDialog.java? Looks like `new File(Main.absolutePathToConfsDir).listFiles();` returns null. Have you tried to debug? – Jens Dec 16 '14 at 06:46
  • It seems I had accidentally assumed ~ resolved to home when opening files. That should have been obvious. Thanks you for your replies – argarevarg Dec 16 '14 at 06:52
  • now, how do I mark it as resolved? – argarevarg Dec 16 '14 at 06:53
  • 1
    See also [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Dec 16 '14 at 07:06
  • See also the [File Browser GUI](http://codereview.stackexchange.com/q/4446/7784). – Andrew Thompson Dec 16 '14 at 07:07

1 Answers1

1

based on comments above, here is some good resolve

 private void loadConfList() {
        if(confs == null || confs.length == 0){
                confList.addElement("Failed To Load Conf.");
                return;
        }//confs == null | empty

        for (int i = 0; i < confs.length - 1; i++) {
            confList.addElement(confs[i].getName());
        }
    }//loadConfList()

credit goes to @MadProgrammer, if he posts an answer, then mark his.

Yazan
  • 5,966
  • 1
  • 16
  • 32