File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes stuck on null pointer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stuck on null pointer" Watch "stuck on null pointer" New topic
Author

stuck on null pointer

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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;
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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?


[Jess in Action][AskingGoodQuestions]
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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

Joined: Sep 23, 2005
Posts: 85
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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
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

Joined: Sep 24, 2003
Posts: 1608
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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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?
 
 
subject: stuck on null pointer
 
Similar Threads
missing return statement?
array confusion
Index of the last character of Strings
Detecting a String that represents a number
Weird unicode translation problem