diff --git a/PROMPT.txt b/PROMPT.txt new file mode 100644 index 0000000..859cbf0 --- /dev/null +++ b/PROMPT.txt @@ -0,0 +1,44 @@ +Pretend that you are a debugging assistant for novice programmers and your responses must be backed by the Kolb Experiential Learning Theory. + +You will be given the language used, error code, the source code/s (with the filename/s), and the corresponding error message. + +The structure should look like this: + +Language: {{language}} +Error Code: {{error_code}} +Source code: +{{filename}} +``` +{{source code}} +``` +Error message: +``` +{{error_message}} +``` + +The target audience of this would be students from the Philippines who don't know how to read the error messages or does not know how to do debugging. They also have a short attention span so longer explanations do not work for them. This is learning by doing so make them understand and gradually do not rely on this tool. + +--- + +The format should be like this (STICK TO THIS!): +# {{error_code}} +{{general description of the error}} +{{explanation local to the file}} + +## Steps to fix +{{step number}}. {{short simple explanation of the fix}} +{{if there are code changes}} +```diff +{{two lines before the offending line/s}} +- {{offending line/s}} ++ {{propose code}} +{{two lines after the offending line/s if available}} +``` +{{endif}} + +"Steps to fix" notes: +- Offending code must be surrounded by two lines above it and two lines below it +- Proposed code must be indicated by "+" before, offending code / code must be denoted by "-" +- If error is on multiple files, rely on the stack trace if present. +- Code modification is not necessary. +- You may give more than one bug fix suggestion. diff --git a/error_templates/java/test_files/arithmetic_exception/test.txt b/error_templates/java/test_files/arithmetic_exception/test.txt index 46cba35..196e130 100644 --- a/error_templates/java/test_files/arithmetic_exception/test.txt +++ b/error_templates/java/test_files/arithmetic_exception/test.txt @@ -5,4 +5,15 @@ Exception in thread "main" java.lang.ArithmeticException: / by zero === template: "Java.ArithmeticException" --- -One of your variables initialized a double value by dividing a number to zero +# ArithmeticException +This error occurs when you try to divide by zero, which is not allowed in mathematics. + +## Steps to fix +1. To fix this issue, you should avoid dividing by zero. In this case, you can change the division operation to something meaningful. + +```diff +- double out = 3 / 0; ++ double out = 3 / 1; // Change the division to something valid +``` + +By changing the division from 3 / 0 to 3 / 1, you will avoid the ArithmeticException, and your code will run without errors. diff --git a/error_templates/java/test_files/array_index_out_of_bounds/OOB.java b/error_templates/java/test_files/array_index_out_of_bounds/OOB.java index 04cf496..4de6332 100644 --- a/error_templates/java/test_files/array_index_out_of_bounds/OOB.java +++ b/error_templates/java/test_files/array_index_out_of_bounds/OOB.java @@ -1,6 +1,6 @@ -public class Arith { - public static void main(String[] args) { - double out = 3 / 0; - System.out.println(out); - } +public class OOB { + public static void main(String[] args) { + int nums[] = {1,2,3,4}; + System.out.println(nums[5]); + } } diff --git a/error_templates/java/test_files/array_index_out_of_bounds/test.txt b/error_templates/java/test_files/array_index_out_of_bounds/test.txt index f58336e..f7c7e60 100644 --- a/error_templates/java/test_files/array_index_out_of_bounds/test.txt +++ b/error_templates/java/test_files/array_index_out_of_bounds/test.txt @@ -5,4 +5,26 @@ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out === template: "Java.ArrayIndexOutOfBoundsException" --- -Your program attempted to access an element in index 5 on an array that only has 4 items +# ArrayIndexOutOfBoundsException +This error occurs because the program is attempting to access an element in an array at an index that does not exist. + +## Steps to fix +1. **Understanding the issue:** The error is due to the attempt to access the element at index 5 in an array that has a length of 4. Array indices start from 0, so in an array of length 4, the valid indices are from 0 to 3. + +```diff +- System.out.println(nums[5]); ++ // Accessing an index beyond the array length causes the ArrayIndexOutOfBoundsException. +``` + +## Steps to fix (Alternative) +1. **Avoid accessing out-of-bounds index:** To fix this, ensure that the index used to access the array elements is within the bounds of the array (0 to array length - 1). + +```diff + public static void main(String[] args) { + int nums[] = {1,2,3,4}; +- System.out.println(nums[5]); ++ // Ensure the index is within the array bounds. + } +``` + +Remember, in Java arrays, indices start from 0, and the last index is always length - 1. Accessing an index beyond the array's length leads to an `ArrayIndexOutOfBoundsException`. diff --git a/error_templates/java/test_files/array_required_type_error/test.txt b/error_templates/java/test_files/array_required_type_error/test.txt index 92649b1..df7f41e 100644 --- a/error_templates/java/test_files/array_required_type_error/test.txt +++ b/error_templates/java/test_files/array_required_type_error/test.txt @@ -7,4 +7,32 @@ NotArray.java:4: error: array required, but int found === template: "Java.ArrayRequiredTypeError" --- -You are calling an index notation on a variable with type int +# ArrayRequiredTypeError +This error occurs because you're trying to access an index of an integer variable `number` as if it were an array, but `number` is just an integer, not an array. + +## Steps to fix +1. **Understand the issue:** In Java, you can only use square brackets to access elements in an array, not on an individual integer variable. + +```diff +public class NotArray { + public static void main(String[] args) { +- int number = 5; ++ int[] number = {5}; // Define 'number' as an array with a single element. +- int value = number[0]; // Access the first (and only) element of the 'number' array. ++ int value = number[0]; + } +} +``` + +2. **Fix the code:** Change the `number` variable to an array by using square brackets and assign the value you wish to access in the array. Then, you can access the first element using square brackets as intended for arrays. This allows you to access the first element of the array correctly. + +```diff +public class NotArray { + public static void main(String[] args) { +- int number = 5; ++ int[] number = {5}; // Define 'number' as an array with a single element. +- int value = number[0]; // Access the first (and only) element of the 'number' array. ++ int value = number[0]; + } +} +``` diff --git a/error_templates/java/test_files/non_static_method_access_error/test.txt b/error_templates/java/test_files/non_static_method_access_error/test.txt index e7c27cc..660209b 100644 --- a/error_templates/java/test_files/non_static_method_access_error/test.txt +++ b/error_templates/java/test_files/non_static_method_access_error/test.txt @@ -8,8 +8,8 @@ Main.java:9: error: non-static method printMessage() cannot be referenced from a template: "Java.NonStaticMethodAccessError" --- # NonStaticMethodAccessError -In the `Main` class, you have invoked the `printMessage` method inside the `main` static method. - +This error occurs when trying to access a non-static method from a static context. In Java, a non-static method belongs to an instance of the class and needs an object to be called upon. + ``` // Attempt to call the non-static method without creating an object printMessage(); // This will result in an error @@ -19,4 +19,12 @@ In the `Main` class, you have invoked the `printMessage` method inside the `main ``` ## Steps to fix -1. Fix 1 \ No newline at end of file +1. **Create an instance of the class to access the non-static method** +```java +Main obj = new Main(); +obj.printMessage(); // This will properly call the non-static method +``` + +The issue arises because the `printMessage()` method is non-static, meaning it belongs to an instance of the class. To use this method within the `main` method, you must create an object of the `Main` class and then call the `printMessage()` method using that object. + +This correction instantiates an object (`obj`) of the `Main` class and then calls the `printMessage()` method using this object, resolving the error. diff --git a/error_templates/java/test_files/null_pointer_exception/test.txt b/error_templates/java/test_files/null_pointer_exception/test.txt index 6734825..13f7274 100644 --- a/error_templates/java/test_files/null_pointer_exception/test.txt +++ b/error_templates/java/test_files/null_pointer_exception/test.txt @@ -6,6 +6,25 @@ Exception in thread "main" java.lang.NullPointerException template: "Java.NullPointerException" --- # NullPointerException -Your program tried to print the value of "toUpperCase" method from "test" which is a null. +This error occurs because the program is trying to access a method or property of a null object, in this case, trying to invoke the `toUpperCase()` method on a `null` string. + ## Steps to fix -1. Go to blabla \ No newline at end of file +1. Check for the variable that is being used as `null` and causing the issue. +```diff + String test = null; +- System.out.println(test.toUpperCase()); ++ if (test != null) { ++ System.out.println(test.toUpperCase()); ++ } else { ++ System.out.println("The string is null."); ++ } +``` + +2. An alternative fix is to initialize the `test` variable with a non-null value before calling the `toUpperCase()` method. +```diff +- String test = null; ++ String test = "example"; // Assign a non-null value + System.out.println(test.toUpperCase()); +``` + +Both proposed solutions help to avoid the `NullPointerException`. The first one checks if the variable is `null` before invoking the `toUpperCase()` method. The second one ensures the variable is initialized with a non-null value to prevent the error. diff --git a/error_templates/java/test_files/parse_end_of_file_error/test.txt b/error_templates/java/test_files/parse_end_of_file_error/test.txt index cf6b2be..e178c85 100644 --- a/error_templates/java/test_files/parse_end_of_file_error/test.txt +++ b/error_templates/java/test_files/parse_end_of_file_error/test.txt @@ -7,4 +7,23 @@ EOF.java:4: error: reached end of file while parsing === template: "Java.ParseEndOfFileError" --- -The compiler was not able to compile your program because of an incomplete syntax +# ParseEndOfFileError +This error indicates that the compiler reached the end of the file unexpectedly while it was expecting something more in the code. + +## Explanation local to the file +The error occurred due to an incomplete block of code in the `EOF.java` file. The compiler was expecting more code or the completion of a block but encountered the end of the file prematurely. + +## Steps to fix +1. Ensure that all opening braces `{` have their corresponding closing braces `}` to complete the code blocks. + +```diff + public class EOF { + public static void main(String[] args) { + System.out.println("This is a sample program."); ++ } + } +``` + +This fix completes the `main` method by adding the closing curly brace `}`. + +2. If the error persists, check the entire file for missing or misplaced braces, ensuring they are correctly matched and closed. Double-check that each opening brace has a corresponding closing brace to resolve this issue. diff --git a/error_templates/java/test_files/public_class_filename_mismatch_error/test.txt b/error_templates/java/test_files/public_class_filename_mismatch_error/test.txt index 1e8d970..86b311f 100644 --- a/error_templates/java/test_files/public_class_filename_mismatch_error/test.txt +++ b/error_templates/java/test_files/public_class_filename_mismatch_error/test.txt @@ -7,4 +7,25 @@ Wrong.java:1: error: class Right is public, should be declared in a file named R === template: "Java.PublicClassFilenameMismatchError" --- -Public class "Right" does not match the name of the file which is "Wrong.java". Rename it to "Right.java" +# PublicClassFilenameMismatchError +This error occurs because the name of the Java file does not match the name of the public class within it. + +## Steps to fix +1. **File and Class Name Mismatch**: In Java, the filename should match the name of the public class in the file. + +```diff +- public class Right { ++ public class Wrong { +``` + +The error is due to the mismatch between the filename (`Wrong.java`) and the class name (`Right`). To resolve this, change the class name to match the filename: + +```java +public class Wrong { + public static void main(String[] args) { + + } +} +``` + +This modification renames the class to match the file name, resolving the error. diff --git a/error_templates/java/test_files/unclosed_character_literal_error/test.txt b/error_templates/java/test_files/unclosed_character_literal_error/test.txt index ddfd8b1..9f3657c 100644 --- a/error_templates/java/test_files/unclosed_character_literal_error/test.txt +++ b/error_templates/java/test_files/unclosed_character_literal_error/test.txt @@ -10,4 +10,24 @@ UnclosedCharacterLiteralError.java:4: error: unclosed character literal === template: "Java.UnclosedCharacterLiteralError" --- -aa +# UnclosedCharacterLiteralError +This error occurs because the character literal in the code is not correctly closed. In Java, character literals should contain only a single character enclosed in single quotes. However, in this case, 'Hello World' is a string and cannot be stored in a single character variable. + +## Steps to fix +1. Change the variable type from `char` to `String` as 'Hello World' is a string. +```diff + public static void main(String[] args) { +- char ch = 'Hello World'; // This will cause an error because the character literal is not closed. ++ String ch = "Hello World"; // Change the variable type to String for a string value. + } +``` + +2. If you want to store a single character, correct the character literal to contain only one character within single quotes. +```diff + public static void main(String[] args) { +- char ch = 'Hello World'; // This will cause an error because the character literal is not closed. ++ char ch = 'H'; // Store a single character within single quotes. + } +``` + +The error message highlights an attempt to assign multiple characters within single quotes to a `char` variable, which causes the "UnclosedCharacterLiteralError". To fix this, either change the variable type to `String` or ensure that only a single character is enclosed within the single quotes for a `char` variable. diff --git a/error_templates/java/test_files/unknown_variable_error/test.txt b/error_templates/java/test_files/unknown_variable_error/test.txt index eaf88ea..c8a3372 100644 --- a/error_templates/java/test_files/unknown_variable_error/test.txt +++ b/error_templates/java/test_files/unknown_variable_error/test.txt @@ -20,4 +20,4 @@ The program cannot find variable "a" ``` ## Steps to fix -Nothing to fix \ No newline at end of file +Nothing to fix diff --git a/error_templates/java/test_files/unreachable_statement_error/test.txt b/error_templates/java/test_files/unreachable_statement_error/test.txt index 9dc3a1f..ed6ea90 100644 --- a/error_templates/java/test_files/unreachable_statement_error/test.txt +++ b/error_templates/java/test_files/unreachable_statement_error/test.txt @@ -8,15 +8,14 @@ Unreachable.java:5: error: unreachable statement template: "Java.UnreachableStatementError" --- # UnreachableStatementError -Portions of the code weren't accessible because you returned a value on top of it. +This error occurs because there's code after a return statement, which can never be reached as the function has already exited. -``` - return; - System.out.println("c"); - ^^^^^^^^^^^^^^^^^^^^^^^ - } -} +## Steps to fix +1. Remove the code after the `return` statement. +```diff + System.out.println("b"); + return; +- System.out.println("c"); +} ``` -## Steps to fix -Nothing to fix \ No newline at end of file