[JAVA] The program doesn't work, the program doesn't stop ...

Introduction

(* This article is for those who are just starting to learn the program.) Inexperienced programmers and new programmers who are just starting the program "What if a bug occurs? You may be full of anxiety, such as "If this doesn't work ..." First of all, it will not start unless you run the program, so let's run it for the time being: thumbs up_tone2:

When I'm an IT school teacher ...

"I'm sorry ... it doesn't work ...: frowning2 :: frowning2:" "A lot of strange characters have come out ...: sob:" "The program doesn't stop-: scream:"

I'm doing: computer: school instructor: computer :, so I'll answer questions like this politely, of course. It's only ** at the beginning **: sweat_smile: In the end, you have to solve it yourself: walking_tone1:

Let's calm down first

Whether the program isn't running or doesn't stop, let's calm down first. There is always a cause: bangbang:

When the program doesn't stop

(* This time, I will describe it on the assumption that Eclipse is used) Let's stop first: sweat_smile: This is often the case when you are addicted to an infinite loop, such as when you practice for sentences.

stop3.png

Here is the code that caused the infinite loop

	Calendar cal = Calendar.getInstance();
	int year = cal.get(Calendar.YEAR);

	List<Integer> yearList = new ArrayList<Integer>();

	//List from the current year to 10 years later
	for(int i = year ; i <= year + 10; year++){

		yearList.add(i);

	}

If you use "** debug mode **", you will notice immediately, but if you do not know how to ** debug ** yet, (Check out debug mode right away: expressionless :) You will notice if you try to output yearList.add (i); as System.out.println (i);. (Please stop right away: expressionless :: expressionless :) This time, it was caused by year ++ in the repeat condition. If the number of years increases, it won't stop.

When the program doesn't work

First of all, "** Let's see the console **": eyes :: eyes: The details of the error and the relevant part should be output. The following is the content displayed on the console by running the program containing the bug.

java.lang.NullPointerException  //★ Error details
    at instructor.BugCode.main(BugCode.java:17) //☆ Where the error occurred
java.lang.ArithmeticException: / by zero //★ Error details
    at instructor.BugCode.main(BugCode.java:38) //☆ Where the error occurred

Don't panic even if you have multiple lines, look for the "... Exception" part and the class name you created: exclamation:

java.lang.IndexOutOfBoundsException: Index: 3, Size: 2 //★ Error details
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at instructor.BugCode.main(BugCode.java:52) //☆ Where the error occurred

★ If you can check the details of the error and the location where the error occurred, you should have some idea. Let's find out about Exceptions without any hassle: rage:

in conclusion

As I wrote at the beginning, the program does not start unless it is run. You can't grow if you're scared of bugs: arrow_lower_right: (Stop reckless code: rolling_eyes :) There is no program without bugs, so run the program more and more and get used to identifying bugs. Let's always fight bug fixes ~: fist:

Recommended Posts

The program doesn't work, the program doesn't stop ...
When the vagrant command doesn't work for some reason
It doesn't work if the Map key is an array
@ClassRules doesn't work with PowerMockRunner
ruby get.chomp.to_i doesn't work, doesn't load
Arrays.asList (int []). Contains (int) doesn't work
I have a question because the gradle setup doesn't work
What to do if validation doesn't work with the update action
"RSpec doesn't work!" The cause was spring, so I investigated it.
[Beginner] When rails s doesn't work
When PyCall doesn't work with PyCall :: PythonNotFound
Order of processing in the program