is it string.length, without the brackets?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
LOL, where did I get that from? Erm could it be the way you're declaring it? I.e lack of public/private declarations?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I have removed the brackets and get the following errors:
cannot find symbol variable length
Thanks anyway.
G
No that was a mistake on my part,I guess my memory isn't that great.:D
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I think I know what you've done. It's saying string 's' has nothing in it. I.e. it is null.
I know you've declared in as String s = "aaa"; but it doesn't work like that. It's to do with public and private scope.
Any basic java tute should tell you where you're going wrong.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
The problem is, you have declared an instance variable s.
Then in initializeStuff you want to give it a value, but what you really did was declare and define a variable local to initializeStuff rather than defining the instance variable.
Change:
void initializeStuff() {
String s = "aaa";
}
to
void initializeStuff() {
s = "aaa";
}
or
void initializeStuff() {
this.s = "aaa";
}
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
void initializeStuff() {
String s = "aaa";
}
Good catch, didn't notice that.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Hi
You have not assigned any value for s which you declred as String s(as an instance variable)
So JVM considers it as null so by giving s.length it cant count the characters of null so you're getting null ponterException.
You just try by assigning some values ti s and see you will get the length of that s variable.
The thread has already been answered and apparently solved, not to mention that is is 1 year old. Don't give the same answer as a previous post
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
The thread has already been answered and apparently solved, not to mention that is is 2 year old.
Also, the initializations usually take place inside the Constructor or during declaration:
class Main {
String s = "aaa";
public Main() {
}
void displayStuff() {
System.out.print("The length of '"+s+"' is: ");
System.out.println(s.length());
}
}
Or
class Main {
String s = null;
public Main() {
s = "aaa";
}
public Main(String s) {
this.s = s;
}
void displayStuff() {
System.out.print("The length of '"+s+"' is: ");
System.out.println(s.length());
}
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
You intiolized the String object 's' inside a local method called...initializeStuff() ...... But 's' is instance variable...so after completing the execution of the this local variable the 's' will hove nothing...in the sence "Null"....if u use the S.O.P statement inside the initializeStuff() method, it will work.....and prints 3...
so assign a value to 's' variable where u have defined it....then it wil give answer to you.....
otherwise it will be an error.........
thank you
Again, you responded to a VERY old thread were the answer you given has already been provided by others, without contributing anything
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448