• 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

stuck on null pointer

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I am having a tough time with objects. I have looked at this code for over 24hrs, and seen my instuctor, but I am truely lost. This stuff makes only some sense. I am not understanding what I am doing wrong. I get the error below.

Instantiating two MyString objects.
Objects instantiated.
Testing getString() returns the string: passed
Testing nextChar() advances to the next character of the string: failed
Testing noMore() returns false if there is more string to go: failed
Testing nextChar() returns 0 and does not advance if out of string: failed
Testing noMore() returns true if there is no more string to go: failed
Testing resetIndex() resets the objects index to 0: failed
Instantiating MyString( null )
Exception in thread "main" java.lang.NullPointerException
at MyString.noMore(MyString.java:60)
at TestMyString.main(TestMyString.java:74)
Press any key to continue . . .

My sample code thus far is;
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the debug message says "Instantiating MyString(null)" which implies that you're calling

new MyString(null)

Given only the code that's not commented out, this will create a MyString in which the 'str" member is null. Calling "noMore()" will call str.length(), but since "str" is null, you'll get a NullPointerException, which is what you get any time that the thing on the left-hand-side of a dot (".") is null.

Make sense?
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Okay I understand that null is not pointing to any object but even if I omit the null component; I still get the same error. I still don't grasp the concept fully. Sorry
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Okay I've changed my code slightly. I now get the following error.

Instantiating two MyString objects.
Objects instantiated.
Testing getString() returns the string: passed
Exception in thread "main" java.lang.NullPointerException
at MyString.noMore(MyString.java:61)
at TestMyString.main(TestMyString.java:41)
Press any key to continue . . .

This error looks very similar to the previous one.
I have modified my code to the following;

This way the code makes a little more sense to me. This is how I see it; the constructor takes a String s, then the constructor initializes the state of the object(fields) to int i =0; And the String s variable reference is copied to str variable. Then next str variable is used in constructing a new String object that contains the reference to the string. Next, I assign that reference to variable a ( which contains the reference to the string that I wish to iterate through). SO i then try to iterate through the string using the reference to the string which is now a, not str. Somewhere in the process the GC comes along and wipes out my object. I don't know if this is correct what I have said is happening, but makes the most sense to me. But I am unsure how to fix the null pointer. I observed that the error is on line 61(which is a method() that returns a boolean value - true or false.)
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The garbage collector, by definition never collects an object that is reachable in any way -- i.e., if any code holds a reference to an object, that object won't be collected.

The reason that "a" appears to lose its value is that you've got two completely separate variables named "a": one is a member of the MyString class, and the other is local to the getString() method. You assign a value to the local one, and when getString() returns, that local variable ceases to exist.

The right way to write getString() is



Strings are "immutable" in Java, meaning that their values cannot be changed. There is never a reason to make a copy of one.

The variable "a" (both of them!) serves no purpose and should be removed.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
http://www.tmorris.net/pubs/npe/

If you're still stuck, provide the information that document asks for; namely, the exact stack trace and the exact corresponding source code that produced that stack trace.
 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, thanks for the input. I have managed to get my class to pass all the selected tests; however, I am still getting a NullPOinter Exception. My runtime error is the following;

Instantiating MyString( null )
Exception in thread "main" java.lang.NullPointerException
at MyString.noMore(MyString.java:67)
at TestMyString.main(TestMyString.java:74)
Press any key to continue . . .

The code responsible for this I believe is the following;

I also had another concern. But I will make another topic of it. I've tried assigning i == str.length() to a variable like int b; it still gave out the same runtime error. I don't know how to fix the dereferencing of the str String reference variable. IF anyone could please suggest something that would be great and much appreciated. Thanks again
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next step is to show line 67 of MyString.java.
Make sure it is the exact corresponding line that produced that stack trace, before you changed it
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike --

I feel like I explained this well before, but I can try again.

If you call "new MyString(null)", the constructor assigns null to the member "str". Then, later, if someone calls noMore() on that object, that method will try to execute "str.length()". Because "str" is null, this will throw a NullPointerException. You can't call any methods on a null reference.

There are three ways you might fix this, in descending order of preference: first, you could have the MyString constructor throw an exception, so the object would refuse to be created if the parameter is nulll. Second, you could check for null in the constructor, and use "" instead. Lastly, every method of MyString could check "str" for null and act accordingly.

Make sense now?
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
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