Instantiating two MyString objects.
Objects instantiated.
Testing getString() returns the string: passed
Exception in thread "main" java.lang.NullPointerException
at MyString.noMore(MyString.java:60)
at TestMyString.main(TestMyString.java:41)
Press any key to continue . . .
> Hello, I am trying to figure out what this means. Can anyone tell me what I
> am doing wrong. ..
It is apparenlty an error in your code, which
first becomes *apparent* at..
> Instantiating two MyString objects.
> Objects instantiated.
> Testing getString() returns the string: passed
> Exception in thread "main" java.lang.NullPointerException
> at MyString.noMore(MyString.java:60)
..at line 60 of MyString. Thought it might be caused
by a null parameter that something in or before..
> at TestMyString.main(TestMyString.java:41)
..was supposed to initialise, but did not.
It is hard to be more specific without the code you
are using, preferably short and complete..
<http://www.physci.org/codes/sscce.jsp>
[ Note that unless the code is complete, it is very
difficult to align the line numbers given in errors,
with the corrseponding code statements. ]
>Exception in thread "main" java.lang.NullPointerException
See
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
Without seeing your code, I can't do anything but hand out generic
advice.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
public class MyString {
// fields
private int i = 0;
private char ch;
private String str;
private boolean a = true;
// methods
// This method resets the index used to iterate through the string
public void resetIndex() {
i = 0;
}
// This method returns the reference to the string currently being
// iterated through
public String getString( ) {
//*****
// this return statement will need to be modified
//System.out.println(a);
return str;
}
// This methods returns the character at the current index and
// increments the index
public char nextChar( ) {
while(i < str.length()) {
ch = str.charAt(i);
return ch;
}
i = i + 1;
if(i == str.length()) {
ch = '0';
return ch;
} else {
ch = str.charAt(i);
}
return ch;
}
// This method returns true if the index is greater than the position
of
// the last character in the string. Otherwise, it returns false.
public boolean noMore( ) {
if( i == str.length()) {
return true;
}
else if(i != str.length()) {
}
return false;
// this return statement will need to be modified
}
// constructors
// this constructor takes initializes the object with the String s
// which will be iterated through
public MyString( String s ) {
//System.out.println(s);
i = 0;
str = s;
if(s==null) {
throw new NullPointerException();
}
//System.out.println(str);
}
}
I am getting an runtime error. Here is the error
Exception in thread "main" java.lang.NullPointerException
at MyString.<init>(MyString.java:88)
at TestMyString.main(TestMyString.java:73)
Press any key to continue . . .
I have used println calls throughout the program to try to find the problem;
however, not any luck yet. Well if someone can see see something obivious;
please point it out. Thanks again in advance.
"Roedy Green" <my_email_is_post...@munged.invalid> wrote in
message news:1kbrm1df1hr5s596h...@4ax.com...
> Hello, thanks guys for the input. I know its kind of hard to help when you
> can't see the code, so here it is.
OK here is where it gest tricky.
1st did you read the article on the SSCCE?
The reason I ask is that after adding a main to
your code to make it runnable, I tried instatiating
two MyString's and printing them, worked just fine.
This suggestst the problem is in the earlier source,
as I'd alluded to in my initial post.
Now please read the SSCCE document, and post an SSCCE*.
[ * Alternately, make a clear statement that you don't
have time to read the documents I recommend. So we at
least know where you're at. ]
> Hello, I will defiently read those documents. But for the time being; that
> is time remaining I don't really have the time to throughly go through those
> SSCCE documents. As a full course load requires much time.
A 'full course load' will be a lot fuller if you require
twice as many posts on usenet to solve a given problem.
The SSCCE helps you to presenet the information required
to allow others to spot the technical problem in the first
post.
Even better, the process of preparing an SSCCE often
*solves* the problem.
Let me know when you've had time to read it, I'll
be prepared to look more closely at your problems
after you start supplying SSCCE's.
Otherwise - *I* don't have the time to make up for
your time deficiencies.
>But for the time being; that
>is time remaining I don't really have the time to throughly go through those
>SSCCE documents. As a full course load requires much time. I have got my
>code to pass everything.
I don't think you understand how rudely that comes across. You are
saying "Your time is valueless, mine precious."
"It is perfectly ok for me to waste your time so long as it saves me
time."
You can't be expected to follow all the advice that every experienced
person gives you, but you have to be careful about tossing it back in
his face without even looking at it.
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 part of code causing this problem is the following;
public boolean noMore( ) {
if( i == str.length()) {
return true;
}else if(i != str.length()) {
}
return false;
In my testString class the line of the code complaining is;
pass = m.noMore() && (m.nextChar() == 0);
I am returning a boolean value of true in the former case if
i==str.length(), and a false if i !=str.length(). Somehow the reference to
the object(str) is becoming dereferenced. I just don't how to cure this
problem. I have tried to introduce a variable before the if clause to accept
the length's value. Then I inserted that into the the clause. For example, I
did the following;
int test = str.length()
if(i==test){
return true;
}else
}
return false;
I hope this is correct to meet the document liking. Thanks again.
"Michael" <mbia...@shaw.ca> wrote in message
news:VSwbf.425936$1i.26091@pd7tw2no...
You don't say which is line 67. Looks as if str is null. Is it?
It's somewhat difficult to say what the exact problem is without a
small, self-contained, compilable example thingy.
Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
> Hello I tried to validate my code with HTML validator,CSS validator, JSLint,
> and JOCL; but I get an error everytime I try to validate it.
My apologies. The Java-On-Line Compiler (the only one
applicable here) isn't (on-line).
The basic concept boils down to code, that when you
copy/paste it from usenet to an IDE/editor, will
compile and run to display the problem.
Check the groups for code posted in between
<sscce>
...the code...
</sscce>
..for actual examples, but most of the code in the Sun
tutorials is also written to be 'small and self contained'.
> I get the following run time error;
>
> 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 part of code causing this problem is the following;
>
> public boolean noMore( ) {
> if( i == str.length()) {
> return true;
> }else if(i != str.length()) {
> }
> return false;
>
> In my testString class the line of the code complaining is;
> pass = m.noMore() && (m.nextChar() == 0);
Which don't make much sense...
It looks like a condition of some sort, but badly constructed.
Do you know what it means?
It looks like it's composed of two parts
pass = m.noMore()
&&
m.nextChar() == 0
The first one assigns a boolean value from the result of m.noMore(). As such
this part of the condition will also have that value (true or false).
The second part compares the "next character" with the numeric value of
zero. As a "char" is a numeric value, it's a valid condition, but it's not
likely that it actually returns that...
Maybe you really wanted to compare with the *character* zero instead?
m.nextChar() == '0'
> I hope this is correct to meet the document liking.
Nope, you've not provided enough code from you "testString" for us to see
how it instantiates and manipulates the MyString.
BTW, there are some logical flaws in your class MyString as well. nextChar
will always return the first character of the string, as it never actually
increments i...
// Bjorn A
>> public boolean noMore( ) {
>> if( i == str.length()) {
>> return true;
>> }else if(i != str.length()) {
>> }
>> return false;
you could write that more concisely as:
public boolean noMore() { return i == str.length(); }
i should be reserved to use in a for loop. Using it in other contexts
is not a good idea because of the presumption i is the for index based
on an idiom dating back to FORTRAN.
you are giving us teeny-tiny pieces of a large jig-saw and asking to
tell you what the picture is!
as it is, if the exception says 'nullpointerException on line 67' then
look at line 67!
See what objects have method calls being performed on them - this object
will be the one that is null.
Once you know what object it is, then you can put a break point on the
line and debug your code.
debugging via usenet will take longer the universe has time left.
Consider using a debugger, such as the one eclipse provides. I tend
to prefer print outs and turn to the debugger as a last resort,
myself, but I think you can probably solve this problem handily by
stepping through your code line by line until it blows up. Then you
know exactly what happened and the path your code followed.
--
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html