0

Java gives me a NullPointerException when using more than 2 options (Notice that case 1 and 3 have same methods) and I want to use case 1 or case 3 but case 3 gives me that error. When I enter case 1 I dont get any errors, but when I enter case 3 I get this error:

Exception in thread "main" java.lang.NullPointerException
    at Files.FileSearch.searchDirectory(FileSearch.java:124)
    at Files.FileSearch.main(FileSearch.java:93)

Its weird because this shouldn't even happen and the error is only displayed when I use case 3. My code is the following:

package Files;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import Plan.Generator;
import Plan.PathDesingerHTML;

public class FileSearch {

    private String fileNameToSearch;
    private String path;
    private List<String> result = new ArrayList<String>();

    public String getFileNameToSearch() {
        return fileNameToSearch;
    }

    public void setFileNameToSearch(String fileNameToSearch) {
        this.fileNameToSearch = fileNameToSearch;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public List<String> getResult() {
        return result;
    }

    private static File resultTemp = null;
    private static String fullPath = null;
    private static String partialPath = "C:\\Users\\etopete\\Desktop\\Designer Proyectos\\";
    private static String name = null;
    private static int option = 0;
    private static int count = 0;
    private static boolean flag = true;

    public static File getResultTemp() {
        return resultTemp;
    }

    public static String getFullPath() {
        return fullPath;
    }

    public static String getName() {
        return name;
    }

    public static int getCount() {
        return count;
    }

    public static void main(String[] args) throws IOException {

        FileSearch fileSearch= new FileSearch();
        FindJars find= new FindJars();
        PathDesingerHTML designerHTML = new PathDesingerHTML();
        Generator generate =  new Generator();


        final JFrame parent = new JFrame();

        name = JOptionPane.showInputDialog(parent, "Folder to search in?", null);
        option = new Integer(JOptionPane.showInputDialog(parent, "Option?", null));

        switch (option) {
        case 1:
            fileSearch.searchDirectory(new File(fullPath = partialPath + name + "\\build\\cobis\\dev\\projects"), ".jar");

            fileSearch.getDirectory();

            //find.Jars();
            break;
        case 2:
            fileSearch.searchDirectory(new File(fullPath = partialPath + name + "\\build\\cobis\\web\\views"), ".html");

            fileSearch.getDirectory();

            designerHTML.DesingerHTML();
            break;
        case 3:
            fileSearch.searchDirectory(new File(fullPath = partialPath + name + "\\build\\cobis\\dev\\projects"), ".jar");

            fileSearch.getDirectory();

            //find.Jars();
            break;
        }

        // try different directory and filename :)


    }

    public void getDirectory() {
        count = getResult().size();
        if (count == 0) {
            System.out.println("\nNo result found!");
        } else {
            System.out.println("\nFound " + count + " result!\n");
            for (String matched : getResult()) {
                System.out.println("Found : " + matched);
            }
        }
    }

    public void searchDirectory(File directory, String fileNameToSearch) {

        setFileNameToSearch(fileNameToSearch);

        if (directory.isDirectory()) {
            search(directory);
            System.out.println(resultTemp.toString());
            setPath(resultTemp.toString());
            System.out.println("Path is: " + getPath());
        } else {
            System.out.println(directory.getAbsoluteFile() + " is not a directory!");
        }
    }

    private File search(File file) {

        if (file.isDirectory()) {
            System.out.println("Searching directory ... " + file.getAbsoluteFile());

            // do you have permission to read this directory?
            if (file.canRead()) {

                for (File temp : file.listFiles()) {
                    if (temp.isDirectory()) {
                        search(temp);
                    } else {
                        /*
                         * if (getFileNameToSearch().equals(temp.getName().toLowerCase())) {
                         * 
                         * }
                         */
                        if(option == 1) {
                            if (temp.getAbsoluteFile().toString().endsWith(".jar")) {
                                result.add(temp.getAbsoluteFile().toString());
                                resultTemp = (File) temp.getAbsoluteFile();
                                if(flag == true) {
                                    flag = false;
                                    return new File(temp.getAbsoluteFile().toString());
                                }
                            }
                        }
                        else if(option == 2) {
                            if (temp.getAbsoluteFile().toString().endsWith(".html")) {
                                result.add(temp.getAbsoluteFile().toString());
                                resultTemp = (File) temp.getAbsoluteFile();
                                if(flag == true) {
                                    flag = false;
                                    return new File(temp.getAbsoluteFile().toString());
                                }
                            }
                        }

                    }
                }
            } else {
                System.out.println(file.getAbsoluteFile() + "Permission Denied");
            }
        }
        return null;
    }

}
E Alexis MT
  • 177
  • 1
  • 15
  • 1
    `resultTemp.toString()` will throw an NPE if `resultTemp` does not have a value assigned to it. – Compass Nov 23 '18 at 17:23
  • @Compass Yeah I understand that, but Case 1 is the same as Case 3 and they have the same exact methods. Why does Case 1 contain something in resultTemp.toString() but Case 3 doesnt contain anything in resultTemp.toString()? If I am doing exactly the same thing on both cases? – E Alexis MT Nov 23 '18 at 17:26
  • 1
    in your `search` method, you only have `if` cases for when `option == 1` and `option == 2`. Since you select option 3, `resultTemp` is null, hence the NPE – user3170251 Nov 23 '18 at 17:30
  • @user3170251 Thx, that was it! Its weird that the error wasnt a little more specific. – E Alexis MT Nov 23 '18 at 17:31
  • @EAlexisMT Glad I could help. Errors can only give us a clue as to whats happening. Only until we debug the program can we understand the context of the error. The duplicate question suggested by Sotirios gives examples on how to detect these errors – user3170251 Nov 23 '18 at 17:32
  • 1
    @EAlexisMT in java when you try to assign null value to primitive type it will always throws and exception. Though you can't assign null value directly to primitive, but you do it through some operation it will throw NPE e.g. int status = someMap.get("ValueDoesntExist"); Here if map returns null then that null value will be tried to assign to primitive int at run-time and NPE will be thrown. – Datta Jan 23 '20 at 09:11

0 Answers0