How To Avoid Stack Overflow Error In Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more how to resolve stack overflow error in java about Stack Overflow the company Business Learn more about hiring developers or posting ads
How To Solve Stack Overflow Error In Java
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow
Stackoverflowerror Java Recursion
is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Java: How to avoid StackOverflowException up vote 10 down vote favorite 2
How To Handle Stack Overflow Error In Java
I'm writing a function that will call itself up to about 5000 times. Ofcourse, I get a StackOverflowException. Is there any way that I can rewrite this code in a fairly simple way?: void checkBlocks(Block b, int amm) { //Stuff that might issue a return call Block blockDown = (Block) b.getRelative(BlockFace.DOWN); if (condition) checkBlocks(blockDown, amm); Block blockUp = (Block) b.getRelative(BlockFace.UP); if (condition) checkBlocks(blockUp, amm); //Same code 4 more times for stack overflow exception in java example each side } By the way, what is the limitation of how deep we may call the functions? Thanks! java stack-overflow share|improve this question asked Apr 9 '12 at 12:52 Henrik Karlsson 3,62121534 stackoverflow.com/questions/214741/… –Balaswamy Vaddeman Apr 9 '12 at 13:01 add a comment| 6 Answers 6 active oldest votes up vote 13 down vote accepted Maybe using a stack? void checkBlocks(Block b, int amm) { Stack
and DropEventsExpandableListViewGridViewHorizontalScrollViewImageButtonImageViewLinearLayoutListViewNotificationsPasswordProgressBarProgressDialogRadioButtonRadioGroupRatingBarRelativeLayoutScrollViewSelectorSettingsSpinnerSurfaceViewTextBoxTimePickerToastToggleButtonWebViewviewMenuOnClickListenerViewPagerwidgetAutoCompleteTextViewEditTextFrameLayoutSeekBarSlidingDrawerStackViewTextViewViewFlipperxmlgamescanvasmain loopOpenGL ESCore JavaanimationapacheANTcommonsbeanutilsconvertersArrayConvertercliBasicParsercodecbinaryBase64Base64OutputStreamcsvdbcpBasicDatasourcePoolingConnectioniocomparatorCompositeFileComparatorDirectoryFileComparatorLastModifiedFileComparatorNameFileComparatorPathFileComparatorFilenameUtilsFileUtilsIOUtilsmonitorFileAlterationMonitorlang3CharUtilsClassPathUtilsmathFractionNumberUtilsStringUtilsloggingLogfactorynetCookieStoreFTPClientURLClassLoaderURLConnectionluceneappletaspectjbeanscajoCharacterclassComperablecryptoDesign PatternsdecoratorstrategyGenericsGradlegsonGsonBuilderstreamJsonReaderJsonWriterioBufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriterByteArrayInputStreamByteArrayOutputStreamConsoleDataInputStreamDataOutputStreamExternalizableFileFileDescriptorFileInputStreamFilenameFilterFileOutputStreamFileReaderFileWriterInputStreamInputStreamReaderIOExceptionObjectInputStreamObjectOutputStreamOutputStreamPrintWriterRandomAccessFileSerializableStreamTokenizerStringReaderStringWriterjsonJacksonJSON.SimplejunitlangmanagementNumberFormatExceptionProcessBuilderReferenceQueueRunnableRuntimeStringStringBufferStringBuilderSystemmat
This Site Careers Other all forums Forum: Beginning Java How to avoid stack overflow error when using recursion? Louis Lewis Ranch Hand Posts: 86 posted 3 years ago I'm trying to write a chess program with the netbeans GUI creator. https://coderanch.com/t/618521/java/java/avoid-stack-overflow-error-recursion So far I've got the visual part of the program down; I've created a http://www.javaworld.com/community/node/3153 chess board that will set up on command, and with pieces that you can move anywhere on the board by clicking them twcie (once to select, once to move), and now I'm trying to add restrictions to only allow legal moves. In order to do this, I created a separate class, which I'm calling Referee, with several methods, stack overflow all of which build on each other to finally determine whether or not a given move is legal (for example, one method checks if a move is legal only in terms of how different pieces are allowed to move, another determines, given a certain position, and using the first method, if either player is in check, and then a final method uses both the surface review based on move capabilites and the stack overflow error position review based on check to decide once and for all if a move is legal). Anyway, the details aren't super important, but the main point is that there are several methods within this class, all of which build on each other, and thus need to be called within the class itself. However, the class also has one method that I need to call from the main class (the full review, to determine if a move is legal). In order to be able to call methods from the Referee class within the main class, and the Referee class, I created a separate object in each class (ref1 inside Referee class, and ref2 inside the main class). However, I'm now having trouble with a stack overflow error. The error comes up on the object creation in the main class; I guess what's happening is that when I create the ref2 object, it has to have its own ref1 object, which in turn has to have its own ref1 object, etc. etc., on to infinity. public static void main(String args[]) { Referee ref2 = new Referee(); //the stack overflow error is happening here } public class Referee { Referee ref1 = new Referee(); } Does anyone know how to redesign my class set up such t
puts automation in the app First look: Google Cloud Machine Learning soars 7 habits of highly effective developers 10 hard-core coding tips for faster Python More Insider Sign Out Search for Suggestions for you Insider email Core Java All Core Java Agile Development Java Concurrency Java Language Java Platform Java Security Programming Careers Testing and Debugging Enterprise Java All Enterprise Java Big Data Cloud Computing Data Analytics Development Tools Java APIs Java App Dev Java Web Development Open Source Scripting and JVM Languages Learn Java All Learn Java Design Patterns Mobile Java All Mobile Java Java Android Developers Java iOS Developers News Features How-Tos Blogs Resources/White Papers Newsletters × Close Home Dustin's Software Development Cogitations and Speculations By Dustin Marx About | A software developer's public collection of tips and tricks, real-world solutions, and industry commentary related to Java programming. How-To Diagnosing and Resolving StackOverflowError More like this Effective Java NullPointerException Handling JavaScript in Java Writing good unit tests, Part 2: Follow your nose Email a friend To Use commas to separate multiple email addresses From Privacy Policy Thank you Your message has been sent. Sorry There was an error emailing this page. Comments JavaWorld | Jul 4, 2009 3:04 PM PT Comments A recent JavaWorld Community forum message (Stack Overflow after instantiating new object) reminded me that the basics of the StackOverflowError are not always understood well by people new to Java. Fortunately, the StackOverflowError is one of the easier of the runtime errors to debug and in this blog posting I will demonstrate how easy it often is to diagnose a StackOverflowError. Note that the potential for stack overflow is not limited to Java.Diagnosing the cause of a StackOverflowError can be fairly straightfoward if the code has been compiled with the debug option turned on so that line numbers are available in the resulting stack trace. In such cases, it is typically simply a matter of finding the repeating pattern of line numbers i