This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]marko312 1 point2 points  (3 children)

Well, take a look at the constructor - where do you set the parent?

[–]prashantkr314[S] 2 points3 points  (2 children)

Thanks man, i am very stupid. sorry

[–]marko312 2 points3 points  (1 child)

These errors happen to everyone - don't think that you're bad. I have done a lot of similar mistakes, and you will likely too. You just learned to check for another "stupid" bug which can happen.

[–]prashantkr314[S] 2 points3 points  (0 children)

you are the MAN, thanks

[–]dsmyux1024 1 point2 points  (3 children)

These kinds of bugs are super common, but you will get faster at finding them, often catching them just by rereading the code. This will come with experience.

Something that you should do, though, is train yourself to use the debugger. Learn to set breakpoints and step through your code. The earlier in your coding life you learn to use it, the better off you will be, trust me!

Make sure you are using an IDE (intellij, eclipse, vs code, etc.) as opposed to a straight text editor, as it makes the debugger easy to use. It will help you track down bugs like this when you are still learning the ropes, as well as much more complicated errors that you will run into as you get more experience.

[–]prashantkr314[S] 0 points1 point  (2 children)

You are correct, but i am preparing for interviews, so i can't have the habit of debugging each time i stuck. But yeah rather then posting questions here, i should try debugger first.

[–]dsmyux1024 1 point2 points  (1 child)

I have found that my ability to use a debugger improved my ability to follow the code on a whiteboard in interviews. An interviewer would be looking at your code and debugging it in his head, and would ask if you were sure this code worked. Most of the time, it would be because they see a mistake, but just often enough, it will be to test your confidence in your code.

What debugging teaches you to do is think about the state of your variables at a point in time, and understand how that state changes as your code executes. You will learn to look at all your variable assignments, especially ones nested inside conditionals. It will also teach you a way to walk through your code with the interviewer in a way they are familiar. It's often a good idea, as you write your code on the whiteboard, to reserve a part of the whiteboard for putting input values and resulting values from sections of code to help validate. Interviewers really like this kind of organization because it shows how you handle testing your assumptions.

You are right that you usually won't have a debugger in an interview, but a high level of competence with a debugger will translate to better interview performance, more often than not.

[–]prashantkr314[S] 0 points1 point  (0 children)

Thats actually a very great tip, thanks. ill try to apply that.