• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

constructor for a class implementing doubly-linked list

 
Ranch Hand
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()




 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Igor Mechnikov
Ranch Hand
Posts: 100
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
I'm so happy! And I wish to make this tiny ad happy too:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic