This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor for a class implementing doubly-linked list

 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()




 
Stefan Evans
Bartender
Posts: 1691
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do a check on the line Before or Change one of your values
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up adapting Sedgewick's doubly-linked list code. Learned a little about iterators as well. Thank you.

Code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic