[Java] Are you reading the error message properly? [How to read the stack trace]

Introduction

When a hard-written program throws an error, do you read the error message properly? Did you run away saying "Because it's English: confounded:"? By the way, I feel that I don't learn how to read errors in training. All error messages are in English, but you can decipher them properly if you hold down the points. (Because it's in English) Don't run away, face each other </ b> </ font>

Order to read error messages

Let's read it step by step using the error sample below: notebook_with_decorative_cover:

  • The content is for newcomers: beginners: so I will read the errors at a slow pace.

Sample program.java


package section01;

import java.util.Random;

/*
 *Random array generation
 *Order: 2N pattern
 */
public class practice4 {

	public static int NUM = 10;
	
	public static void main(String[] args) {
		int[] numArray = new int[0]; /*Declaring an array with zero elements*/
		Random random = new Random();
		
		int i, randomNum;
		for(i = 0 ; i < NUM ; i++) {
			numArray[i] = i; /*Here the exception occurs*/
		}
		
		for(i = NUM-1 ; i > 1 ; i--) {
			randomNum = random.nextInt(NUM-1);
			int tmp = numArray[randomNum];
			numArray[randomNum] = numArray[i];
			numArray[i] = tmp;
		}
		
		for(i = 0 ; i < NUM ; i++) {
			System.out.println(Integer.toString(numArray[i]));
		}
	}
}

Error sample


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at section01.practice4.main(practice4.java:20)★

1) Slowly return to the starting point of the error message

When all the error messages are output, the cursor is down to the bottom. In the sample, it is the part of ★ next to (practice4.java:20). So, scroll the screen and slowly return to the starting point where the error message was output: arrow_left: Once you get used to it, you will be able to return at once. See the big picture as you go around the wheel of mouse: mouse_three_button :. By the way, since it is a sample, it ends in 2 lines, but if you use the framework, you will see more: computer:

2) Look for the characters "Exception" and "Error" at the beginning

In the sample, it is the place of ʻException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0`. Other than that,

  • NullPointerException
  • NumberFormatException
  • OutOfMemoryError

etc. This is the content of the error. It doesn't matter if it doesn't make sense, so first look for Exception / Error. The important thing is to recognize that "I don't know what it means, but I'm getting an error called NullPointerException."

3) Find the class name of the program you run

Most programming languages, not just Java, will output the location of the error as "what file and what line". So, first find the class name of the program you ran: exclamation: In the sample, it is the part of ʻat section01.practice4.main (practice4.java:20)`. If you come this far

--There is an error on line 20 of practice4.java. --The content of the error is "ArrayIndexOutOfBoundsException".

Now that you know two things, modify the program to eliminate the ʻArrayIndexOutOfBoundsException that occurs on line 20 of practice4.java`: stuck_out_tongue: If you don't understand, let's ask, "The exception of △△ occurred on the nth line of ○○. How can I eliminate it?"

Error message

If you google every time, you will gradually remember it, so you won't have to google every time. Then, just by looking at a part of the word, you can get an idea of what kind of mistake you are making. In addition, most programming languages or SQL often use English words similar to error messages, which allows some guessing in any language.

You will be able to understand the content of the error even if you do not understand the meaning or reading: star:

Recommended Posts