| | |
my program is compiled.but on run that i have message "Exception in thread "main" jav
![]() |
my program is compiled.but on run that i have message "Exception in thread "main" jav
0
#1 Feb 7th, 2007
my program is compiled.but on run that i have message "Exception in thread "main" java.lang.NullPointer Exception"
1. class Rev
2. {
3. char a[];
4. char i=3;
5. void str()
6. {
7. for(i='A';i<'D';i++)
8. {
9. a[i]='i';
10. }
11. }
12. void show()
13. {
14. for(int i='A';i<'D';i++)
15. {
16. System.out.print(a[i]);
17. }
18. }
19. void reversStr()
20. {
21. for(i='A';i<'D';i++)
22. {
23. System.out.print(a[i]);
24. }
25. }
26. }
27. class Revers
28. {
29. public static void main(String[] args)
30. {
31. Rev a1=new Rev();
32. a1.str();
33. a1.show();
34. a1.reversStr();
35. }
36. }
37. plz run and solve the problem
1. class Rev
2. {
3. char a[];
4. char i=3;
5. void str()
6. {
7. for(i='A';i<'D';i++)
8. {
9. a[i]='i';
10. }
11. }
12. void show()
13. {
14. for(int i='A';i<'D';i++)
15. {
16. System.out.print(a[i]);
17. }
18. }
19. void reversStr()
20. {
21. for(i='A';i<'D';i++)
22. {
23. System.out.print(a[i]);
24. }
25. }
26. }
27. class Revers
28. {
29. public static void main(String[] args)
30. {
31. Rev a1=new Rev();
32. a1.str();
33. a1.show();
34. a1.reversStr();
35. }
36. }
37. plz run and solve the problem
Re: my program is compiled.but on run that i have message "Exception in thread "main"
1
#2 Feb 7th, 2007
I think you should reconsider your for loop
I never heard that in java you can increase value of character type variable, this is possible but in Perl
Create array of characters and with use of "i" get the character from the array
so if char[] myArray = { 'A', 'B', 'C'}
then if i = 1
you get character B
Java Syntax (Toggle Plain Text)
for(i='A';i<'D';i++)
I never heard that in java you can increase value of character type variable, this is possible but in Perl
Create array of characters and with use of "i" get the character from the array
so if char[] myArray = { 'A', 'B', 'C'}
then if i = 1
you get character B
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Re: my program is compiled.but on run that i have message "Exception in thread "main"
1
#3 Feb 7th, 2007
Oh my.
For one, I think this is how you meant to represent your code:
And, I get the error because you're trying to access an array element without having defined the size of the array.
So, define a length for the array at the start of class Rev:
You're still going to end up with issues, such as java.lang.ArrayIndexOutOfBoundsException: 65 when you try to access a['A'] and so on, because 'A' as a char is 65 as an integer, and integers are what you want to work with when accessing arrays.
I have no idea what your program's trying to do, but those are some thing to consider.
For one, I think this is how you meant to represent your code:
java Syntax (Toggle Plain Text)
class Rev { char a[]; char i=3; void str() { for(i='A';i<'D';i++) { a[i]='i'; } } void show() { for(int i='A';i<'D';i++) { System.out.print(a[i]); } } void reversStr() { for(i='A';i<'D';i++) { System.out.print(a[i]); } } } class Revers { public static void main(String[] args) { Rev a1=new Rev(); a1.str(); a1.show(); a1.reversStr(); } }
And, I get the error because you're trying to access an array element without having defined the size of the array.
Java Syntax (Toggle Plain Text)
void str() { for(i='A';i<'D';i++) { a[i]='i'; // ERROR OCCURS HERE } }
So, define a length for the array at the start of class Rev:
char[] a = new char[(some integer)];You're still going to end up with issues, such as java.lang.ArrayIndexOutOfBoundsException: 65 when you try to access a['A'] and so on, because 'A' as a char is 65 as an integer, and integers are what you want to work with when accessing arrays.
I have no idea what your program's trying to do, but those are some thing to consider.
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
Re: my program is compiled.but on run that i have message "Exception in thread "main"
0
#4 Feb 7th, 2007
Re: my program is compiled.but on run that i have message "Exception in thread "main"
0
#5 Feb 7th, 2007
![]() |
Similar Threads
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- exception in thread "main" java.lang.NoClassDefFoundError (Java)
- Exception in thread "QueuedExecutor" (Java)
Other Threads in the Java Forum
- Previous Thread: urgent jdbc code
- Next Thread: Primitive types as byte arrays
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows






