| Author |
constructor for a class implementing doubly-linked list
|
Igor Mechnikov
Ranch Hand
Joined: Feb 13, 2011
Posts: 100
|
|
Hello,
I am getting NullPointerException because my Tape() constructor is not correct.
I welcome any hints and suggestion regarding this or anything else that does not look right.
Thank you.
There are 3 classes: Cell, Tape, and TapeTest which has main()
|
String knock = "\u042F \u0418\u0433\u043e\u0440\u044c";
|
 |
Stefan Evans
Bartender
Joined: Jul 06, 2005
Posts: 1002
|
|
|
|
 |
Igor Mechnikov
Ranch Hand
Joined: Feb 13, 2011
Posts: 100
|
|
Stefan Evans wrote:
Thank you for responding.
The idea was to implement "initially consists of a single cell. The cell should contain a blank space, and the current cell pointer should point to it."
I suppose if it is a new Cell containing a char that would have a default value of '\u0000' already so it is not necessary to initialize it.
|
 |
Jared Malcolm
Ranch Hand
Joined: May 02, 2011
Posts: 54
|
|
Igor Mechnikov wrote:The idea was to implement "initially consists of a single cell. The cell should contain a blank space, and the current cell pointer should point to it."
Then why assign the value of NULL to the char? Why not
Also something that should be done is encapsulation... If you are changing variables in another class you should be using accessor methods to do so. I.E.
While what you are doing will work it is in no way proper.
|
SCJA 6 (Studying for SCJP 6)
|
 |
Igor Mechnikov
Ranch Hand
Joined: Feb 13, 2011
Posts: 100
|
|
Jared Malcolm wrote:
Igor Mechnikov wrote:The idea was to implement "initially consists of a single cell. The cell should contain a blank space, and the current cell pointer should point to it."
Then why assign the value of NULL to the char? Why not
While what you are doing will work it is in no way proper.
Thank you very much. I actually looked up the default value for char, did not know I could have used ' '. little light bulb flickers.
As far as encapsulation, I was given Cell and TapeTest classes and had to create Tape.
However, It makes too much sense that methods having to do with Cell ought to be where they belong.
This way Tape methods could go to the currentCell and then implement Cell methods.
|
 |
Jared Malcolm
Ranch Hand
Joined: May 02, 2011
Posts: 54
|
|
Igor Mechnikov wrote:As far as encapsulation, I was given Cell and TapeTest classes and had to create Tape.
Gotcha, was kinda curious what code if any you had been provided.
Regardless, adding that in when it wasn't required simply to practice proper OO coding SHOULD (I'd hope) impress/please the instructor.
Also...
could be done as...
|
 |
Igor Mechnikov
Ranch Hand
Joined: Feb 13, 2011
Posts: 100
|
|
Jared Malcolm wrote:
Igor Mechnikov wrote:As far as encapsulation, I was given Cell and TapeTest classes and had to create Tape.
Gotcha, was kinda curious what code if any you had been provided.
Regardless, adding that in when it wasn't required simply to practice proper OO coding SHOULD (I'd hope) impress/please the instructor.
Also...
could be done as...
I wish I had an instructor who evaluated my work (story for another day)
I changed constructor to
and Eclipse no longer complains about it,
Now, the problem becomes my for loop
since index is initialized to 0 I am getting more NullPointerExceptions
|
 |
Jared Malcolm
Ranch Hand
Joined: May 02, 2011
Posts: 54
|
|
|
So do a check on the line Before or Change one of your values
|
 |
Igor Mechnikov
Ranch Hand
Joined: Feb 13, 2011
Posts: 100
|
|
I ended up adapting Sedgewick's doubly-linked list code. Learned a little about iterators as well. Thank you.
Code:
|
 |
 |
|
|
subject: constructor for a class implementing doubly-linked list
|
|
|