Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with .jar file and ./gradlew run #30

Open
SHAUNGOH97 opened this issue Feb 3, 2020 · 8 comments
Open

Issues with .jar file and ./gradlew run #30

SHAUNGOH97 opened this issue Feb 3, 2020 · 8 comments

Comments

@SHAUNGOH97
Copy link

Hi, in both cases when I created my .jar file and when I added gradle support to my project, when I ran the main class, they both would run and display the opening screen of Duke.
However, whenever I typed in any usual command like list or todo, it would lead to a
Exception in thread "main" java.lang.NullPointerException
I'm not sure how to fix this issue, is it something to do with the classpaths of the other files?
Would appreciate any help. Thanks:)

@CornCobs
Copy link

CornCobs commented Feb 3, 2020

Hi, if your project managed to compile (and I'm sure your main class references the other classes in your other files), then there is no problems with the classpaths of the other files. A NullPointerException occurs when you expect to have an object but somehow the method you receive the object from returned null instead, and you try to call any method on the null. Maybe you could run your project in debug mode to see which method is returning null? You can also look at the stack trace to see where the exception occurs.

@mario7lorenzo
Copy link

Hi! Can I know what do you mean by running the main class? As in how do you run it? are you using "java Main" command? or ./gradlew run command? and I would like to see the details of the corresponding classes and the full error message. Thanks.

@SHAUNGOH97
Copy link
Author

I typed in ./gradlew run
And the load screen pops up asking for input commands as per usual
-If i type in any commands, it will cause this error
(But this does not occur if i run the file as per usual
eg. javac Duke.java -> java Duke)


Exception in thread "main"
java.lang.NullPointerException
at Parser.parse(Parser.java:52)
at Duke.run(Duke.java:58)
at Duke.main(Duke.java:66)

Task :run FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':run'.

Process 'command '/Library/Java/JavaVirtualMachines/jdk-11.0.5.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

@AaronCQL
Copy link

AaronCQL commented Feb 3, 2020

This is caused by your Storage class reading a file that doesn't exist. The null pointer is thrown at Parser in line 52, where the parameter String input is null. To recreate this effect in your java duke way, delete the data/ folder (located in src/main/java) containing duke.txt and run java duke again. This same error message will be thrown when attempting to add a task as reading a non-existent file will result in reading in null.

You need to handle the case where the storage file duke.txt does not exist and have not been created yet, which happens in the case of ./gradlew run (as the files will be compiled and run under the [project_root]/build folder.

@mario7lorenzo
Copy link

@AaronCQL I think if the file is not available, it will throw FileNotFoundException instead ofNullPointerException. @SHAUNGOH97 can you provide the snippets of you Parser's parse method, Duke's run and main method?

@SHAUNGOH97
Copy link
Author

SHAUNGOH97 commented Feb 3, 2020

public void parse (String input) {
    if (input.equals ("list")) {
        taskList.list();
    } else if (input.contains ("done")) {
        try {
            int taskNum = Integer.parseInt (input.substring(5));
            taskList.done(taskNum);
        } catch (Exception e) {
            System.out.println ("Please state a task number:)");
        }
    } else if (input.contains ("delete")) {
        //Delete task
        try {
            int taskNum = Integer.parseInt(input.substring(7));
            taskList.delete(taskNum);
        } catch (Exception e) {
            System.out.println ("Please state a proper delete command:(");
        }
    } else if (input.contains ("find")) {
        try {
            String keyWord = input.substring(5);
            taskList.find (keyWord);
        } catch (Exception e) {
            System.out.println ("Where is your keyword:(");
        }
    } else {
        //Create task using key words: "todo", "deadline", "event"
        if (input.contains ("todo")) {
            //todo request format: todo<space><task>
            taskList.addTask ("T", input);
        } else if (input.contains ("deadline")) {
            //deadline request format: deadline<space><task></><yyyy-mm-dd>
            taskList.addTask ("D", input);
        } else if (input.contains ("event")) {
            //event request format: event<space><task></><yyyy-mm-dd><T><hh:mm-hh:mm>
            taskList.addTask ("E", input);
        } else {
            //must have todo/deadline/event request format
            System.out.println ("Im sorry, but I do not understand what this means:-(");
        }
    }
}

public void run() throws IOException {
    ui.printOpeningScreen();
    Parser parser = new Parser(tasks);
    String input = "";
    while ( ! (input = sc.nextLine()).equals ("bye")) {
        ui.printBreak();
        parser.parse (input);
        ui.printBreak();
    }
    storage.saveFiles (tasks);
    ui.closeScreen();
}
public static void main(String[] args) throws IOException {
    new Duke ("data/duke.txt").run();
}

@AaronCQL
Copy link

AaronCQL commented Feb 3, 2020

@mario7lorenzo Yes, the FileNotFoundException is "handled" in the Duke constructor in Duke.java by simply catching and printing "error somewhere". As such, the TaskList tasks in Duke.java (line 41) never gets initialised at all. Thus, when line 54 of Duke.java runs, the Parser object is constructed with a null taskList. The null pointer is thrown in the line 52 of Parser.java: taskList.addTask ("T", input); as taskList is still null.

@SHAUNGOH97 My bad, it is your taskList in Parser.java (line 52) that is null not the input. Refer above.

@SHAUNGOH97
Copy link
Author

@AaronCQL Oh man I did not see that. I modified it and now it works:) Thanks alot @mario7lorenzo @CornCobs @AaronCQL
psps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants